From a78ca26b05fa9bc31f7371d00d50926cb7cca8f1 Mon Sep 17 00:00:00 2001 From: Aidan Follestad Date: Tue, 21 Apr 2015 14:53:27 -0500 Subject: [PATCH] Circle view will show black checkmark if background is white. --- .../gramophone/views/CircleView.java | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/kabouzeid/gramophone/views/CircleView.java b/app/src/main/java/com/kabouzeid/gramophone/views/CircleView.java index 9e4064f7..1d4bd40d 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/views/CircleView.java +++ b/app/src/main/java/com/kabouzeid/gramophone/views/CircleView.java @@ -1,4 +1,4 @@ -package com.kabouzeid.gramophone.views; +package com.afollestad.cabinet.views; import android.content.Context; import android.graphics.Bitmap; @@ -7,43 +7,47 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; import android.util.AttributeSet; import android.widget.FrameLayout; -import com.kabouzeid.gramophone.R; +import com.afollestad.cabinet.R; + public class CircleView extends FrameLayout { - + private final Bitmap mCheck; private final Paint paint; private final Paint paintBorder; + private Paint paintCheck; private final int borderWidth; - + public CircleView(Context context) { this(context, null, 0); } - + public CircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } - + public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); final int checkSize = (int) context.getResources().getDimension(R.dimen.circle_view_check); mCheck = getResizedBitmap(BitmapFactory.decodeResource(context.getResources(), - R.drawable.ic_check), checkSize, checkSize); + R.drawable.ic_check), checkSize, checkSize); borderWidth = (int) getResources().getDimension(R.dimen.circle_view_border); - + paint = new Paint(); paint.setAntiAlias(true); - + paintBorder = new Paint(); paintBorder.setAntiAlias(true); paintBorder.setColor(Color.BLACK); - + setWillNotDraw(false); } - + private static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { int width = bm.getWidth(); int height = bm.getHeight(); @@ -55,20 +59,20 @@ public class CircleView extends FrameLayout { bm.recycle(); return resizedBitmap; } - + @Override public void setBackgroundColor(int color) { paint.setColor(color); requestLayout(); invalidate(); } - + public void setBorderColor(int color) { paintBorder.setColor(color); requestLayout(); invalidate(); } - + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); @@ -84,22 +88,29 @@ public class CircleView extends FrameLayout { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } - + @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); - + int canvasSize = canvas.getWidth(); if (canvas.getHeight() < canvasSize) canvasSize = canvas.getHeight(); - + int circleCenter = (canvasSize - (borderWidth * 2)) / 2; canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) + borderWidth - 4.0f, paintBorder); canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) - 4.0f, paint); - + if (isActivated()) { final int offset = (canvasSize / 2) - (mCheck.getWidth() / 2); - canvas.drawBitmap(mCheck, offset, offset, null); + if (paint.getColor() == Color.WHITE && paintCheck == null) { + paintCheck = new Paint(); + paintCheck.setAntiAlias(true); + paintCheck.setColorFilter(new PorterDuffColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN)); + } else { + paintCheck = null; + } + canvas.drawBitmap(mCheck, offset, offset, paintCheck); } } -} +} \ No newline at end of file