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
| /* * Created by Hanks * Copyright (c) 2015 Nashangban. All rights reserved * */ package app.hanks.com.customanimateview.OpAnimateView;
import android.animation.ValueAnimator; import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PointF; import android.os.Build; import android.util.AttributeSet; import android.view.View;
import java.util.ArrayList; import java.util.List;
import app.hanks.com.customanimateview.R;
/** * Created by Hanks on 2015/5/27. */ public class OpAnimationView extends View {
private Paint paint; private Paint paintShadow;
private int width; private int height; private int c;
private boolean isAnimating = false; //is isAnimating? private boolean isRgihtShape = false; //is in right_shap?
private float progress; //animation‘s progress private int DURATION = 300; // animation DURATION private int r; //randius private int shadowWidth; //shadow randius private int defaultShapeWidth; private int shapeWidth;
public OpAnimationView(Context context) { this(context, null); }
public OpAnimationView(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public OpAnimationView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public OpAnimationView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }
/** * init paint * * @param context * @param attrs * @param defStyleAttr */ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
defaultShapeWidth = dp2px(4);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paintShadow = new Paint(Paint.ANTI_ALIAS_FLAG); setLayerType(LAYER_TYPE_SOFTWARE, paintShadow);
shadowWidth = dp2px(4); paintShadow.setShadowLayer(shadowWidth, 0.0f, dp2px(2), Color.parseColor("#66000000"));
// load the styled attributes and set their properties TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.OpAnimationView, defStyleAttr, 0);
setBackgroundColor(attributes.getColor(R.styleable.OpAnimationView_backgroundColor, Color.parseColor("#FF4081"))); setShapeColor(attributes.getColor(R.styleable.OpAnimationView_shapeColor, Color.parseColor("#ffffff"))); shapeWidth = attributes.getDimensionPixelOffset(R.styleable.OpAnimationView_shapeWidth, defaultShapeWidth); setShapeWidth(shapeWidth);
// We no longer need our attributes TypedArray, give it back to cache attributes.recycle();
}
private List<PointF> points = new ArrayList<>();
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); width = w; height = h; r = Math.min(w, h) / 2 - shadowWidth; float g2 = (float) Math.sqrt(2); c = (int) (r / g2); float dis = r - c + dp2px(4);
points.clear(); points.add(new PointF(c + dis, c / 3f + dis)); points.add(new PointF(c + dis, c * 5f / 3f + dis)); points.add(new PointF(c / 3f + dis, c + dis)); points.add(new PointF(c * 5f / 3f + dis, c + dis)); points.add(new PointF(c * (1 + 3 * g2) / 3f + dis, c * (3 - g2) / 3f + dis)); points.add(new PointF(c * (1 + g2) / 3f + shapeWidth / g2 / 2 + dis, c * (3 + g2) / 3f + shapeWidth / g2 / 2 + dis)); points.add(new PointF(c * (1 + g2) / 3f - shapeWidth / g2 / 2 + dis, c * (3 + g2) / 3f + shapeWidth / g2 / 2 + dis)); }
//set background circle color public void setBackgroundColor(int color) { paintShadow.setColor(color); }
//set center shape color public void setShapeColor(int color) { paint.setColor(color); }
public void setShapeWidth(int width) { paint.setStrokeWidth(width); }
@Override protected void onDraw(Canvas canvas) {
canvas.drawCircle(width / 2, height / 2, r, paintShadow);
if (isRgihtShape) { // right --> add float x0 = (points.get(0).x - points.get(4).x) * progress + points.get(4).x; float y0 = (points.get(0).y - points.get(4).y) * progress + points.get(4).y; float x1 = (points.get(1).x - points.get(6).x) * progress + points.get(6).x; float y1 = (points.get(1).y - points.get(6).y) * progress + points.get(6).y;
float x2 = points.get(2).x; float y2 = points.get(2).y; float x3 = (points.get(3).x - points.get(5).x) * progress + points.get(5).x; float y3 = (points.get(3).y - points.get(5).y) * progress + points.get(5).y; canvas.drawLine(x0, y0, x1, y1, paint); canvas.drawLine(x2, y2, x3, y3, paint);
} else { //add --> right
float x0 = (points.get(4).x - points.get(0).x) * progress + points.get(0).x; float y0 = (points.get(4).y - points.get(0).y) * progress + points.get(0).y; float x1 = (points.get(6).x - points.get(1).x) * progress + points.get(1).x; float y1 = (points.get(6).y - points.get(1).y) * progress + points.get(1).y;
float x2 = points.get(2).x; float y2 = points.get(2).y; float x3 = (points.get(5).x - points.get(3).x) * progress + points.get(3).x; float y3 = (points.get(5).y - points.get(3).y) * progress + points.get(3).y; canvas.drawLine(x0, y0, x1, y1, paint); canvas.drawLine(x2, y2, x3, y3, paint); } }
public boolean isRightShape() { return isRgihtShape; }
/** * add_shape to right_shape */ public void add2right() { if (isAnimating) { return; } progress = 0f; isAnimating = true; ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1); valueAnimator.setDuration(DURATION); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { progress = (float) animation.getAnimatedValue(); if (progress >= 1) { isRgihtShape = true; isAnimating = false; } else { invalidate(); } } }); valueAnimator.start(); }
/** * right_shape to add_shape */ public void right2add() { if (isAnimating) { return; } progress = 0f; isAnimating = true; ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1); valueAnimator.setDuration(DURATION); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { progress = (float) animation.getAnimatedValue(); if (progress >= 1) { isRgihtShape = false; isAnimating = false; } else { invalidate(); } } }); valueAnimator.start(); }
/** * dpת px. * * @param value the value * @return the int */ public int dp2px(float value) { final float scale = getResources().getDisplayMetrics().densityDpi; return (int) (value * (scale / 160) + 0.5f); } }
|