动画中级

  1. public class FloatEvaluator implements TypeEvaluator {  
  2.     public Object evaluate(float fraction, Object startValue, Object endValue) {  
  3.         float startFloat = ((Number) startValue).floatValue();  
  4.         return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);  
  5.     }  

  1. }  
这个typeevaluator主要表示根据完成度返回一个对象。
  1. Point point1 = new Point(00);  
  2. Point point2 = new Point(300300);  
  3. ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);  
  4. anim.setDuration(5000);  
  5. anim.start();  
point1和point2分别表示两个自己创建的对象。
  1. public class MyAnimView extends View {  
  2.   
  3.     public static final float RADIUS = 50f;  
  4.   
  5.     private Point currentPoint;  
  6.   
  7.     private Paint mPaint;  
  8.   
  9.     public MyAnimView(Context context, AttributeSet attrs) {  
  10.         super(context, attrs);  
  11.         mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  12.         mPaint.setColor(Color.BLUE);  
  13.     }  
  14.   
  15.     @Override  
  16.     protected void onDraw(Canvas canvas) {  
  17.         if (currentPoint == null) {  
  18.             currentPoint = new Point(RADIUS, RADIUS);  
  19.             drawCircle(canvas);  
  20.             startAnimation();  
  21.         } else {  
  22.             drawCircle(canvas);  
  23.         }  
  24.     }  
  25.   
  26.     private void drawCircle(Canvas canvas) {  
  27.         float x = currentPoint.getX();  
  28.         float y = currentPoint.getY();  
  29.         canvas.drawCircle(x, y, RADIUS, mPaint);  
  30.     }  
  31.   
  32.     private void startAnimation() {  
  33.         Point startPoint = new Point(RADIUS, RADIUS);  
  34.         Point endPoint = new Point(getWidth() - RADIUS, getHeight() - RADIUS);  
  35.         ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);  
  36.         anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
  37.             @Override  
  38.             public void onAnimationUpdate(ValueAnimator animation) {  
  39.                 currentPoint = (Point) animation.getAnimatedValue();  
  40.                 invalidate();  
  41.             }  
  42.         });  
  43.         anim.setDuration(5000);  
  44.         anim.start();  
  45.     }  
  46.   
  47. }  
画布上画画
然后通过invalidate进行回调机制
然后再在xml文件中加载这个class就ojbk了。

  1. public class MyAnimView extends View {  
  2.   
  3.     ...  
  4.   
  5.     private String color;  
  6.   
  7.     public String getColor() {  
  8.         return color;  
  9.     }  
  10.   
  11.     public void setColor(String color) {  
  12.         this.color = color;  
  13.         mPaint.setColor(Color.parseColor(color));  
  14.         invalidate();  
  15.     }  
  16.   
  17.     ...  
  18.   
  19. }  
颜色改变机制


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值