Android View动画之——Tween动画

英文较好的可以移步官方介绍

Tween动画(补间动画),可以在一个容器(布局)内执行透明度变化,旋转,大小变化,位移等动画,它是通过ParentView来不断调整ChildView的不同坐标来实现的。


Animation属性

package:android.view.animation;
抽象类 Animation;

xml属性java方法解释
android:detachWallpapersetDetachWallpaper(boolean)是否在壁纸上运行
android:durationsetDuration(long)动画持续时间,毫秒为单位
android:fillAftersetFillAfter(boolean)控件动画结束时是否保持动画最后的状态
android:fillBeforesetFillBefore(boolean)控件动画结束时是否还原到开始动画前的状态
android:fillEnabledsetFillEnabled(boolean)与android:fillBefore效果相同
android:interpolatorsetInterpolator(Interpolator)设定插值器(指定的动画效果,譬如回弹等)
android:repeatCountsetRepeatCount(int)重复次数
android:repeatModesetRepeatMode(int)重复类型有两个值,reverse表示倒序回放,restart表示从头播放
android:startOffsetsetStartOffset(long)调用start函数之后等待开始运行的时间,单位为毫秒
android:zAdjustmentsetZAdjustment(int)表示被设置动画的内容运行时在Z轴上的位置(top/bottom/normal),默认为normal

抽象类 Animation的实现类

package:android.view.animation;

Java类动画
AlphaAnimation渐变透明度动画效果
RotateAnimation画面转移旋转动画效果
ScaleAnimation渐变尺寸伸缩动画效果
AnimationSet动画容器
TranslateAnimation画面转移位置移动动画效果

Animation的实现类每一个都具有Animation的属性。


分别看一下每种动画的属性:

Alpha属性

xml属性java方法解释
android:fromAlphaAlphaAnimation(float fromAlpha, …)动画开始的透明度(0.0到1.0,0.0是全透明,1.0是不透明)
android:toAlphaAlphaAnimation(…, float toAlpha)动画结束的透明度,同上

Rotate属性

xml属性java方法解释
android:fromDegreesRotateAnimation(float fromDegrees, …)旋转开始角度,正代表顺时针度数,负代表逆时针度数
android:toDegreesRotateAnimation(…, float toDegrees, …)旋转结束角度,正代表顺时针度数,负代表逆时针度数
android:pivotXRotateAnimation(…, float pivotX, …)缩放起点X坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%做为初始点、50%p表示以当前View的左上角加上父控件宽高的50%做为初始点)
android:pivotYRotateAnimation(…, float pivotY)缩放起点Y坐标,同上规律

Scale属性

xml属性java方法解释
android:fromXScaleScaleAnimation(float fromX, …)初始X轴缩放比例,1.0表示无变化
android:toXScaleScaleAnimation(…, float toX, …)结束X轴缩放比例
android:fromYScaleScaleAnimation(…, float fromY, …)初始Y轴缩放比例
android:toYScaleScaleAnimation(…, float toY, …)结束Y轴缩放比例
android:pivotXScaleAnimation(…, float pivotX, …)缩放起点X轴坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%做为初始点、50%p表示以当前View的左上角加上父控件宽高的50%做为初始点)
android:pivotYScaleAnimation(…, float pivotY)缩放起点Y轴坐标,同上规律

Translate属性

xml属性java方法解释
android:fromXDeltaTranslateAnimation(float fromXDelta, …)起始点X轴坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%做为初始点、50%p表示以当前View的左上角加上父控件宽高的50%做为初始点)
android:fromYDeltaTranslateAnimation(…, float fromYDelta, …)起始点Y轴从标,同上规律
android:toXDeltaTranslateAnimation(…, float toXDelta, …)结束点X轴坐标,同上规律
android:toYDeltaTranslateAnimation(…, float toYDelta)结束点Y轴坐标,同上规律

AnimationSet属性

动画容器,没有自己特有的属性,继承自Animation的属性对标签下所有生效

Tween动画使用方法

Java代码方式

使用示例:

//Alpha动画

ImageView imageView = (ImageView) findViewById(R.id.imageView);

Animation animation = new AlphaAnimation(0.1f, 1.0f); //fromAlpha 0.1f   toAlpha 1.0f

animation.setDuration(1000);//动画持续时间

animation.setRepeatCount(-1);// 设置动画的重复次数,-1表示无限重复,0表示不重复

animation.setRepeatMode(Animation.REVERSE);// 设置动画重复时的切换模式(RESTART表示重新执行,REVERSE表示动画反操作)

imageView.startAnimation(animation);//开始动画

//imageView.setAnimation(animation);
//animation.start();

其余几种使用方式大同小异,不同的构造方法传不同的参数。

//AnimationSet使用

ImageView imageView = (ImageView) findViewById(R.id.imageView);
AnimationSet animationSet = new AnimationSet(true);

AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //fromAlpha 0.1ftoAlpha 1.0f
alphaAnimation.setDuration(1000);

TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
translateAnimation.setDuration(1000);

animationSet.addAnimation(alphaAnimation);//添加Alpha动画
animationSet.addAnimation(translateAnimation);//添加Translate动画
imageView.startAnimation(animationSet);

xml方式

在res/anim目录下新建资源文件

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromAlpha="1.0"
    android:toAlpha="0.0">
</alpha>

在要使用的地方:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Animation animation = AnimationUtils.loadAnimation(this,R.anim.alpha);
imageView.setAnimation(animation);

其他的类似。

Interpolator插值器

public class
Interpolator
extends Object

Interpolator定义了动画变化的速率或规律
Interpolator接口的实现:

java类xml id值描述
AccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolator动画始末速率较慢,中间加速
AccelerateInterpolator@android:anim/accelerate_interpolator动画开始速率较慢,之后慢慢加速
AnticipateInterpolator@android:anim/anticipate_interpolator开始的时候从后向前甩
AnticipateOvershootInterpolator@android:anim/anticipate_overshoot_interpolator类似上面AnticipateInterpolator
BounceInterpolator@android:anim/bounce_interpolator动画结束时弹起
CycleInterpolator@android:anim/cycle_interpolator循环播放速率改变为正弦曲线
DecelerateInterpolator@android:anim/decelerate_interpolator动画开始快然后慢
LinearInterpolator@android:anim/linear_interpolator动画匀速改变
OvershootInterpolator@android:anim/overshoot_interpolator向前弹出一定值之后回到原来位置
PathInterpolator新增,定义路径坐标后按照路径坐标来跑
Interpolator使用

Java方式

AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //fromAlpha 0.1f   toAlpha 1.0f
alphaAnimation.setDuration(1000);
alphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());

//AnimationSet
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());

xml方式

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:shareInterpolator="true"
    android:fillAfter="true">
//也可以为单个动画设置
<alpha
    android:interpolator="@android:anim/anticipate_overshoot_interpolator"
    android:duration="3000"
    android:fromAlpha="1.0"
    android:toAlpha="0.0" />

动画监听

可以为每一个动画来注册一个监听,来监听动画的状态

AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //fromAlpha 0.1f   toAlpha 1.0f
alphaAnimation.setDuration(1000);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        //动画开始时
     }

    @Override
    public void onAnimationEnd(Animation animation) {
        //动画重复时
        }

    @Override
    public void onAnimationRepeat(Animation animation) {
        //动画结束时
        }
    });

自定义Tween动画

前面我们讲过View动画是通过ParentView来不断调整ChildView的不同坐标来实现的,动画的实现也就是通过变化像素矩阵的点来实现动画的,通过这个我们其实可以自己定制矩阵的变化来实现各种效果,像三维变换等。
在抽象类Animation中有一个重要的方法:

protected void applyTransformation(float interpolatedTime, Transformation t)

其实AlphaAnimation,RotateAnimation,ScaleAnimation|,TranslateAnimation都是通过定制这个方法来实现的。
系统通过不断调用这个方法来实现动画的变换,那我们要自己定制动画的话就通过定制 Transformation t 来告诉系统具体的变换方法。
float interpolatedTime 可以理解为动画的进度
Transformation中有两个属性:

    protected Matrix mMatrix;//矩阵
    protected float mAlpha;//透明度

ok,实现一个抛物线的动画看看效果。

public class ParabolaAnimation extends Animation {

    private final float mPara;

    /**
     * Creates a new animation with a duration of 0ms, the default interpolator, with
     * fillBefore set to true and fillAfter set to false
     */
    public ParabolaAnimation(float mPara) {
        this.mPara = mPara;
    }

    /**
     * Initialize this animation with the dimensions of the object being
     * animated as well as the objects parents. (This is to support animation
     * sizes being specified relative to these dimensions.)
     * <p>
     * <p>Objects that interpret Animations should call this method when
     * the sizes of the object being animated and its parent are known, and
     * before calling {@link #getTransformation}.
     *
     * @param width        Width of the object being animated
     * @param height       Height of the object being animated
     * @param parentWidth  Width of the animated object's parent
     * @param parentHeight Height of the animated object's parent
     */
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    /**
     * Helper for getTransformation. Subclasses should implement this to apply
     * their transforms given an interpolation value.  Implementations of this
     * method should always replace the specified Transformation or document
     * they are doing otherwise.
     *
     * @param interpolatedTime The value of the normalized time (0.0 to 1.0)
     *                         after it has been run through the interpolation function.
     * @param t                The Transformation object to fill in with the current
     */
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);

        float paraX = mPara * interpolatedTime;//横坐标随着时间匀速增加
        float paraY = paraX*paraX;//纵坐标为横坐标的平方
        final Matrix matrix = t.getMatrix();//拿到矩阵
        matrix.setTranslate(10*paraX,paraY);//调用矩阵的Translate方法
    }
}

使用:

final ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ParabolaAnimation parabolaAnimation = new ParabolaAnimation(imageView.getWidth());
                parabolaAnimation.setDuration(4000);
                imageView.startAnimation(parabolaAnimation);
            }
        });

看看效果:
抛物线

这只是一个简单的例子,更复杂一点的靠我们仔细去研究,但是方法都是差不多的。



感谢

http://blog.csdn.net/yanbober/article/details/46481171

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值