android的动画效果大体分三种,帧动画,补间动画,布局动画。
帧动画Frame:
帧动画的效果是最为简单的一种,类似我们手机相册的幻灯片播放。在res下建anim文件夹并创建一个xml。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/lu101" android:duration="1000"/>
<item android:drawable="@drawable/lu102" android:duration="1000"/>
<item android:drawable="@drawable/lu103" android:duration="1000"/>
<item android:drawable="@drawable/lu104" android:duration="1000"/>
<item android:drawable="@drawable/lu105" android:duration="1000"/>
<item android:drawable="@drawable/lu106" android:duration="1000"/>
<item android:drawable="@drawable/lu107" android:duration="1000"/>
<item android:drawable="@drawable/lu108" android:duration="1000"/>
<item android:drawable="@drawable/lu109" android:duration="1000"/>
<item android:drawable="@drawable/lu110" android:duration="1000"/>
</animation-list>
这里的每个item对应要显示的图片,duration控制每个多少毫秒播放下个图片,oneshot为true代表只播放一次,
false则为循环播放。
在布局中放一个ImageView的背景为@anim/你的资源文件,在java代码获取对象:
AnimationDrawable anim = (AnimationDrawable) image.getBackground();
补间动画主要控制缩放旋转等变化
AlphaAnimation:透明度变化,0-1为透明-非透明
ScaleAnimation:大小缩放
RotateAnimation:旋转
TranslateAnimation:位移
Interpolator:控制动画变化速度
LinearInterpolator:匀速
AccelerateInterpolator:由慢到快
DecelerateInterpolator:由快到慢
AccelerateDecelerateInterpolator:由快到慢再到快
和帧动画一样要建个xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000"/>//从透明到非透明,持续2s
<scale
android:fromXScale="0"
android:toXScale="1.00"x缩放无到正常大小
android:fromYScale="0"
android:toYScale="1"y缩放无到正常大小
android:pivotX="50%"初始x轴位置
android:pivotY="50%"初始y轴位置
android:duration="1000"/>
<rotate
android:fromDegrees="0"
android:toDegrees="720"0度到720度,即旋转2圈
android:pivotX="50%"
android:pivotY="50%"位置
android:duration="3000"/>
<translate
android:fromXDelta="0"
android:toXDelta="50%"左上角x坐标位置从0到屏幕一半
android:fromYDelta="0"
android:toYDelta="50%"左上角y坐标位置从0到屏幕一半
android:duration="1000"
/>
</set>
java中AnimationUtils工具类获取对象,start方法开启补间动画
Animation anims = AnimationUtils.loadAnimation(this, R.anim.anim);
//动画结束后保留结束状态
anims.setFillAfter(true);
image.startAnimation(anims);
anims.setAnimationListener(new AnimationListener() {
//监听器
@Override
public void onAnimationStart(Animation animation) {
//动画开始
Toast.makeText(MainActivity.this, "start", 0).show();
}
@Override
public void onAnimationRepeat(Animation animation) {
//动画重复,可通过setRepeat和setRepeatMode设置
Toast.makeText(MainActivity.this, "repeat", 0).show();
}
@Override
public void onAnimationEnd(Animation animation) {
//动画结束
Toast.makeText(MainActivity.this, "end", 0).show();
}
帧动画Frame:
帧动画的效果是最为简单的一种,类似我们手机相册的幻灯片播放。在res下建anim文件夹并创建一个xml。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/lu101" android:duration="1000"/>
<item android:drawable="@drawable/lu102" android:duration="1000"/>
<item android:drawable="@drawable/lu103" android:duration="1000"/>
<item android:drawable="@drawable/lu104" android:duration="1000"/>
<item android:drawable="@drawable/lu105" android:duration="1000"/>
<item android:drawable="@drawable/lu106" android:duration="1000"/>
<item android:drawable="@drawable/lu107" android:duration="1000"/>
<item android:drawable="@drawable/lu108" android:duration="1000"/>
<item android:drawable="@drawable/lu109" android:duration="1000"/>
<item android:drawable="@drawable/lu110" android:duration="1000"/>
</animation-list>
这里的每个item对应要显示的图片,duration控制每个多少毫秒播放下个图片,oneshot为true代表只播放一次,
false则为循环播放。
在布局中放一个ImageView的背景为@anim/你的资源文件,在java代码获取对象:
AnimationDrawable anim = (AnimationDrawable) image.getBackground();
anim.start()和anim.stop()控制播放/停止,也可以用addFrame()动态添加图片,参数和上面item定义的一样
补间动画主要控制缩放旋转等变化
AlphaAnimation:透明度变化,0-1为透明-非透明
ScaleAnimation:大小缩放
RotateAnimation:旋转
TranslateAnimation:位移
Interpolator:控制动画变化速度
LinearInterpolator:匀速
AccelerateInterpolator:由慢到快
DecelerateInterpolator:由快到慢
AccelerateDecelerateInterpolator:由快到慢再到快
和帧动画一样要建个xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000"/>//从透明到非透明,持续2s
<scale
android:fromXScale="0"
android:toXScale="1.00"x缩放无到正常大小
android:fromYScale="0"
android:toYScale="1"y缩放无到正常大小
android:pivotX="50%"初始x轴位置
android:pivotY="50%"初始y轴位置
android:duration="1000"/>
<rotate
android:fromDegrees="0"
android:toDegrees="720"0度到720度,即旋转2圈
android:pivotX="50%"
android:pivotY="50%"位置
android:duration="3000"/>
<translate
android:fromXDelta="0"
android:toXDelta="50%"左上角x坐标位置从0到屏幕一半
android:fromYDelta="0"
android:toYDelta="50%"左上角y坐标位置从0到屏幕一半
android:duration="1000"
/>
</set>
java中AnimationUtils工具类获取对象,start方法开启补间动画
Animation anims = AnimationUtils.loadAnimation(this, R.anim.anim);
//动画结束后保留结束状态
anims.setFillAfter(true);
image.startAnimation(anims);
anims.setAnimationListener(new AnimationListener() {
//监听器
@Override
public void onAnimationStart(Animation animation) {
//动画开始
Toast.makeText(MainActivity.this, "start", 0).show();
}
@Override
public void onAnimationRepeat(Animation animation) {
//动画重复,可通过setRepeat和setRepeatMode设置
Toast.makeText(MainActivity.this, "repeat", 0).show();
}
@Override
public void onAnimationEnd(Animation animation) {
//动画结束
Toast.makeText(MainActivity.this, "end", 0).show();
}
});
还有一些组合的方式,比如activity跳到另一个activity,第一个activity退出要A动画,第二个进入要B动画
overridePendingTransition(R.anim.A,R.anim.B);
补间动画是可以自定义的,需要继承Animation,具体如何做大家可以自己研究下。
布局动画LayoutAnimation
例如我们要做一个listview并且每一个项都要执行一个相同的动画
LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,R.anim.x));
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);//设置变换顺序
listView.setLayoutAnimation(controller);
listView.startLayoutAnimation();
这样listview的项就会按指定的顺序做变换