一、帧动画
- 帧动画就是一堆图片的集合
- 首先new一个资源文件
- 然后添加动画集合(图片不能太大不然不能成功加载)属性值为animation-list。
- 然后在控件中设置动画背景
- 在活动中获取控件背景(start和stop分别控制动画启动和停止)
- 效果如图所示:
二、补间动画
- 补间动画有四个操作属性:分别是透明度(alpha)、缩放(scale)、平移(translate)、旋转(rotate);
这些操作属性都要写在xml文件中:
首先要在res中new一个anim包,然后在包中new所需要的xml文件
透明度(alpha)
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"/>
缩放(scale)
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
平移(translate)
这里有个小bug,如果命名空间没写对不会报错,但是duration属性会无效
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
旋转(rotate)
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotY="50%"
android:pivotX="10%"
android:duration="2000"/>
组合(要用set属性框起来)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"/>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotY="50%"
android:pivotX="10%"
android:duration="2000"/>
<scale xmlns="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXDelta="320"
android:toYDelta="0" />
</set>
- 要使控件动起来只需要用 animationUtils这个动画工具类去调用它的函数loadanimation,从而获取到一个Animation对象,再用控件对象去启动动画把他传参进去。
Animation loadAnimation = AnimationUtils.loadAnimation(context, xml);
imageView.startAnimation(loadAnimation);
- 展示图片