PropertyAnimation属性动画资源 ArgbEvaluator使用

http://my.oschina.net/summerpxy/blog/181780

PropertyAnimation,属性动画,顾名思义就是利用对象的属性变化形成动画的效果。属性动画的类可以用Animator这个抽象类来表示,通常使用它的子类:AnimatorSet和ValueAnimator,同时ValueAnimator有两个子类分别是ObjectAniamtor和TimeAnimator。

  定义属性动画的XML资源的时候通常可以是如下三个元素之一作为根元素:

   <set>元素:该资源元素代表的是AniamtorSet类,这个类可以包含<set>,<objectAniamtor>,<animator>三个子元素。

 <objectAnimator>元素:用于定义objectAniamtor类。

<animator>元素:用于定义ValueAnimator类。


该资源文件的定义如下:

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
< set  android:ordering = "[together|sequentially]" >
 
     < objectAnimator
 
            android:propertyName = "string"
            android:duration = "int"
            android:valueFrom = "float|int|color"
            android:valueTo = "float|int|color"
            android:startOffset = "int"
            android:repeatCount = "int"
            android:interpolator = ""
            android:repeatMode = "[reapeat|reverse]"
            android:valueType = "[intType|floatType]" />
   
     < animator 
           
            android:duration = "int"
            android:valueFrom = "float|int|color"
            android:valueTo = "float|int|color"
            android:startOffset = "int"
            android:repeatCount = "int"
            android:interpolator = ""
            android:repeatMode = "[reapeat|reverse]"
            android:valueType = "[intType|floatType]" />
 
     < set >
         ....
     </ set >
            
 
</ set >


属性文件通常保存在animator文件夹下面。


下面定义一个图片背景色渐变的demo,首先是定义的属性动画文件:

1
2
3
4
5
6
7
8
9
10
11
<? xml  version = "1.0"  encoding = "utf-8" ?>
< objectAnimator  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:duration = "5000"
     android:propertyName = "backgroundColor"
     android:repeatCount = "infinite"
     android:repeatMode = "reverse"
     android:valueFrom = "#ff8080"
     android:valueTo = "#8080ff"
     android:valueType = "intType"  >
 
</ objectAnimator >

MainActivity.java:

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
package  com.example.android_propertyanimation;
 
import  android.os.Bundle;
import  android.animation.AnimatorInflater;
import  android.animation.ArgbEvaluator;
import  android.animation.ObjectAnimator;
import  android.annotation.SuppressLint;
import  android.app.Activity;
import  android.view.Menu;
import  android.view.View;
import  android.view.View.OnClickListener;
import  android.widget.Button;
import  android.widget.ImageView;
 
public  class  MainActivity  extends  Activity {
 
     ImageView imageView =  null ;
 
     Button btn =  null ;
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         imageView = (ImageView)  this .findViewById(R.id.imageview);
 
         btn = (Button)  this .findViewById(R.id.btn);
 
         imageView.setImageResource(R.drawable.dog);
 
         btn.setOnClickListener( new  OnClickListener() {
 
             @SuppressLint ( "NewApi" )
             @Override
             public  void  onClick(View view) {
               //加载属性动画需要用到AnimatorInflater类
                 ObjectAnimator oa = (ObjectAnimator) AnimatorInflater
                         .loadAnimator(MainActivity. this , R.animator.objectdemo);
            //用于动画计算的需要,如果开始和结束的值不是基本类型的时候,这个方法是需要的。
                 oa.setEvaluator( new  ArgbEvaluator());
             
             //设置动画的设置目标
                       oa.setTarget(imageView);
 
                 oa.start();
 
             }
 
         });
     }
 
}

实现的效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值