【Android -- 动画】Tween 动画

一、前言

Tween 动画是 Android 最早的动画框架,从 Android1.0 就开始提供。Tween 动画使用简单,功能也不强,支持代码和 xml 两种方式编写动画。

二、原理

通过确定开始的视图样式、结束的视图样式、中间动画变化过程由系统补全来确定一个动画。根据不同的效果,分为四种动画:

  • 平移动画(Translate)
  • 缩放动画(scale)
  • 旋转动画(rotate)
  • 透明度动画(alpha)

三、代码实现

在这里插入图片描述

1. 平移动画(Translate)

1. 动画效果 xml 文件,/res/anim/translate_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:startOffset ="1000"
    android:fillBefore = "true"
    android:fillAfter = "false"
    android:fillEnabled= "true"
    android:repeatMode= "restart"
    android:repeatCount = "0"
    android:fromXDelta="0"
    android:toXDelta="500"
    android:fromYDelta="0"
    android:toYDelta="500" />

2. 代码

public class MainActivity extends AppCompatActivity {
    private TextView mAnim;
    private Button mPlay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initView();
        initEvent();

        // 方式二:通过 TranslateAnimation 实现
        Animation animation = new TranslateAnimation(0,500,0,500);
        animation.setDuration(3000);
        mAnim.startAnimation(animation);
    }

    private void initView() {
        mAnim = findViewById(R.id.tv_anim);
        mPlay = findViewById(R.id.btn_play);
    }

    private void initEvent() {
        mPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 方式一:通过加载 xml 文件实现
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate_anim);
                mAnim.startAnimation(animation);
            }
        });
    }
}

2. 缩放动画(Scale)

1. 动画效果 xml 文件,/res/anim/scale_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:startOffset ="1000"
    android:fillBefore = "true"
    android:fillAfter = "false"
    android:fillEnabled= "true"
    android:repeatMode= "restart"
    android:repeatCount = "0"

    android:fromXScale="0.0"
    android:toXScale="2.0"
    android:fromYScale="0.0"
    android:toYScale="2.0"
    android:pivotX="50%"
    android:pivotY="50%"/>

2. 代码

public class MainActivity extends AppCompatActivity {
    private TextView mAnim;
    private Button mPlay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initView();
        initEvent();

        // 方式二:通过 ScaleAnimation 实现
        Animation animation = new ScaleAnimation(0,2,0,2,Animation.RELATIVE_TO_SELF,
                0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        animation.setDuration(3000);
        mAnim.startAnimation(animation);
    }

    private void initView() {
        mAnim = findViewById(R.id.tv_anim);
        mPlay = findViewById(R.id.btn_play);
    }

    private void initEvent() {
        mPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 方式一:通过加载 xml 文件实现
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_anim);
                mAnim.startAnimation(animation);
            }
        });
    }
}

3.旋转动画(Rotate)

1. 动画效果 xml 文件,/res/anim/rotate_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromDegrees="0"
    android:toDegrees="+360"
    android:repeatCount="10"
    android:repeatMode="restart"
    android:pivotX="50%"
    android:pivotY="50%"
    android:interpolator="@android:anim/overshoot_interpolator" />

2. 代码

public class MainActivity extends AppCompatActivity {
    private TextView mAnim;
    private Button mPlay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initView();
        initEvent();

        // 方式二:通过 RotateAnimation 实现
        Animation animation = new RotateAnimation(0,270,Animation.RELATIVE_TO_SELF,
                0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        animation.setDuration(3000);
        mAnim.startAnimation(animation);
    }

    private void initView() {
        mAnim = findViewById(R.id.tv_anim);
        mPlay = findViewById(R.id.btn_play);
    }

    private void initEvent() {
        mPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 方式一:通过加载 xml 文件实现
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate_anim);
                mAnim.startAnimation(animation);
            }
        });
    }
}

4.透明度动画(Alpha)

1. 动画效果 xml 文件,/res/anim/alpha_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:startOffset ="1000"
    android:fillBefore = "true"
    android:fillAfter = "false"
    android:fillEnabled= "true"
    android:repeatMode= "restart"
    android:repeatCount = "0"
    android:interpolator="@android:anim/overshoot_interpolator"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"/>

2. 代码

public class MainActivity extends AppCompatActivity {
    private TextView mAnim;
    private Button mPlay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initView();
        initEvent();

        // 方式二:通过 AlphaAnimation 实现
        Animation animation = new AlphaAnimation(1,0);
        animation.setDuration(3000);
        mAnim.startAnimation(animation);
    }

    private void initView() {
        mAnim = findViewById(R.id.tv_anim);
        mPlay = findViewById(R.id.btn_play);
    }

    private void initEvent() {
        mPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 方式一:通过加载 xml 文件实现
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha_anim);
                mAnim.startAnimation(animation);
            }
        });
    }
}

5. 组合动画

上面讲的都是单个动画效果,但实际中很多需求都需要同时使用平移、缩放、旋转 & 透明度4种动画,即组合动画。
1. 动画效果 xml 文件,/res/anim/combination_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:startOffset ="1000"
    android:fillBefore = "true"
    android:fillAfter = "false"
    android:fillEnabled= "true"
    android:repeatMode= "restart"
    android:repeatCount = "0"
    android:interpolator="@android:anim/overshoot_interpolator"
    >
    <!--设置旋转动画,语法同单个动画-->
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatMode="restart"
        android:repeatCount="infinite"
        />

    <!--设置平移动画,语法同单个动画-->
    <translate
        android:duration="10000"
        android:startOffset = "1000"
        android:fromXDelta="-50%p"
        android:fromYDelta="0"
        android:toXDelta="50%p"
        android:toYDelta="0" />

    <!--设置透明度动画,语法同单个动画-->
    <alpha
        android:startOffset="7000"
        android:duration="3000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

    <!--设置缩放动画,语法同单个动画-->
    <scale
        android:startOffset="4000"
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" />

</set>

2. 代码

public class MainActivity extends AppCompatActivity {
    private TextView mAnim;
    private Button mPlay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initView();
        initEvent();

        // 方式二:通过 AnimationSet 实现
        AnimationSet animation = new AnimationSet(true);
        animation.setRepeatMode(Animation.RESTART);
        animation.setRepeatCount(1);

        //子动画1: 旋转动画
        Animation rotate = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,
                0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotate.setDuration(1000);
        rotate.setRepeatMode(Animation.RESTART);
        rotate.setRepeatCount(Animation.INFINITE);

        // 子动画2:平移动画
        Animation translate = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, -0.5f,TranslateAnimation.RELATIVE_TO_PARENT,
                0.5f,TranslateAnimation.RELATIVE_TO_SELF,0,TranslateAnimation.RELATIVE_TO_SELF,0);
        translate.setDuration(10000);

        // 子动画3:透明度动画
        Animation alpha = new AlphaAnimation(1,0);
        alpha.setDuration(3000);
        alpha.setStartOffset(7000);

        // 子动画4:缩放动画
        Animation scale = new ScaleAnimation(1,0.5f,1,0.5f,Animation.RELATIVE_TO_SELF,
                0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        scale.setDuration(1000);
        scale.setStartOffset(4000);

        animation.addAnimation(rotate);
        animation.addAnimation(translate);
        animation.addAnimation(alpha);
        animation.addAnimation(scale);
        mAnim.startAnimation(animation);
    }

    private void initView() {
        mAnim = findViewById(R.id.tv_anim);
        mPlay = findViewById(R.id.btn_play);
    }

    private void initEvent() {
        mPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 方式一:通过加载 xml 文件实现
                Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.combination_anim);
                mAnim.startAnimation(animation);
            }
        });
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值