Animations是Android的UI实现动态效果的API。
主要包含两类,一类是Tweened Animations, 一类是是frame-by-frame Animations。
第一类是对一个view的处理,而第二类是对一个drawable序列的处理。
这两类效果既可以在代码中实现,也可以在xml中实现。
这里介绍下在xml中的实现。
Tweened Animations提供了rotate(旋转)、alpha(淡入淡出)、translate(移动)、Scale(缩放)四种效果。
在new xml中RT选择Tween Animation,选择set新建。
rotate:
< set xmlns:android= "http://schemas.android.com/apk/res/android" >
<!-- 动画时间 -->
<!-- 开始角度 -->
<!-- 结束角度 -->
<!-- 逆时针旋转270度 -->
<rotate
android:duration= "2000"
android:fromDegrees= "0"
android:toDegrees= "270" />
</ set>
alpha:
< set xmlns:android= "http://schemas.android.com/apk/res/android" >
<!-- 动画时间 -->
<!-- 开始透明度 -->
<!-- 结束透明度 -->
<alpha
android:duration= "2000"
android:fromAlpha= "1"
android:toAlpha= "0.1" />
</ set>
scale:
< set xmlns:android= "http://schemas.android.com/apk/res/android" >
<!-- 动画时间 -->
<!-- x,y轴起始压缩 -->
<!-- x,y轴结束压缩 -->
<scale
android:duration= "2000"
android:fromXScale= "0"
android:fromYScale= "1"
android:toXScale= "1"
android:toYScale= "1" />
</ set>
translate:
< set xmlns:android= "http://schemas.android.com/apk/res/android" >
<!-- x,y轴起始坐标 -->
<!-- x,y轴结束坐标 -->
<!-- 动画时间 -->
<translate
android:fromXDelta= "0%"
android:toXDelta= "100%"
android:fromYDelta= "0%"
android:toYDelta= "100%"
android:duration= "2000" />
</ set>
在代码中引用:
View view = findViewById(R.id. img);
Animation animation = AnimationUtils.loadAnimation(MainActivity. this, R.anim. alpha);
view.setAnimation(animation);
frame-by-frame Animations,可以用于显示gif图,直接使用第三方库+gif图,难免会报OOM。
在new xml中RT选择Drawable,选择animation-list新建mylist。
< animation-list xmlns:android ="http://schemas.android.com/apk/res/android" >
<item android:duration = "40" android:drawable ="@drawable/icon_000" />
<item android:duration = "40" android:drawable ="@drawable/icon_001" />
<item android:duration = "40" android:drawable ="@drawable/icon_002" />
<item android:duration = "40" android:drawable ="@drawable/icon_003" />
<item android:duration = "40" android:drawable ="@drawable/icon_004" />
<!-- 只要有帧图,就还可以继续往下写 -->
</ animation-list>
引用:
ImageView img = (ImageView) findViewById(R.id.img);
img.setBackgroundResource(R.anim. mylist);
AnimationDrawable animationDrawable = (AnimationDrawable) img.getBackground();
animationDrawable.start();