Tween动画
通过对View的内容进行一系列的图形变换(包括平移,缩放,旋转,改变透明度)来实现动画效果。动画效果的定义可以采用XML来做也可以采用编码来做。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 透明度改变效果:透明度从0到1,不可见到完全看清,用时7秒 -->
<alpha
android:fromAlpha="0"
android:toAlpha="1.0"
android:duration="7000" />
<!-- 平移动画效果:从(0,0)到(100,100),用时7秒 -->
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100"
android:toYDelta="100"
android:duration="7000"/>
<!-- 缩放效果:从原始大小放大到5倍,用时7秒 -->
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="5.0"
android:toYScale="5.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="7000" />
<!-- 旋转动画效果:起始角度为0,最终旋转到顺时针180度,以中心为旋转中心,用时7秒 -->
<rotate
android:fromDegrees="0"
android:toDegrees="180"
android:pivotX="50%"
android:pivotY="50%"
android:duration="7000" />
</set>
ImageView imageView = (ImageView) this.findViewById(R.id.imageview);
// Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);//使用alpha.xml文件生成一个透明度动画对象
// imageView.startAnimation(animation);//将生成的动画对象传递给ImageView
//
// Animation animation2 =AnimationUtils.loadAnimation(this, R.anim.translate);//用translate.xml文件生成平移动画对象
// imageView.startAnimation(animation2);
//
// Animation animation3 = AnimationUtils.loadAnimation(this, R.anim.scale);//用scale.xml文件生成缩放动画对象
// imageView.startAnimation(animation3);
// Animation animation4 = AnimationUtils.loadAnimation(this, R.anim.rotate);//用rotate.xml文件生成旋转动画对象
// imageView.startAnimation(animation4);
//使用编码来实现旋转效果
// Animation animation5 = new RotateAnimation(0, -7200, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// animation5.setDuration(5000);//旋转时间
Animation animation6 = AnimationUtils.loadAnimation(this, R.anim.mix);//混合动画效果
imageView.startAnimation(animation6);
Frame动画
即顺序播放事先做好的图像,跟放胶片电影类似。
1. 把准备好的图片放进res/drawable下
2. 在项目的res目录下创建文件夹anim,然后在anim文件夹下定义动画XML文件,名称可以自定义。也可以用编码方式定义动画效果。(使用AnimationDrawable类)
3. 为View 控件绑定动画效果。调用代表动画的AnimationDrawable的start()方法开始动画
frame.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="@drawable/girl_1" android:duration="500" />
<item android:drawable="@drawable/girl_2" android:duration="500" />
<item android:drawable="@drawable/girl_3" android:duration="500" />
<item android:drawable="@drawable/girl_4" android:duration="500" />
<item android:drawable="@drawable/girl_5" android:duration="500" />
<item android:drawable="@drawable/girl_6" android:duration="800" />
<item android:drawable="@drawable/girl_7" android:duration="800" />
<item android:drawable="@drawable/girl_8" android:duration="500" />
<item android:drawable="@drawable/girl_9" android:duration="500" />
<item android:drawable="@drawable/girl_10" android:duration="500" />
<item android:drawable="@drawable/girl_11" android:duration="500" />
</animation-list>
MainActivity.java
public class MainActivity extends Activity {
private AnimationDrawable animationDrawable ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView) this.findViewById(R.id.textView);
textView.setBackgroundResource(R.drawable.frame);
animationDrawable = (AnimationDrawable) textView.getBackground();
//animationDrawable.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){//触摸后播放
animationDrawable.start();
}
return super.onTouchEvent(event);
}
}