1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
| package com.nsb.app.ui.view;
import android.animation.ValueAnimator; import android.annotation.TargetApi; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.os.Build; import android.util.AttributeSet; import android.view.View;
public class MaterialCheckBox extends View {
private Paint paintBlue; private Paint paintWithe;
private int borderColor = Color.GRAY; private int backgroundColor = Color.BLUE; private int doneShapeColor = Color.WHITE;
private int baseWidth; private int borderWidth; private int width, height;
private float[] points = new float[8];
private int DURATION = 200; private boolean checked; private float correctProgress;
private boolean drawRecting; private boolean isAnim; private OnCheckedChangeListener listener;
private float padding;
public MaterialCheckBox(Context context) { this(context, null); }
public MaterialCheckBox(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public MaterialCheckBox(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public MaterialCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }
private void init(Context context) {
borderWidth = baseWidth = dp2px(2);
paintBlue = new Paint(Paint.ANTI_ALIAS_FLAG); paintBlue.setColor(borderColor); paintBlue.setStrokeWidth(borderWidth);
paintWithe = new Paint(Paint.ANTI_ALIAS_FLAG); paintWithe.setColor(doneShapeColor); paintWithe.setStrokeWidth(dp2px(2));
setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setChecked(!isChecked()); } }); drawRecting = true; }
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); height = width = Math.max(w, h);
points[0] = 101 / 378f * width; points[1] = 0.5f * width;
points[2] = 163 / 378f * width; points[3] = 251 / 378f * width;
points[4] = 149 / 378f * width; points[5] = 250 / 378f * width;
points[6] = 278 / 378f * width; points[7] = 122 / 378f * width;
padding = 57 / 378f * width;
}
@Override protected void onDraw(Canvas canvas) { RectF rect = new RectF(padding, padding, width - padding, height - padding); canvas.drawRoundRect(rect, baseWidth, baseWidth, paintBlue); if (drawRecting) { canvas.drawRect(padding + borderWidth, padding + borderWidth, width - padding - borderWidth, height - padding - borderWidth, paintWithe); } else { if (correctProgress > 0) { if (correctProgress < 1 / 3f) { float x = points[0] + (points[2] - points[0]) * correctProgress; float y = points[1] + (points[3] - points[1]) * correctProgress; canvas.drawLine(points[0], points[1], x, y, paintWithe); } else { float x = points[4] + (points[6] - points[4]) * correctProgress; float y = points[5] + (points[7] - points[5]) * correctProgress; canvas.drawLine(points[0], points[1], points[2], points[3], paintWithe); canvas.drawLine(points[4], points[5], x, y, paintWithe); } } } }
public void setBackgroundColor(int backgroundColor) { this.backgroundColor = backgroundColor; }
public void setDoneShapeColor(int doneShapeColor) { this.doneShapeColor = doneShapeColor; paintWithe.setColor(doneShapeColor); }
public void setBorderColor(int borderColor) { this.borderColor = borderColor; }
public void setBorderWidth(int baseWidth) { this.baseWidth = baseWidth; }
public boolean isChecked() { return checked; }
public void setChecked(boolean checked) { this.checked = checked; if (checked) { showRect(); } else { hideCorrect(); } }
private void hideRect() { if (isAnim) { return; } isAnim = true; drawRecting = true; ValueAnimator va = ValueAnimator.ofFloat(0, 1).setDuration(DURATION); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float p = (float) animation.getAnimatedValue(); float c = 1f - p; borderWidth = (int) (baseWidth + c * (width - baseWidth)); paintBlue.setColor(evaluate(c, borderColor, backgroundColor)); invalidate(); if (p >= 1) { isAnim = false; if (listener != null) { checked = false; listener.onCheckedChanged(MaterialCheckBox.this, checked); } } } }); va.start(); }
private void showRect() { if (isAnim) { return; } isAnim = true; drawRecting = true; ValueAnimator va = ValueAnimator.ofFloat(0, 1).setDuration(DURATION); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float p = (float) animation.getAnimatedValue(); borderWidth = (int) (10 + p * (width - 10)); paintBlue.setColor(evaluate(p, borderColor, backgroundColor)); invalidate(); if (p >= 1) { isAnim = false; drawRecting = false; showCorrect(); } } }); va.start(); }
private void showCorrect() { if (isAnim) { return; } isAnim = true; correctProgress = 0; drawRecting = false; ValueAnimator va = ValueAnimator.ofFloat(0, 1).setDuration(DURATION); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { correctProgress = (float) animation.getAnimatedValue(); invalidate(); if (correctProgress >= 1) { isAnim = false; if (listener != null) { checked = true; listener.onCheckedChanged(MaterialCheckBox.this, checked); } } } }); va.start(); }
private void hideCorrect() { if (isAnim) { return; } isAnim = true; correctProgress = 1; drawRecting = false; ValueAnimator va = ValueAnimator.ofFloat(0, 1).setDuration(DURATION); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float p = (float) animation.getAnimatedValue(); correctProgress = 1f - p; invalidate(); if (p >= 1) { isAnim = false; hideRect(); } } }); va.start(); }
public void setOnCheckedChangedListener(OnCheckedChangeListener listener) { this.listener = listener; }
interface OnCheckedChangeListener { void onCheckedChanged(View view, boolean isChecked); }
private int evaluate(float fraction, int startValue, int endValue) { int startInt = startValue; int startA = (startInt >> 24) & 0xff; int startR = (startInt >> 16) & 0xff; int startG = (startInt >> 8) & 0xff; int startB = startInt & 0xff;
int endInt = endValue; int endA = (endInt >> 24) & 0xff; int endR = (endInt >> 16) & 0xff; int endG = (endInt >> 8) & 0xff; int endB = endInt & 0xff; return ((startA + (int) (fraction * (endA - startA))) << 24) | ((startR + (int) (fraction * (endR - startR))) << 16) | ((startG + (int) (fraction * (endG - startG))) << 8) | ((startB + (int) (fraction * (endB - startB)))); }
public int dp2px(float value) { final float scale = getContext().getResources().getDisplayMetrics().densityDpi; return (int) (value * (scale / 160) + 0.5f); } }
|