帧动画 FrameAnimation
- 帧动画其实就是多张图片连续播放实现动画效果
- 在drawable文件夹中定义XML帧动画文件(根节点为animation-list)
- 其中XML文件中的oneshot属性可选值为true或者false,指定是否循环播放,true表示只播放一次,false表示循环播放
先上图:
帧动画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/g1"
android:duration="200"/>
<item
android:drawable="@drawable/g2"
android:duration="200"/>
<item
android:drawable="@drawable/g3"
android:duration="200"/>
<item
android:drawable="@drawable/g4"
android:duration="200"/>
<item
android:drawable="@drawable/g5"
android:duration="200"/>
<item
android:drawable="@drawable/g6"
android:duration="200"/>
<item
android:drawable="@drawable/g7"
android:duration="200"/>
<item
android:drawable="@drawable/g8"
android:duration="200"/>
<item
android:drawable="@drawable/g9"
android:duration="200"/>
<item
android:drawable="@drawable/g10"
android:duration="200"/>
</animation-list>
在Activity中播放动画代码如下:
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setBackgroundResource(R.drawable.animation);
AnimationDrawable animationDrawable =(AnimationDrawable) iv.getBackground();
animationDrawable.start();
这里是直接在onCreate()中调用start()方法的,但是Google官方并不建议这么做,因为在onCreate()中,AnimationDrawable可能还没有完全的绑定到窗口中。如果想不和用户有任何交互就立即播放动画,Google推荐在onWindowFocusChanged()方法中进行帧动画的播放,即界面获得到焦点的时候会回调onWindowFocusChanged()方法。
补间动画 TweenAnimation
- 从初始状态到终止状态之间的一个转变过程
- 常用的四种动画效果:位移,旋转,缩放,透明度(Translate,Rotate, Scale, Alpha)
为ImageView对象设置位移动画
TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 100);
animation.setDuration(5000);
iv.startAnimation(animation);
TranslateAnimation构造方法说明:
/**
* @params fromXDelta 动画的起始x坐标,相对于ImageView自身位置的x偏移量
* @params toXDelta 动画的结束x坐标,相对于ImageView自身位置的x偏移量
* @params fromYDelta 动画的起始y左标,相对于ImageView自身位置的y偏移量
* @params toYDelta 动画的结束y坐标,相对于ImageView自身位置的y偏移量
*/
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
/**
* @params fromXType fromXValue值得参考,可选Animation.RELATIVE_TO_SELF,Animation.ABSOLUTE,和Animation.RELATIVE_TO_PARENT
* Animation.RELATIVE_TO_SELF 起始位置的x坐标为ImageView自身宽度的fromXValue倍+ImageView的自身x坐标
* Animation.ABSOLUTE fromXValue和fromYValue的值是绝对的,如同第一个构造方法效果相同
* Animation.RELATIVE_TO_PARENT (百分比)起始位置的x坐标为父控件的宽度的fromXValue倍+ImageView的自身x坐标
*
* @params fromYType 类似于fromXType
*/
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
/**
* TranslateAnimation从XML文件中加载资源
*/
TranslateAnimation(Context context, AttributeSet attrs)
本文详细介绍了帧动画和补间动画的概念及实现方法。帧动画通过连续播放多张图片来达到动画效果,可在XML文件中定义并控制循环播放。补间动画则通过对象状态的转变实现动画效果,包括位移、旋转、缩放和透明度变化。
2770

被折叠的 条评论
为什么被折叠?



