补间动画的实现

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="透明"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="缩放"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="旋转"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="平移"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="综合"
        android:layout_weight="1"/>
    </LinearLayout>

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

</LinearLayout>
2.MainActivity.java

package com.cwj.anim2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView iv;
    private Button btn1,btn2,btn3,btn4,btn5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = (ImageView) findViewById(R.id.iv);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
                //透明
                AlphaAnimation aAnim = new AlphaAnimation(0f,1f);
                aAnim.setDuration(2 * 1000);
                //动画完成后,是否保持
                aAnim.setFillAfter(true);
                iv.startAnimation(aAnim);
                break;
            case R.id.btn2:
                //缩放
                ScaleAnimation sAnim = new ScaleAnimation(1f,2f,1f,2f,
                        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                sAnim.setDuration(2 * 1000);
                //动画完成后,是否保持
                sAnim.setFillAfter(false);
                iv.startAnimation(sAnim);
                break;
            case R.id.btn3:
                //旋转
                RotateAnimation rAnim = new RotateAnimation(0,360,
                        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                rAnim.setDuration(2 * 1000);
                //动画完成后,是否保持
                rAnim.setFillAfter(true);
                iv.startAnimation(rAnim);
                break;
            case R.id.btn4:
                //平移
                TranslateAnimation tAnim = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,5f,
                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,5f
                );
                tAnim.setDuration(2 * 1000);
                //动画完成后,是否保持
                tAnim.setFillAfter(true);
                iv.startAnimation(tAnim);
                break;
            case R.id.btn5:
                //综合
                //缩放
                sAnim = new ScaleAnimation(1f, 2f, 1f, 2f,
                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                sAnim.setDuration(2 * 1000);
                //旋转
                rAnim = new RotateAnimation(0, 360,
                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                rAnim.setDuration(2 * 1000);
                //透明
                aAnim = new AlphaAnimation(0f, 1f);
                aAnim.setDuration(2 * 1000);

                AnimationSet animset = new AnimationSet(true);
                animset.addAnimation(sAnim);
                animset.addAnimation(rAnim);
                animset.addAnimation(aAnim);
                iv.startAnimation(animset);
                break;
            default:
                break;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

举儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值