//建一个类
public class CirclePoint {
private float positionX;
private float positionY;
private float raduis=100;
public float getPositionX() {
return positionX;
}
public void setPositionX(float positionX) {
this.positionX = positionX;
}
public float getPositionY() {
return positionY;
}
public void setPositionY(float positionY) {
this.positionY = positionY;
}
public float getRaduis() {
return raduis;
}
public void setRaduis(float raduis) {
this.raduis = raduis;
}
}
//自定义一个View
public class CircleView extends View implements View.OnClickListener {
private CirclePoint circlePoint;
private Paint paint;
public CircleView(Context context) {
super(context);
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void setRed(int red) {
this.red = red;
}
int red=0;
int green=200;
int black=150;
private void init() {
circlePoint = new CirclePoint();
paint = new Paint();
paint.setColor(Color.argb(255,red,green,black));
setOnClickListener(this);
}
@Override
public void onClick(View view) {
startAnimation();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.argb(255,red,green,black));
canvas.drawCircle(circlePoint.getPositionX()+100,circlePoint.getPositionY()+100,circlePoint.getRaduis(),paint);
}
private void startAnimation() {
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(circlePoint, "positionX", 100, 500);
objectAnimatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
}
});
ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(circlePoint, "positionY", 100, 1000);
ObjectAnimator objectAnimatorColor = ObjectAnimator.ofInt(this,"red",0,255);
animatorSet.play(objectAnimatorX).with(objectAnimatorY).with(objectAnimatorColor);
animatorSet.setDuration(5000);
animatorSet.start();
}
}