首先在res中新建一个anim的文件夹
在anim中新建需要的动画xml资源文件(这里我把四个都写出来)
anim/alpha.xml(渐变动画)
1
2
3
4
5
6
7
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
alpha
android:duration
=
"2000"
android:fromAlpha
=
"0.1"
android:toAlpha
=
"1.0"
/>
</
set
><
span
></
span
>
|
duration:动画的持续时间(单位是毫秒)
fromAlpha:开始的透明度
toAlpha:结束的透明度
透明度0.0是完全透明,1.0是不透明
anim/scale(伸缩动画)
1
2
3
4
5
6
7
8
9
10
11
12
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
scale
android:duration
=
"2000"
android:fromXScale
=
"0.0"
android:fromYScale
=
"0.0"
android:interpolator
=
"@android:anim/accelerate_decelerate_interpolator"
android:pivotX
=
"00%"
android:pivotY
=
"00%"
android:toXScale
=
"1.4"
android:toYScale
=
"1.4"
/>
</
set
>
|
interpolator:动画插入器。
accelerate_decelerate_interpolator:加速-减速
accelerate_interpolator:加速
decelerate_interpolator:减速
fromXScale,fromYScale:开始X和Y的大小(0为缩小到没有)
toXScale,toYScale:结束X和Y的大小(1为没有变化)
pivotX,privotY:左上角的坐标(可以用百分比也可以用数字)
anim/translate(移动动画)
1
2
3
4
5
6
7
8
9
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
translate
android:duration
=
"2000"
android:fromXDelta
=
"30"
android:fromYDelta
=
"30"
android:toXDelta
=
"-80"
android:toYDelta
=
"300"
/>
</
set
>
|
toXDelta,toYDelta:结束时候左上角的坐标
anim/rotate(旋转动画的)
1
2
3
4
5
6
7
8
9
|
<
span
style
=
"font-size:9pt;line-height:1.5;"
><?
xml
version
=
"1.0"
encoding
=
"utf-8"
?></
span
> <
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
rotate
android:duration
=
"3000"
android:fromDegrees
=
"0"
android:interpolator
=
"@android:anim/accelerate_decelerate_interpolator"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:toDegrees
=
"+360"
/>
</
set
> <
span
></
span
><
span
></
span
>
|
interpolator:动画插入器。
accelerate_decelerate_interpolator:加速-减速
accelerate_interpolator:加速
decelerate_interpolator:减速
fromDegrees:开始的角度
toDegrees:结束的角度
pivotX,privotY:旋转中心的坐标(可以用百分比也可以用数字)
到此xml资源文件就完成了,接下来就是如何调用这些资源文件。
以alpha.xml为例子,先建立AlphaActivity
activity_alpha.xml
1
2
3
4
5
6
7
8
|
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:background
=
"@drawable/start_bg"
tools:context
=
".AlphaActivity"
>
</
RelativeLayout
>
|
AlphaActivity.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
|
package
com.example.animationdemo;
import
android.os.Bundle;
import
android.app.Activity;
import
android.view.View;
import
android.view.animation.Animation;
import
android.view.animation.Animation.AnimationListener;
import
android.view.animation.AnimationUtils;
import
android.widget.Toast;
public
class
AlphaActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
View view=View.inflate(
this
, R.layout.activity_alpha,
null
);
setContentView(view);
Animation animation=AnimationUtils.loadAnimation(
this
, R.anim.alpha);
view.startAnimation(animation);
animation.setAnimationListener(
new
AnimationListener() {
@Override
public
void
onAnimationStart(Animation arg0) {}
@Override
public
void
onAnimationRepeat(Animation arg0) {}
@Override
public
void
onAnimationEnd(Animation arg0) {
Toast.makeText(AlphaActivity.
this
,
"动画完成"
, Toast.LENGTH_SHORT).show();
}
});
}
}
|
1
2
|
View view=View.inflate(
this
, R.layout.activity_alpha,
null
);
setContentView(view);
|
否则动画是无法start的。之后我设置了动画监听器,可以加上自己喜欢的事件。
这里重点提一下android:pivotX和android:pivotY和android:fromXDelta,android:toXDelta
android:pivotX="50"使用绝对坐标
android:pivotX="50%"相对自己
android:pivotX="50%p"相对父控件