animator属性动画

package com.example.dell.animator;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = findViewById(R.id.img);
        findViewById(R.id.btn_alpha).setOnClickListener(this);
        findViewById(R.id.btn_rotate).setOnClickListener(this);
        findViewById(R.id.btn_scale).setOnClickListener(this);
        findViewById(R.id.btn_translate).setOnClickListener(this);
        findViewById(R.id.btn_set).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //透明度
            case R.id.btn_alpha:
                //参数一:指定要设置动画的图片   参数二:动画类型   参数三:动画执行参数
                ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});

                //设置动画时长
                alpha.setDuration(2000);

                //设置动画执行模式
                alpha.setRepeatMode(ObjectAnimator.RESTART);

                //设置动画再次执行的次数
                alpha.setRepeatCount(0);

                //开启动画
                alpha.start();
                break;

                //旋转
            case R.id.btn_rotate:
                ObjectAnimator rotationY = ObjectAnimator.ofFloat(img, "rotationY", new float[]{90f, 180f, 270f, 360f});

                rotationY.setDuration(2000);

                rotationY.setRepeatMode(ObjectAnimator.RESTART);

                rotationY.setRepeatCount(0);

                rotationY.start();
                break;

                //缩放
            case R.id.btn_scale:
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(img, "scaleY", new float[]{1.0f, 2.0f, 3.0f, 4.0f, 5.0f,1.0f});

                scaleY.setDuration(2000);

                scaleY.setRepeatMode(ObjectAnimator.RESTART);

                scaleY.setRepeatCount(0);

                scaleY.start();
                break;

                //位移
            case R.id.btn_translate:
                ObjectAnimator translationY = ObjectAnimator.ofFloat(img, "translationX", new float[]{1.0f, 100f, 200f, 300f, 400f,500f,600f,650f,1.0f});

                translationY.setDuration(2000);

                translationY.setRepeatMode(ObjectAnimator.RESTART);

                translationY.setRepeatCount(0);

                translationY.start();
                break;

                //组合动画
            case R.id.btn_set:
                //创建组合动画的对象
                AnimatorSet set = new AnimatorSet();

                ObjectAnimator rotationX = ObjectAnimator.ofFloat(img, "rotationX", new float[]{90f, 180f, 270f, 360f});

                rotationX.setDuration(2000);

                ObjectAnimator translationX = ObjectAnimator.ofFloat(img, "translationX", new float[]{1.0f, 100f, 200f, 300f, 400f,500f,600f,650f,1.0f});

                translationX.setDuration(2000);

                set.playTogether(rotationX,translationX);

                set.start();
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/gaoyiranblog/article/details/80434673