android动画之帧动画(drawable animation)和补间动画(view animation)

来源:互联网 发布:adobeacrobat9pro mac 编辑:程序博客网 时间:2024/05/29 03:39
  • 帧动画
    drawable animation使用比较简单,而且支持市场上所以API版本,下面简单贴下代码。帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。

直接代码贴上
xml资源文件配置

<?xml version="1.0" encoding="utf-8"?><!--    根标签为animation-list    XML文件要放在/res/drawable目录下    其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画根标签下,通过item标签对动画中的每一个图片进行声明    android:duration 表示展示所用的该图片的时间长度 --><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="true">    <item        android:drawable="@mipmap/ic_launcher1"        android:duration="150"></item>    <item        android:drawable="@mipmap/ic_launcher2"        android:duration="150"></item>    <item        android:drawable="@mipmap/ic_launcher3"        android:duration="150"></item>    <item        android:drawable="@mipmap/ic_launcher4"        android:duration="150"></item></animation-list>

主要方法

/**     * android中的逐帧动画.     * 逐帧动画的原理很简单,跟电影的播放一样,一张张类似的图片不停的切换,当切换速度达到一定值时,     * 我们的视觉就会出现残影,残影的出现保证了视觉上变化的连续性,这时候图片的切换看在我们眼中就跟真实的一样了。     * 想使用逐帧动画:     * 第一步:需要在res/drawable文件夹下新建一个xml文件,该文件详细定义了动画播放时所用的图片、切换每张图片     * 所用的时间、是否为连续播放等等。(有些文章说,在res/anim文件夹下放置该文件,事实证明,会出错哦)     * 第二步:在代码中,将该动画布局文件,赋值给特定的图片展示控件,如本例子中的ImageView。     * 第三步:通过imageView.getBackGround()获取相应的AnimationDrawable对象,然后通过该对象的方法进行控制动画     *     * @param targetButton     */    public void test2(ImageView targetButton) {        // 获取AnimationDrawable对象        targetButton.setBackgroundResource(R.drawable.test);        AnimationDrawable animationDrawable = (AnimationDrawable) targetButton.getBackground();        // 动画是否正在运行        if (animationDrawable.isRunning()) {            //停止动画播放            animationDrawable.stop();        } else {            //开始或者继续动画播放            animationDrawable.start();        }    }

帧动画结束了,下面开始补间动画

  • 补间动画
    用xml资源文件实现,资源文件全部放入Android项目的res/anim/目录下。

目前Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类库中。
1.AlphaAnimation:透明度(alpha)渐变效果,对应标签。
2.TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应标签。
3.ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应标签。
4.RotateAnimation:旋转渐变,可以指定旋转的参考点,对应标签。
5.AnimationSet:组合渐变,支持组合多种渐变效果,对应标签。

重要的xml资源文件贴上

<alpha xmlns:android="http://schemas.android.com/apk/res/android"      android:interpolator="@android:anim/accelerate_decelerate_interpolator"      android:fromAlpha="1.0"      android:toAlpha="0.1"      android:duration="2000"/>   <!--    fromAlpha :起始透明度   toAlpha:结束透明度   1.0表示完全不透明   0.0表示完全透明    --> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android"      android:interpolator="@android:anim/accelerate_decelerate_interpolator"      android:fromDegrees="0"      android:toDegrees="360"      android:duration="1000"      android:repeatCount="1"      android:repeatMode="reverse"/>  <!--   fromDegrees:表示旋转的起始角度  toDegrees:表示旋转的结束角度  repeatCount:旋转的次数  默认值是0 代表旋转1次  如果值是repeatCount=4 旋转5次,值为-1或者infinite时,表示补间动画永不停止  repeatMode 设置重复的模式。默认是restart。当repeatCount的值大于0或者为infinite时才有效。  repeatCount=-1 或者infinite 循环了  还可以设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。   -->  
<scale xmlns:android="http://schemas.android.com/apk/res/android"      android:interpolator="@android:anim/accelerate_interpolator"      android:fromXScale="0.2"      android:toXScale="1.5"      android:fromYScale="0.2"      android:toYScale="1.5"      android:pivotX="50%"      android:pivotY="50%"      android:duration="2000"/>  <!--   fromXScale:表示沿着x轴缩放的起始比例  toXScale:表示沿着x轴缩放的结束比例  fromYScale:表示沿着y轴缩放的起始比例  toYScale:表示沿着y轴缩放的结束比例  图片中心点:    android:pivotX="50%"       android:pivotY="50%"   --> 
<translate xmlns:android="http://schemas.android.com/apk/res/android"      android:interpolator="@android:anim/accelerate_decelerate_interpolator"      android:fromXDelta="0"      android:toXDelta="320"      android:fromYDelta="0"      android:toYDelta="0"      android:duration="2000"/>   <!--     android:interpolator 动画的渲染器    1、accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速    2、decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速    3、accelerate_decelerate_interpolator(动画加速减速器)             中间位置分层:  使动画在开始的时候 最慢,然后逐渐加速                     使动画在开始的时候 最快,然后逐渐减速  结束的位置最慢   fromXDelta  动画起始位置的横坐标   toXDelta    动画起结束位置的横坐标   fromYDelta  动画起始位置的纵坐标   toYDelta   动画结束位置的纵坐标   duration 动画的持续时间   -->  
<set xmlns:android="http://schemas.android.com/apk/res/android"      android:interpolator="@android:anim/decelerate_interpolator"      android:shareInterpolator="true" >      <scale          android:duration="2000"          android:fromXScale="0.2"          android:fromYScale="0.2"          android:pivotX="50%"          android:pivotY="50%"          android:toXScale="1.5"          android:toYScale="1.5" />      <rotate          android:duration="1000"          android:fromDegrees="0"          android:repeatCount="1"          android:repeatMode="reverse"          android:toDegrees="360" />      <translate          android:duration="2000"          android:fromXDelta="0"          android:fromYDelta="0"          android:toXDelta="320"          android:toYDelta="0" />      <alpha          android:duration="2000"          android:fromAlpha="1.0"          android:toAlpha="0.1" />  </set>  

加载xml资源文件的Java代码,举个例子,其他类似

// 移动效果    public void translateImpl(View v) {        // XML文件        Animation animation = AnimationUtils.loadAnimation(this,                R.anim.translate_demo);        animation.setRepeatCount(Animation.INFINITE);//循环显示        v.startAnimation(animation);    }
public void setAll(View v) {        Animation animation = AnimationUtils.loadAnimation(this,                R.anim.set_demo);        v.startAnimation(animation);    }

在xml里面写资源文件,大家看代码就很清晰
但是要注意的一点是,例如平移开始点相对的锚点默认是view的左上角

用代码控制也很容易
java中对应的方法
setDuration 设置持续时间
startNow 立刻启动动画
start 启动动画
cancel 取消动画
setRepeatCount 设置重复次数
setFillEnabled 使能填充效果
setFillBefore 设置起始填充
setFillAfter 设置终止填充
setRepeatMode 设置重复模式
setStartOffset 设置启动时间

一:TranslateAnimation类:位置变化动画

TranslateAnimation类是Android系统中的位置变化动画类,用于控制View对象的位置变化,该类继承于Animation类。TranslateAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是TranslateAnimation构造方法。

【基本语法】public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

【参数说明】

fromXDelta:位置变化的起始点X坐标。

toXDelta:位置变化的结束点X坐标。

fromYDelta:位置变化的起始点Y坐标。

toYDelta:位置变化的结束点Y坐标。

二:RotateAnimation类:旋转变化动画类

RotateAnimation类是Android系统中的旋转变化动画类,用于控制View对象的旋转动作,该类继承于Animation类。RotateAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是RotateAnimation构造方法。

【基本语法】public RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

【参数说明】

fromDegrees:旋转的开始角度。

toDegrees:旋转的结束角度。

pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotXValue:X坐标的伸缩值。

pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotYValue:Y坐标的伸缩值。

三:ScaleAnimation类:尺寸变化动画

ScaleAnimation类是Android系统中的尺寸变化动画类,用于控制View对象的尺寸变化,该类继承于Animation类。ScaleAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是ScaleAnimation构造方法。
【基本语法】public ScaleAnimation (float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
【参数说明】

fromX:起始X坐标上的伸缩尺寸。

toX:结束X坐标上的伸缩尺寸。

fromY:起始Y坐标上的伸缩尺寸。

toY:结束Y坐标上的伸缩尺寸。

pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotXValue:X坐标的伸缩值。

pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

pivotYValue:Y坐标的伸缩值。

四:AlphaAnimation类:透明度变化动画类

AlphaAnimation类是Android系统中的透明度变化动画类,用于控制View对象的透明度变化,该类继承于Animation类。AlphaAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是AlphaAnimation构造方法。

【基本语法】public AlphaAnimation (float fromAlpha, float toAlpha)
【参数说明】

fromAlpha:开始时刻的透明度,取值范围0~1。

toAlpha:结束时刻的透明度,取值范围0~1。

五:AnimationSet类:动画集合类

AnimationSet类是Android系统中的动画集合类,用于控制View对象进行多个动作的组合,该类继承于Animation类。AnimationSet类中的很多方法都与Animation类一致,该类中最常用的方法便是addAnimation方法,该方法用于为动画集合对象添加动画对象。

大家了解了那几个类的使用就明白了,下面简单贴下Java代码

public void test4(View view) {        Animation translateAnimation = new TranslateAnimation(0, 300, 0, 300);//移动动画效果         translateAnimation.setDuration(3000); //设置动画持续时间         view.setAnimation(translateAnimation); //设置动画效果         translateAnimation.startNow(); //启动动画     }

大家重点要明白AlphaAnimation,TranslateAnimation,ScaleAnimation,RotateAnimation,AnimationSet这几个类的构造方法参数的含义
参考了两个写得比较好的博客这里附上链接
http://blog.csdn.net/sgx425021234/article/details/9195829
http://blog.csdn.net/heyuchang666/article/details/41478795
/**
* --------------
* 欢迎转载 | 转载请注明
* --------------
* 如果对你有帮助,请点击|顶|
* --------------
* 请保持谦逊 | 你会走的更远
* --------------
* @author css
* @github https://github.com/songsongbrother
* @blog http://blog.csdn.net/xiangxi101
*/

0 0
原创粉丝点击