补间动画的实现

补间动画的实现_第1张图片

1.activity_main.xml



    
    
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;
        }
    }
}


你可能感兴趣的:(Android)