Android动画之补间动画

本文详细介绍了Android中补间动画的使用方法,包括透明动画、位移动画、旋转动画、缩放动画等,并展示了如何使用动画插入器使动画效果更丰富,以及如何通过AnimationSet进行动画组合。

理解:指定一个开始的位置,再指定一个结束的位置,自动补充中间的变化过程
为了更好的演示,写了一个Demo,xml界面如下(最后有源码)另一种:(Android动画之补间动画(XML 形式)
在这里插入图片描述

要介绍的有:
1.透明动画:AlphaAnimation
2.位移动画:TranslateAnimation
3.各种动画插入器
4.旋转动画:RotateAnimation
5.缩放动画:ScaleAnimation
6.组合显示:AnimationSet(动画集合容器)

1.透明动画:AlphaAnimation是一个实现透明动画的API,参数:制定透密度从多少到多少(范围0-1),0代表透明
在这里插入图片描述
2.位移动画:TranslateAnimation:四个参数:X轴开始位置,X轴结束位置,Y轴开始位置,Y轴结束位置
在这里插入图片描述
3.各种动画插入器:改变中间的补充过程(变得更有趣!!!,可以不加)

 //AccelerateDecelerateInterpolator	:其变化开始和结束速率较慢,中间加速
                //AccelerateInterpolator	:加速,开始速度慢,后面加速
                //DecelerateInterpolator	:减速,开始速度快,后面减速
                //LinearInterpolator	:线性(线性均匀变化)	其变化速率恒定
                //AnticipateInterpolator:反向	先向相反的方向改变一段距离后,再加速
                //AnticipateOvershootInterpolator:反向加超越:开始向后甩,然后向前甩,过冲到目标值,最后又回到了终值
                //OvershootInterpolator	:超越,其变化开始向前甩,过冲到目标值,最后又回到了终值
                //BounceInterpolator	:跳跃	其变化在结束时反弹
                //CycleInterpolator	 :	循环播放,其速率为正弦曲线
                //TimeInterpolator	 :	一个接口,可以自定义插值器

4.旋转动画:RotateAnimation:参数,开始时角度,结束时角度,旋转中心的横坐标,旋转中心的纵坐标在这里插入图片描述
5.缩放动画:ScaleAnimation,参数:x开始的比例值,x结束的比例值,Y开始的比例值,Y结束的比例值,后面两个参数为缩放中心的坐标点
在这里插入图片描述
6.组合显示:AnimationSet(动画集合容器)在这里插入图片描述
源码如下:
activity_second.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_alpha"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="透明动画" />

        <Button
            android:id="@+id/btn_translate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="位移动画" />

        <Button
            android:id="@+id/btn_rotate"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="旋转动画" />

        <Button
            android:id="@+id/btn_scale"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="缩放动画" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="groupshow"
        android:text="组合显示" />
</RelativeLayout>

SecondActivity.java文件

//补间动画
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_alpha;
    private Button btn_translate;
    private Button btn_rotate;
    private Button btn_scale;
    private ImageView iv_show;
    Animation animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initView();
    }

    private void initView() {
        btn_alpha = (Button) findViewById(R.id.btn_alpha);
        btn_translate = (Button) findViewById(R.id.btn_translate);
        btn_rotate = (Button) findViewById(R.id.btn_rotate);
        btn_scale = (Button) findViewById(R.id.btn_scale);

        btn_alpha.setOnClickListener(this);
        btn_translate.setOnClickListener(this);
        btn_rotate.setOnClickListener(this);
        btn_scale.setOnClickListener(this);
        iv_show = (ImageView) findViewById(R.id.iv_show);
        iv_show.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_alpha://透明动画
                //AlphaAnimation是一个实现透明动画的API,参数:制定透密度从多少到多少(范围0-1),0代表透明
                animation = new AlphaAnimation(1f, 0);
                break;
            case R.id.btn_translate://位移动画
                //TranslateAnimation:四个参数:X轴开始位置,X轴结束位置,Y轴开始位置,Y轴结束位置
                //指定一个开始的位置,再指定一个结束的位置,自动补充中间的变化过程
                //中间的变化过程又分为各种动画插入器
                //AccelerateDecelerateInterpolator	:其变化开始和结束速率较慢,中间加速
                //AccelerateInterpolator	:加速,开始速度慢,后面加速
                //DecelerateInterpolator	:减速,开始速度快,后面减速
                //LinearInterpolator	:线性(线性均匀变化)	其变化速率恒定
                //AnticipateInterpolator:反向	先向相反的方向改变一段距离后,再加速
                //AnticipateOvershootInterpolator:反向加超越:开始向后甩,然后向前甩,过冲到目标值,最后又回到了终值
                //OvershootInterpolator	:超越,其变化开始向前甩,过冲到目标值,最后又回到了终值
                //BounceInterpolator	:跳跃	其变化在结束时反弹
                //CycleInterpolator	 :	循环播放,其速率为正弦曲线
                //TimeInterpolator	 :	一个接口,可以自定义插值器
                animation = new TranslateAnimation(0, -200f, 0, 200f);
                //指定一个插入器,来改变中间的补充过程(变得更有趣!!!,可以不加)
                animation.setInterpolator(new BounceInterpolator());
                break;
            case R.id.btn_rotate://旋转动画
                //顺时针旋转, RotateAnimation:参数,开始时角度,结束时角度,旋转中心的横坐标,旋转中心的纵坐标
                animation = new RotateAnimation(0, 180f, iv_show.getWidth(), iv_show.getHeight());
                //指定一个插入器,来改变中间的补充过程(变得更有趣!!!,可以不加)
                animation.setInterpolator(new BounceInterpolator());
                break;
            case R.id.btn_scale://缩放动画
                //以1为标准,比一小就变小,比1大就放大
                //ScaleAnimation参数:x开始的比例值,x结束的比例值,Y开始的比例值,Y结束的比例值,后面两个参数为缩放中心的坐标点
                animation = new ScaleAnimation(2f, 0.5f, 2f, 0.5f, iv_show.getWidth(), iv_show.getHeight());
                break;

        }

        //设置动画播放时间
        animation.setDuration(3000);
        //设置是否保存动画结束后的状态
        animation.setFillAfter(true);
        //不管点击那个动画,最终都要开启,开启动画
        iv_show.startAnimation(animation);
    }

    public void groupshow(View view) {//组合显示
        //分别创建要组合的动画
        RotateAnimation rotate = new RotateAnimation(0, 180f, iv_show.getWidth(), iv_show.getHeight());
        ScaleAnimation scale = new ScaleAnimation(2f, 0.5f, 2f, 0.5f, iv_show.getWidth(), iv_show.getHeight());
        //创建动画集合容器
        AnimationSet set = new AnimationSet(true);
        set.addAnimation(scale);
        set.addAnimation(rotate);
        //设置动画播放时间
        set.setDuration(3000);
        //设置是否保存动画结束后的状态
        set.setFillAfter(true);
        //设置次数
        set.setRepeatCount(2);
        //设置重复模式
        set.setRepeatMode(Animation.RESTART);
        iv_show.startAnimation(set);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值