http://my.oschina.net/summerpxy/blog/181780
PropertyAnimation,属性动画,顾名思义就是利用对象的属性变化形成动画的效果。属性动画的类可以用Animator这个抽象类来表示,通常使用它的子类:AnimatorSet和ValueAnimator,同时ValueAnimator有两个子类分别是ObjectAniamtor和TimeAnimator。
定义属性动画的XML资源的时候通常可以是如下三个元素之一作为根元素:
该资源文件的定义如下:
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();
}
});
}
}
|
实现的效果: