Logo.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package io.card.payment;
  2. /* Logo.java
  3. * See the file "LICENSE.md" for the full license governing this code.
  4. */
  5. import android.content.Context;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Canvas;
  9. import android.graphics.Paint;
  10. import android.graphics.Rect;
  11. import android.graphics.RectF;
  12. import com.riso.scancardlibrary.R;
  13. // TODO - cache logo drawing as a bitmap and just draw that
  14. // TODO - get alpha overlay computation working properly with whites. should not look gray.
  15. class Logo {
  16. private static final int ALPHA = 100;
  17. private final Paint mPaint;
  18. private Bitmap mLogo;
  19. private boolean mUseCardIOLogo;
  20. private final Context mContext;
  21. public Logo(Context context) {
  22. mPaint = new Paint();
  23. mPaint.setAntiAlias(true);
  24. mPaint.setAlpha(ALPHA);
  25. mLogo = null;
  26. mContext = context;
  27. }
  28. void loadLogo(boolean useCardIOLogo) {
  29. if (mLogo != null && useCardIOLogo == mUseCardIOLogo) {
  30. return; // no change, don't reload
  31. }
  32. mUseCardIOLogo = useCardIOLogo;
  33. if (useCardIOLogo) {
  34. mLogo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.cio_card_io_logo);
  35. } else {
  36. mLogo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.cio_paypal_logo);
  37. }
  38. }
  39. public void draw(Canvas canvas, float maxWidth, float maxHeight) {
  40. if (mLogo == null) {
  41. loadLogo(false);
  42. }
  43. canvas.save();
  44. float drawWidth, drawHeight;
  45. float targetAspectRatio = (float) mLogo.getHeight() / mLogo.getWidth();
  46. if ((maxHeight / maxWidth) < targetAspectRatio) {
  47. drawHeight = maxHeight;
  48. drawWidth = maxHeight / targetAspectRatio;
  49. } else {
  50. drawWidth = maxWidth;
  51. drawHeight = maxWidth * targetAspectRatio;
  52. }
  53. float halfWidth = drawWidth / 2;
  54. float halfHeight = drawHeight / 2;
  55. canvas.drawBitmap(mLogo, new Rect(0, 0, mLogo.getWidth(), mLogo.getHeight()), new RectF(
  56. -halfWidth, -halfHeight, halfWidth, halfHeight), mPaint);
  57. canvas.restore();
  58. }
  59. }