[AndroidUI]对Tween动画的简单封装

这个学期,老师要求我们要在两个月之内交一个app,并且采用scrum快速迭代的方式搞,而且下星期就要交一个可运行的"版本"了。因为是组队的方式,队友都指望着我,我可不能让他们失望啊!在讨论app中的某些功能时,大家的争议都挺大的,但是当我提议要在app中加入妹子(模型),没想到队友们毫不犹豫地同意了。。

在UI动画方面,我就打算再封装一下,让代码更加简洁一些。因为感觉平移旋转缩放透明度基本可以满足要求,所以这个类暂时就这样了!


package com.example.administrator.myapplication;

import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import java.util.List;

/**
 * Created by Administrator on 2015/9/18.
 * setFillBefore(false);//如果为true,表示动画结束时画面停留在此动画的第一帧
 * setFillAfter(true);//如果为true,表示动画结束时画面停留在此动画的最后一帧
 * setStartOffset;//系统默认当执行start方法后立刻执行动画,当使用该方法设置后,将延迟一定的时间再启动动画。
 * 屏幕的左上角是坐标系统原点,原点向右延伸是X轴正方向,原点向下延伸是Y轴正方向
 */
public class UITween {

    //默认动画结束时画面停留在此动画的最后一帧
    private static void defaultSetting(Animation anim,long duration,long delay)
    {
        anim.setDuration(duration * 1000);
        anim.setStartOffset(delay * 1000);
        anim.setFillEnabled(true);
        anim.setFillAfter(true);
    }

    public static TranslateAnimation getTranslateAnimation(View view,float toXValue,float toYValue,long duration,long delay)
    {
        float fromXValue = view.getTranslationX();
        float fromYValue = view.getTranslationY();
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,fromXValue,Animation.RELATIVE_TO_SELF,toXValue,
                Animation.RELATIVE_TO_SELF,fromYValue,Animation.RELATIVE_TO_SELF,toYValue);

        defaultSetting(ta, duration, delay);
        return  ta;
    }

    public static RotateAnimation getRotateAnimation(View view,float toDegree,long duration,long delay)
    {
        float fromDegree = view.getRotation();
        RotateAnimation ra = new RotateAnimation(fromDegree,toDegree,Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);

        defaultSetting(ra, duration, delay);
        return  ra;
    }

    public static ScaleAnimation getScaleAnimation(View view,float toXScale,float toYScale,long duration,long delay)
    {
        float fromXScale = view.getScaleX();
        float fromYScale = view.getScaleY();
        ScaleAnimation sa = new ScaleAnimation(fromXScale,toXScale,fromYScale,toYScale,Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);

        defaultSetting(sa, duration, delay);
        return  sa;
    }

    public static AlphaAnimation getAlphaAnimation(View view,float toAlpha,long duration,long delay)
    {
        float fromAlpha = view.getAlpha();
        AlphaAnimation aa = new AlphaAnimation(fromAlpha,toAlpha);

        defaultSetting(aa, duration, delay);
        return  aa;
    }

    public static AnimationSet getAnimationSet(Animation[] animArray)
    {
        AnimationSet as = new AnimationSet(false);
        for(int i = 0;i < animArray.length;i++)
        {
            as.addAnimation(animArray[i]);
        }
        as.setFillEnabled(true);
        as.setFillAfter(true);
        return as;
    }
}

后来发现Android中有一个叫“属性动画”的东西,比起上面的要更加强大,而且网上已经有不错的教程了:

http://blog.csdn.net/lmj623565791/article/details/38067475

http://blog.csdn.net/lmj623565791/article/details/38092093

你可能感兴趣的:(Tween)