Android动画分为Tween动画和Frame动画,近期学习了,体tween动画,现在讲学习的心得以及相关知识介绍如下。
Tween又称为补间动画,可以把对象进行缩小、放大、旋转和渐变等操作。
第一: Tween动画四个主要实现类:
1、AlphaAnimation:渐变(颜色)动画,主要控制透明度变化动画类,常使用AlphaAnimation(float fromAlpha, float toAlpha)来构造;
fromAlpha:动画开始时的透明度(取值范围为0.0到1.0);
toAlpha:动画结束时的透明度;
2、ScaleAnimation:主要控制大小变化,常使用ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)来构造;
fromX:动画开始X坐标上的伸缩尺度(是相对于原来图片的大小而言);
toX:动画结束X坐标上的伸缩尺度;
fromY:动画开始Y坐标上的伸缩尺度;
toY:动画结束Y坐标上的伸缩尺度;
pivotXType:X坐标上的伸缩模式(类型),取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF:相对于自身, Animation.RELATIVE_TO_PARENT;
pivotXValue:X坐标上缩放的中心位置;
pivotYType:Y坐标上的伸缩模式,取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT;
pivotYValue:Y坐标上缩放的中心位置;
3、TranslateAnimation:主要控制位置变换的动画实现类,常使用TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)来构造;
fromXDelta:动画开始的X坐标;
toXDelta:动画结束的X坐标;
fromYDelta:动画开始的Y坐标;
toYDelta:动画结束的Y坐标;
4、RotateAnimation:主要控制旋转的动画实现类,常使用RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)来构造;
fromDegrees:旋转开始角度;
toDegrees:旋转结束角度;
pivotXType, pivotXValue, pivotYType, pivotYValue与尺度变化动画ScaleAnimation类似;
第二:所包含的共用的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//设置播放时间
animation.setDuration(
2000
);
//设置重复的次数,记着是重复的次数
animation.setRepeatCount(
2
);
//设置重复的模式,有两种RESTART:重新开始与REVERSE:反向开始
animation.setRepeatMode(AlphaAnimation.REVERSE);
//启动播放
iv.startAnimation(animation);
|
第三:实例,
布局文件我们这样写,
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
android:paddingBottom=
"@dimen/activity_vertical_margin"
android:paddingLeft=
"@dimen/activity_horizontal_margin"
android:paddingRight=
"@dimen/activity_horizontal_margin"
android:paddingTop=
"@dimen/activity_vertical_margin"
tools:context=
"com.ftf.tween.MainActivity"
>
<LinearLayout
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:orientation=
"horizontal"
>
<Button
android:onClick=
"click"
android:text=
"透明度"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:orientation=
"horizontal"
/>
<Button
android:onClick=
"click2"
android:text=
"缩放"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:orientation=
"horizontal"
/>
<Button
android:onClick=
"click3"
android:text=
"旋转"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:orientation=
"horizontal"
/>
<Button
android:onClick=
"click4"
android:text=
"平移"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:orientation=
"horizontal"
/>
<Button
android:onClick=
"click5"
android:text=
"组合"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:orientation=
"horizontal"
/>
</LinearLayout>
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:gravity=
"center"
>
<ImageView
android:id=
"@+id/iv"
android:src=
"@drawable/ic_launcher"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
</LinearLayout>
|
activity中,这样写:
// 透明度变化
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
public
void
click(View view) {
AlphaAnimation animation =
new
AlphaAnimation(
0
.0f,
1
.0f);
animation.setDuration(
2000
);
animation.setRepeatCount(
2
);
animation.setRepeatMode(AlphaAnimation.REVERSE);
iv.startAnimation(animation);
}
public
void
click2(View view) {
ScaleAnimation animation =
new
ScaleAnimation(
0
.2f,
2
.0f,
0
.2f,
2
.0f,
Animation.RELATIVE_TO_SELF,
0
.5f, Animation.RELATIVE_TO_SELF,
0
.5f);
animation.setDuration(
2000
);
animation.setRepeatCount(
2
);
animation.setRepeatMode(AlphaAnimation.REVERSE);
iv.startAnimation(animation);
}
public
void
click3(View view) {
RotateAnimation animation =
new
RotateAnimation(
0
,
360
,
Animation.RELATIVE_TO_SELF,
0
.5f, Animation.RELATIVE_TO_SELF,
0
.5f);
animation.setDuration(
2000
);
animation.setRepeatCount(
2
);
animation.setRepeatMode(AlphaAnimation.REVERSE);
iv.startAnimation(animation);
}
public
void
click4(View view) {
TranslateAnimation animation =
new
TranslateAnimation(
Animation.RELATIVE_TO_PARENT,
0
.2f,
Animation.RELATIVE_TO_PARENT,
1
.0f,
Animation.RELATIVE_TO_PARENT,
0
.2f,
Animation.RELATIVE_TO_PARENT,
1
.0f);
animation.setDuration(
2000
);
animation.setRepeatCount(
2
);
animation.setRepeatMode(AlphaAnimation.REVERSE);
iv.startAnimation(animation);
}
public
void
click5(View view) {
//设置混合模式,也即多重播放效果放在一起。
AnimationSet set =
new
AnimationSet(
false
);
TranslateAnimation pa =
new
TranslateAnimation(
Animation.RELATIVE_TO_PARENT,
0
.2f,
Animation.RELATIVE_TO_PARENT,
1
.0f,
Animation.RELATIVE_TO_PARENT,
0
.2f,
Animation.RELATIVE_TO_PARENT,
1
.0f);
pa.setDuration(
2000
);
pa.setRepeatCount(
2
);
pa.setRepeatMode(AlphaAnimation.REVERSE);
RotateAnimation ra =
new
RotateAnimation(
0
,
360
,
Animation.RELATIVE_TO_SELF,
0
.5f, Animation.RELATIVE_TO_SELF,
0
.5f);
ra.setDuration(
2000
);
ra.setRepeatCount(
2
);
ra.setRepeatMode(AlphaAnimation.REVERSE);
ScaleAnimation sa =
new
ScaleAnimation(
0
.2f,
2
.0f,
0
.2f,
2
.0f,
Animation.RELATIVE_TO_SELF,
0
.5f, Animation.RELATIVE_TO_SELF,
0
.5f);
sa.setDuration(
2000
);
sa.setRepeatCount(
2
);
sa.setRepeatMode(AlphaAnimation.REVERSE);
set.addAnimation(sa);
set.addAnimation(ra);
set.addAnimation(pa);
iv.startAnimation(set);
}
|