Circle view will show black checkmark if background is white.
This commit is contained in:
parent
6564986f52
commit
a78ca26b05
1 changed files with 31 additions and 20 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue