Android 动画——帧动画与渐变动画

在Android动画只有三种基本动画,帧动画,渐变动画和属性动画。在Android 3.0版本之前只有帧动画和渐变动画,Android 3.0之后出现了属性动画。要在Android 3.0之前使用属性动画的话,需要依赖NineOldAndroids,NineOldAndroids在3.0之前的属性动画并不是真正的属性动画,而是用渐变动画来模拟。

帧动画

帧动画对应的类是AnimationDrawable,在这个类中开头有这么段注释,描述了帧动画的本质就是一帧一帧的显示图片。

An object used to create frame-by-frame animations, defined by a series of Drawable objects,which can be used as a View object’s background.

下面这段注释描述了帧动画的用法

The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the res/drawable/ folder, and set it as the background to a View object. Then, call
{@link #start()} to run the animation.

帧动画用法代码示例
先在res/drawable/文件夹下用根节点为animation-list的xml文件定义AnimationDrawable

<animation-list 
xmlns:android=“http://schemas.android.com/apk/res/androidandroid:oneshot=“false”>   
<item android:drawable=“drawable”android:duration=“int”/>
.
.
.
</animation-list>

再把它设为view的背景,然后调用AnimationDrawable的start()方法就可以运行帧动画了

AnimationDrawable animationDrawable = (AnimationDrawable)view.getBackground();
animationDrawable.start();

注意:帧动画的每幅图片不宜过大,否则容易造成OOM。

渐变动画

渐变动画对应的类是Animation,在这个类中开头有这么段注释,描述了渐变动画的作用对象。

Abstraction for an Animation that can be applied to Views, Surfaces, or other objects.

渐变动画只有四种基本的动画:

动画名称对应xml的根节点
TranslationAnimation(平移动画)translate
ScaleAnimation(变形动画)scale
AlphaAnimation(透明动画)alpha
RoateAnimation(旋转动画)roate



这四种渐变动画都继承与Animation。

渐变动画的用法

渐变动画可以通过xml来写,也可以通过代码直接写。

代码示例,以TranslationAnimation举例

通过xml实现

<?xml version="1.0" encoding="utf-8"?>
<translate 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="100%"//从距view本来宽度的100%位置
    android:toXDelta="0"//到距view本来宽度的0%位置
    android:fillAfter="true"//动画运行完后是否回到view本来的位置还是现在的位置
    android:duration="300"//动画持续时间
    android:interpolator="@android:interpolator/linear"//动画速率的变化
    android:repeatMode="restart"//是以什么方式重复动画,restart是按动画原来的方式重复,reserve是按动画的相反的方式重复
    android:repeatCount="1">//重复次数
</translate>

在代码中需要这样调用

TranslateAnimation animation = (TranslateAnimation)AnimationUtils.loadAnimation(this, R.anim.left_in);                
animImg.startAnimation(translateAnimation);//animImg是动画作用的对象

通过代码实现

TranslateAnimation translateAnimation = new TranslateAnimation(1f, 0f, 0f, 0f);
translateAnimation.setRepeatMode(Animation.RESTART);
translateAnimation.setRepeatCount(1);
translateAnimation.setFillAfter(true);
translateAnimation.setInterpolator(new LinearInterpolator());
translateAnimation.setDuration(300);
animImg.startAnimation(translateAnimation);                

渐变动画可以通过setAnimationListener(this)来监听动画的过程。


自定义渐变动画

自定义渐变动画需要实现两个方法initialize和applyTransformation,在applyTransformation方法有两个参数float interpolatedTine和Transformation t,可以通过Transformation来获取Matrix和Alpha,通过Matrix可以实现平移,变形,和旋转动画,透明动画就是不断改变Alpha值。在applyTransformation方法中也可以通过Camera来实现在Z轴上的动画。

代码示例

public class ViewAnimationZ extends Animation {
    private float centerX, centerY;
    private Camera camera = null;//android.graphics.Camera不是android.handware.Camera
    public ViewAnimationCamera() {
        camera = new Camera();
    }
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        centerX = width / 2.0f;
        centerY = height / 2.0f;
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        Log.d("ZGL", "interpolatedTime  ---------->   " + interpolatedTime);
        final Matrix matrix = t.getMatrix();
        camera.save();
        camera.rotate(0.0f, 45 * interpolatedTime, 0.0f);
        camera.translate((float)(centerX*(1 - Math.cos(Math.PI/180*30*interpolatedTime))), 0.0f, 300*interpolatedTime);
//        camera.translate(0.0f, 0.0f, 1300*(1.0f - interpolatedTime));
//        camera.rotateX(360 * interpolatedTime);
        camera.getMatrix(matrix);
        matrix.preTranslate(-centerX, -centerY);//这里用到了矩阵中的知识,这是前乘下面是后乘,用来确定动画的中心位置
        matrix.postTranslate(centerX, centerY);
        camera.restore();//camera的save()和restore()总是成对的出现
    }
}

在代码中运用

Animation animation = new ViewAnimationZ();
animation.setDuration(2500);
animation.setFillAfter(true);
animation.setInterpolator(new LinearInterpolator());
animImg.startAnimation(animation);

这篇只讲帧动画和渐变动画,下篇讲属性动画。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值