share Paint between onDraw calls
This commit is contained in:
parent
c3fff75a03
commit
0c51c89d22
@ -28,12 +28,18 @@ import android.graphics.Color;
|
|||||||
import android.graphics.ColorFilter;
|
import android.graphics.ColorFilter;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
|
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
|
||||||
public class VanillaImageButton extends ImageButton {
|
public class VanillaImageButton extends ImageButton {
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private static int mNormalTint;
|
private static int mNormalTint;
|
||||||
private static int mActiveTint;
|
private static int mActiveTint;
|
||||||
private int mCircleColor;
|
/**
|
||||||
|
* The paint used to draw this buttons circle.
|
||||||
|
*/
|
||||||
|
private @Nullable Paint mCirclePaint;
|
||||||
|
|
||||||
public VanillaImageButton(Context context) {
|
public VanillaImageButton(Context context) {
|
||||||
this(context, null);
|
this(context, null);
|
||||||
@ -51,8 +57,12 @@ public class VanillaImageButton extends ImageButton {
|
|||||||
|
|
||||||
if (attrs != null) {
|
if (attrs != null) {
|
||||||
TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.VanillaImageButton, 0, 0);
|
TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.VanillaImageButton, 0, 0);
|
||||||
mCircleColor = a.getColor(R.styleable.VanillaImageButton_backgroundCircleColor, 0);
|
final int color = a.getColor(R.styleable.VanillaImageButton_backgroundCircleColor, 0);
|
||||||
a.recycle();
|
a.recycle();
|
||||||
|
|
||||||
|
mCirclePaint = new Paint();
|
||||||
|
mCirclePaint.setColor(color);
|
||||||
|
mCirclePaint.setAntiAlias(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateImageTint(-1);
|
updateImageTint(-1);
|
||||||
@ -66,16 +76,14 @@ public class VanillaImageButton extends ImageButton {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDraw(Canvas canvas) {
|
public void onDraw(Canvas canvas) {
|
||||||
if (mCircleColor != 0) {
|
if (mCirclePaint != null) {
|
||||||
// Draw a circle on the background, but only
|
// Draw a circle on the background, but only
|
||||||
// if the color is set.
|
// if the color is set.
|
||||||
final int x = getWidth() / 2;
|
final int x = getWidth() / 2;
|
||||||
final int y = getHeight() / 2;
|
final int y = getHeight() / 2;
|
||||||
final float r = (x > y ? y : x);
|
final float r = (x > y ? y : x);
|
||||||
Paint paint = new Paint();
|
|
||||||
paint.setColor(mCircleColor);
|
canvas.drawCircle(x, y, r*0.80f, mCirclePaint);
|
||||||
paint.setAntiAlias(true);
|
|
||||||
canvas.drawCircle(x, y, r*0.80f, paint);
|
|
||||||
}
|
}
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user