Android动画-帧动画&补间动画

一、帧动画
1、先把素材添加到res/drawable目录下。
2、在drawable下建立以animation-list为标签的xml文件。
例如:xml文件中 android:duration表示一帧停留的时间。可以换算成FPS。

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/walk_1"
        android:duration="62" />
    <item
        android:drawable="@drawable/walk_2"
        android:duration="62" />
    <item
        android:drawable="@drawable/walk_3"
        android:duration="62" />
    <item
        android:drawable="@drawable/walk_4"
        android:duration="62" />
    <item
        android:drawable="@drawable/walk_5"
        android:duration="62" />
    <item
        android:drawable="@drawable/walk_6"
        android:duration="62" />
    <item
        android:drawable="@drawable/walk_7"
        android:duration="62" />
    <item
        android:drawable="@drawable/walk_1"
        android:duration="62" />
</animation-list>

然后在要显示控件中引用drawable下的xml文件。
3、在java代码中启动这个帧动画。

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

PS:

//得到屏幕宽高以及控件宽高
    public int[] getScreenWH(){
        WindowManager windowManager = getWindowManager();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        int width = displayMetrics.widthPixels;
        int hight = displayMetrics.heightPixels;
        return new int[]{width, hight};
    }

二、视图动画(补间动画)
可以通过XML文件老描述动画过程,也可以用代码实现。推荐使用代码。
1、透明度AlphaAnimation

public class MainActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textview);

        AlphaAnimation aa = new AlphaAnimation(0, 1);
        aa.setDuration(7000);
        textView.startAnimation(aa);
    }
}

AlphaAnimation构造函数中的参数是指动画的透明度的渐变过程。
2、旋转动画

参数说明:
(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

float fromDegrees:旋转的开始角度。
float toDegrees:旋转的结束角度。
int pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
float pivotXValue:X坐标的伸缩值。
int pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
float pivotYValue:Y坐标的伸缩值。

       RotateAnimation rotateAnimation = new RotateAnimation(0.0f, 180.0f,
                Animation.RELATIVE_TO_SELF, 0.3f, Animation.RELATIVE_TO_SELF, 0.3f);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        textView.setAnimation(rotateAnimation);

其参数分别为旋转的起始位置角度和旋转中心的坐标。也可以通过设置参数来控制旋转动画的参考系。

 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);

3、位移动画
参数说明:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) ;

float fromXDelta 动画开始的点离当前View X坐标上的差值
float toXDelta 动画结束的点离当前View X坐标上的差值
float fromYDelta 动画开始的点离当前View Y坐标上的差值
float toYDelta 动画开始的点离当前View Y坐标上的差值

   TranslateAnimation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 200.0f,
                Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 300);
        translateAnimation.setDuration(3000);
        translateAnimation.setFillAfter(true);
        translateAnimation.setInterpolator(new LinearInterpolator());
        textView.setAnimation(translateAnimation );

4、缩放动画

参数说明:
ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

float fromX : 动画起始时 X坐标上的伸缩尺寸
float toX :动画结束时 X坐标上的伸缩尺寸
float fromY: 动画起始时Y坐标上的伸缩尺寸
float toY : 动画结束时Y坐标上的伸缩尺寸
int pivotXType: 动画在X轴相对于物件位置类型
float pivotXValue: 动画相对于物件的X坐标的开始位置
int pivotYType: 动画在Y轴相对于物件位置类型
float pivotYValue: 动画相对于物件的Y坐标的开始位置

 ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 0.5f, 0.0f, 0.5f,
                Animation.ABSOLUTE, 0.5f, Animation.ABSOLUTE, 0.5f);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setInterpolator(new BounceInterpolator());
        textView.setAnimation(scaleAnimation);

5、为动画设置监听事件

 animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值