public class Sample08ObjectAnimatorView extends View {
final float radius = dpToPixel(100);
float progress = 0;
float middle = 0;
float small = 0;//属性动画的属性
RectF arcRectF = new RectF();
RectF arcRect1 = new RectF();
RectF arcRect2 = new RectF();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);{
paint.setTextSize(dpToPixel(10));
paint.setTextAlign(Paint.Align.CENTER);
}
public Sample08ObjectAnimatorView(Context context) {
super(context);
}
public Sample08ObjectAnimatorView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Sample08ObjectAnimatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public float getProgress() {
return progress;
}
public void setProgress(float progress) {
this.progress = progress;
invalidate();
}
public float getSmall() {
return small;
}
public void setSmall(float small) {
this.small = small;
invalidate();
}
public float getMiddle() {
return middle;
}
public void setMiddle(float middle) {
this.middle = middle;
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
float centerX = getWidth() / 2;
float centerY = getHeight() / 2;
paint.setColor(Color.parseColor("#66E91E63"));
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(centerX, centerY, radius / 3, paint);//先画三哥圆
canvas.drawCircle(centerX, centerY, 2 * radius / 3, paint);
canvas.drawCircle(centerX, centerY, radius, paint);
paint.setColor(Color.parseColor("#E91E63"));
paint.setStrokeCap(Paint.Cap.ROUND);
// paint.setStrokeWidth(dpToPixel(15));
arcRectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
arcRect2.set(centerX - 2 * radius / 3, centerY - 2 * radius / 3, centerX + 2 * radius / 3, centerY + 2 * radius / 3);
arcRect1.set(centerX - radius / 3, centerY - radius / 3, centerX + radius / 3, centerY + radius / 3);
canvas.drawArc(arcRectF, -90, progress * 3.6f, false, paint);//画圆环的动画(类似于进度条)
canvas.drawArc(arcRect1, -45, progress * 3.6f, false, paint);
canvas.drawArc(arcRect2, -90, progress * 3.6f, false, paint);
Path path = new Path();//裁切中间图形的大小
path.addCircle(centerX, centerY, small, Path.Direction.CW);
canvas.save();
canvas.clipPath(path);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(centerX, centerY, radius / 3, paint);
paint.setColor(Color.WHITE);
canvas.drawText("RTU实时值", centerX, centerY - (paint.ascent() + paint.descent()) / 2, paint);
canvas.restore();
paint.setColor(Color.GRAY);//最后波纹的圆环
paint.setStyle(Paint.Style.STROKE);
if (middle == radius || middle == 0) {
paint.setColor(Color.TRANSPARENT);
}
canvas.drawCircle(centerX, centerY, middle, paint);
canvas.drawCircle(centerX, centerY, middle / 4 + radius, paint);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
ObjectAnimator animator = ObjectAnimator.ofFloat(this, "progress", 0, 100);
animator.setInterpolator(new FastOutSlowInInterpolator());
ObjectAnimator animator1 = ObjectAnimator.ofFloat(this, "small", 0, radius / 3);
animator1.setInterpolator(new OvershootInterpolator(2));
ObjectAnimator animator2 = ObjectAnimator.ofFloat(this, "middle", radius / 3, radius);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(animator).before(animator1).before(animator2);
animatorSet.setDuration(1000);
animatorSet.start();
}
public static float dpToPixel(float dp) {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
return dp * metrics.density;
}
}