Android动画分类(一)

来源:互联网 发布:php error log 不生成 编辑:程序博客网 时间:2024/05/16 09:58

一、官方分类,三种:视图动画(分为逐帧动画和补间动画)、属性动画(Android 3.0之后才添加)、Drawable 动画(逐帧动画的xml资源)


二、属性动画

一、逐帧动画(Frame)

逐帧是最容易理解的动画,它要求开发者把动画过程的每张静态图片都收集起来,然后由程序来控制依次显示这些静态图片,就是播放电影一样。 

逐帧动画与Drawable 动画结合使用:虽然我们可以在代码中定义动画资源,但通常我们使用xml的形式去定义。

如:

a、在资源中定义动画资源

在drawable文件夹下新建一个文件frame.xml,而不是anim文件夹

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"                android:oneshot="false">//oneshot:是否只播放一次    <item        android:drawable="@mipmap/lottery_1"        android:duration="200"/>//duration:这一帧持续的时间    <item        android:drawable="@mipmap/lottery_2"        android:duration="200"/>    <item        android:drawable="@mipmap/lottery_3"        android:duration="200"/></animation-list>
将ImageView作为动画载体

 <ImageView   android:id="@+id/iv"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_gravity="center"   android:layout_margin="10dp"   android:src="@drawable/frame" />

java代码中控制动画

imageView = (ImageView)findViewById(R.id.iv);AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();animationDrawable.start();animationDrawable.stop();

b、在代码中定义动画资源

AnimationDrawable anim = new AnimationDrawable();    for (int i = 1; i <= 6; i++) {    int id = getResources().getIdentifier("lottery_" + i, "mipmap", getPackageName());    Drawable drawable = getResources().getDrawable(id);    anim.addFrame(drawable, 200);//添加帧    }    anim.setOneShot(false);//设置为循环播放    imageView.setImageDrawable(anim);    anim.start();

二、补间动画(Tween)

补间动画就是我们只需要指定动画开始、动画结束“关键帧”和动画的持续时间就可以,而动画变化的“中间帧”由系统计算并补齐,并且动画执行后不能改变View响应事件的位置,View响应事件的位置还是在动画执行前的位置,不具备交互性。在xml中定义时是放在res/anim/文件夹里。

Animation代表抽象的动画类,它的实现类有如下几个:AlphaAnimation、ScaleAnimation、RotateAnimation、TranslateAnimation、AnimationSet。

动画类型xml配置方式Java实现方式透明度<alpha>AlphaAnimation大小缩放<scale>ScaleAnimation旋转<rotate>RotateAnimation位移变化<transtate>TranslateAnimation组合动画<set>AnimationSet

1、xml使用

<alpha>

<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="500"    android:fillAfter="false"    android:fromAlpha="1.0"    android:toAlpha="0.0" />
fillAfter:设置动画结束后是否保挂当前透明度

<scale>

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="500"    android:fromXScale="0.0"    android:fromYScale="0.0"    android:toXScale="1.5"    android:toYScale="1.5"    android:interpolator="@android:anim/decelerate_interpolator"    android:pivotX="50%"    android:pivotY="50%"    android:repeatCount="1"    android:repeatMode="reverse"    android:startOffset="0"    />
fromXScale:起始时X轴的缩放

toYScale:结束时Y轴的缩放

fromXDelta:起始时X座标

toYDelta:动画结束时Y的座标 

pivotX, pivotY 动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从自身中间开始

startOffset, 动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒 

repeatCount,动画重复的计数,动画将会执行该值+1次

repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变

<rotate>

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="500"    android:fromDegrees="0"    android:interpolator="@android:anim/accelerate_decelerate_interpolator"    android:pivotX="50%"    android:pivotY="50%"    android:toDegrees="-360" />
fromDegrees 动画开始时的角度
toDegrees 动画结束时物件的旋转角度,正代表顺时针
pivotX 动画相对于物件的X坐标的开始位置
pivotY 动画相对于物件的Y坐标的开始位置

<translate>

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="500"    android:fromXDelta="100"    android:fromYDelta="0"    android:interpolator="@android:anim/cycle_interpolator"    android:toXDelta="0"    android:toYDelta="0" />

<set>

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@[package:]anim/interpolator_resource"    android:shareInterpolator=["true" | "false"] >    <alpha        android:fromAlpha="float"        android:toAlpha="float" />    <scale        android:fromXScale="float"        android:toXScale="float"        android:fromYScale="float"        android:toYScale="float"        android:pivotX="float"        android:pivotY="float" />    <translate        android:fromXDelta="float"        android:toXDelta="float"        android:fromYDelta="float"        android:toYDelta="float" />    <rotate        android:fromDegrees="float"        android:toDegrees="float"        android:pivotX="float"        android:pivotY="float" />    <set>        ...    </set></set>
interpolator:设置插值器

shareInterpolator:是否要将插值器应用到所有子元素

代码中将动画资源设置到View中并启动动画:

 Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha); imageView.startAnimation(alphaAnimation);

2、代码使用

下面以设置透明度为例子

Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);alphaAnimation.setDuration(500);//设置动画持续时间为500毫秒alphaAnimation.setFillAfter(false);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)imageView.startAnimation(alphaAnimation);

3、插值器就是控制动画的变化速率,使其能够以匀速变化,先加速后减速、加速变化等各种变化。

系统自带的几种插值器:
java代码xml解释AccelerateInterpolator@android:anim/accelerate_interpolator开始慢后面快DecelerateInterpolator@android:anim/decelerate_interpolator前面快后面慢AccelerateDecelerateInterolator@android:anim/accelerate_decelerate_interpolator先加速后减速AnticipateInterpolator@android:anim/anticipate_interpolator反向,先向相反方向改变一段再加速播放AnticipateOvershootInterpolator@android:anim/anticipate_overshoot_interpolator 反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值CycleIinterpolator@android:anim/cycle_interpolator循环,动画循环一定次数,值的改变为一正弦函数LinearInterpolator@android:amin/linear_interpolator线性,线性均匀改变OvershootInterpolator@android:overshoot_interpolator超越,最后超出目的值然后缓慢改变到目的值

参考文章:

http://www.cnblogs.com/whoislcj/p/5730520.html

0 0
原创粉丝点击