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.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
|
@ -7,43 +7,47 @@ import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Matrix;
|
import android.graphics.Matrix;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
|
import android.graphics.PorterDuff;
|
||||||
|
import android.graphics.PorterDuffColorFilter;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
import com.kabouzeid.gramophone.R;
|
import com.afollestad.cabinet.R;
|
||||||
|
|
||||||
|
|
||||||
public class CircleView extends FrameLayout {
|
public class CircleView extends FrameLayout {
|
||||||
|
|
||||||
private final Bitmap mCheck;
|
private final Bitmap mCheck;
|
||||||
private final Paint paint;
|
private final Paint paint;
|
||||||
private final Paint paintBorder;
|
private final Paint paintBorder;
|
||||||
|
private Paint paintCheck;
|
||||||
private final int borderWidth;
|
private final int borderWidth;
|
||||||
|
|
||||||
public CircleView(Context context) {
|
public CircleView(Context context) {
|
||||||
this(context, null, 0);
|
this(context, null, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CircleView(Context context, AttributeSet attrs) {
|
public CircleView(Context context, AttributeSet attrs) {
|
||||||
this(context, attrs, 0);
|
this(context, attrs, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
|
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||||
super(context, attrs, defStyleAttr);
|
super(context, attrs, defStyleAttr);
|
||||||
final int checkSize = (int) context.getResources().getDimension(R.dimen.circle_view_check);
|
final int checkSize = (int) context.getResources().getDimension(R.dimen.circle_view_check);
|
||||||
mCheck = getResizedBitmap(BitmapFactory.decodeResource(context.getResources(),
|
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);
|
borderWidth = (int) getResources().getDimension(R.dimen.circle_view_border);
|
||||||
|
|
||||||
paint = new Paint();
|
paint = new Paint();
|
||||||
paint.setAntiAlias(true);
|
paint.setAntiAlias(true);
|
||||||
|
|
||||||
paintBorder = new Paint();
|
paintBorder = new Paint();
|
||||||
paintBorder.setAntiAlias(true);
|
paintBorder.setAntiAlias(true);
|
||||||
paintBorder.setColor(Color.BLACK);
|
paintBorder.setColor(Color.BLACK);
|
||||||
|
|
||||||
setWillNotDraw(false);
|
setWillNotDraw(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
|
private static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
|
||||||
int width = bm.getWidth();
|
int width = bm.getWidth();
|
||||||
int height = bm.getHeight();
|
int height = bm.getHeight();
|
||||||
|
|
@ -55,20 +59,20 @@ public class CircleView extends FrameLayout {
|
||||||
bm.recycle();
|
bm.recycle();
|
||||||
return resizedBitmap;
|
return resizedBitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBackgroundColor(int color) {
|
public void setBackgroundColor(int color) {
|
||||||
paint.setColor(color);
|
paint.setColor(color);
|
||||||
requestLayout();
|
requestLayout();
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBorderColor(int color) {
|
public void setBorderColor(int color) {
|
||||||
paintBorder.setColor(color);
|
paintBorder.setColor(color);
|
||||||
requestLayout();
|
requestLayout();
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||||
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
|
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
|
||||||
|
|
@ -84,22 +88,29 @@ public class CircleView extends FrameLayout {
|
||||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
|
|
||||||
int canvasSize = canvas.getWidth();
|
int canvasSize = canvas.getWidth();
|
||||||
if (canvas.getHeight() < canvasSize)
|
if (canvas.getHeight() < canvasSize)
|
||||||
canvasSize = canvas.getHeight();
|
canvasSize = canvas.getHeight();
|
||||||
|
|
||||||
int circleCenter = (canvasSize - (borderWidth * 2)) / 2;
|
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) + borderWidth - 4.0f, paintBorder);
|
||||||
canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) - 4.0f, paint);
|
canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, ((canvasSize - (borderWidth * 2)) / 2) - 4.0f, paint);
|
||||||
|
|
||||||
if (isActivated()) {
|
if (isActivated()) {
|
||||||
final int offset = (canvasSize / 2) - (mCheck.getWidth() / 2);
|
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