帧动画的使用需要在xml文件中指定每一帧所对应的图片
animation-list写法如下:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/b1" android:duration="200"/> <item android:drawable="@drawable/b2" android:duration="200"/> <item android:drawable="@drawable/b3" android:duration="200"/> <item android:drawable="@drawable/b4" android:duration="200"/> <item android:drawable="@drawable/b5" android:duration="200"/> <item android:drawable="@drawable/b6" android:duration="200"/> </animation-list>
播放帧动画的java代码如下:
iv.setImageResource(R.drawable.myanimationlist); AnimationDrawable animation = (AnimationDrawable) iv.getDrawable(); animation.start(); // 开始播放动画
旋转,平移,缩放,渐变这四种补间动画的写法很相似,统一列在下面:
public void rote(View v) { RotateAnimation rotateAnimation = new RotateAnimation(0, 180, iv.getWidth() / 2, iv.getHeight() / 2); rotateAnimation.setDuration(2000); iv.startAnimation(rotateAnimation); } public void translate(View v) { TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200); translateAnimation.setDuration(2000); iv.startAnimation(translateAnimation); } public void scale(View v) { ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 2, 0.5f, 2, iv.getWidth() / 2, iv.getHeight() / 2); scaleAnimation.setDuration(2000); iv.startAnimation(scaleAnimation); } public void alpha(View v) { AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); alphaAnimation.setDuration(2000); iv.startAnimation(alphaAnimation); }
补间动画同时只能有一个在执行,如果想让几种补间动画同时执行的话,可以使用animationSet:
public void together(View v) { AnimationSet animationSet = new AnimationSet(false); RotateAnimation rotateAnimation = new RotateAnimation(0, 180, iv.getWidth() / 2, iv.getHeight() / 2); rotateAnimation.setDuration(2000); TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200); translateAnimation.setDuration(2000); ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 2, 0.5f, 2, iv.getWidth() / 2, iv.getHeight() / 2); scaleAnimation.setDuration(2000); AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); alphaAnimation.setDuration(2000); animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(translateAnimation); animationSet.addAnimation(scaleAnimation); animationSet.addAnimation(alphaAnimation); iv.startAnimation(animationSet); }
这样几种补间动画就可以一起执行了
这篇先总结到这里,下篇总结动画监听事件以及页面切换动画.