- public class FloatEvaluator implements TypeEvaluator {
- public Object evaluate(float fraction, Object startValue, Object endValue) {
- float startFloat = ((Number) startValue).floatValue();
- return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);
- }
- }
这个typeevaluator主要表示根据完成度返回一个对象。
- Point point1 = new Point(0, 0);
- Point point2 = new Point(300, 300);
- ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);
- anim.setDuration(5000);
- anim.start();
- public class MyAnimView extends View {
- public static final float RADIUS = 50f;
- private Point currentPoint;
- private Paint mPaint;
- public MyAnimView(Context context, AttributeSet attrs) {
- super(context, attrs);
- mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
- mPaint.setColor(Color.BLUE);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- if (currentPoint == null) {
- currentPoint = new Point(RADIUS, RADIUS);
- drawCircle(canvas);
- startAnimation();
- } else {
- drawCircle(canvas);
- }
- }
- private void drawCircle(Canvas canvas) {
- float x = currentPoint.getX();
- float y = currentPoint.getY();
- canvas.drawCircle(x, y, RADIUS, mPaint);
- }
- private void startAnimation() {
- Point startPoint = new Point(RADIUS, RADIUS);
- Point endPoint = new Point(getWidth() - RADIUS, getHeight() - RADIUS);
- ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
- anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
- @Override
- public void onAnimationUpdate(ValueAnimator animation) {
- currentPoint = (Point) animation.getAnimatedValue();
- invalidate();
- }
- });
- anim.setDuration(5000);
- anim.start();
- }
- }
然后通过invalidate进行回调机制
然后再在xml文件中加载这个class就ojbk了。
- public class MyAnimView extends View {
- ...
- private String color;
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- mPaint.setColor(Color.parseColor(color));
- invalidate();
- }
- ...
- }