属性动画

来源:互联网 发布:服装成本统计软件 编辑:程序博客网 时间:2024/06/06 15:01

  ==========控件为imageview===============

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;

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

        imageView = findViewById(R.id.image_view);

    }

    public void trans(View view) {

        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "y", 0, 1300);

        animator.setDuration(3000);

        animator.setInterpolator(new AccelerateInterpolator());//加速的插值器
        animator.start();

        //监听事件
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                Toast.makeText(MainActivity.this,"结束,跳转页面",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }

    public void rotrate(View view) {

        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotationY", 0, 360);

        animator.setDuration(3000);
        animator.start();

    }

    public void alpha(View view) {

        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 0, 1);

        animator.setDuration(3000);
        animator.start();

    }

    public void scale(View view) {

        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "scaleX", 0, 2);

        animator.setDuration(3000);
        animator.start();
    }

    @SuppressLint("ObjectAnimatorBinding")
    public void zuhe_01(View view) {

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "abc", 0, 1);
        objectAnimator.setDuration(4000);

        objectAnimator.start();

        //动画的属性值不停更新的方法
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //属性的值
                float animatedValue = (float) valueAnimator.getAnimatedValue();

                imageView.setScaleX(animatedValue);
                imageView.setScaleY(animatedValue);
                imageView.setAlpha(animatedValue);

            }
        });


    }

    public void zuhe_02(View view) {

        ObjectAnimator y = ObjectAnimator.ofFloat(imageView, "y", 0, 10, 200, 250, 500, 1000, 1100, 1300);

        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 0.5f, 2);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 0.5f, 2);

        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1.0f);

        ObjectAnimator rotationX = ObjectAnimator.ofFloat(imageView, "rotationX", 0, 360);
        ObjectAnimator rotationY = ObjectAnimator.ofFloat(imageView, "rotationY", 0, 360);


        AnimatorSet animatorSet = new AnimatorSet();

        //设置
        /*animatorSet.play(y).before(scaleX);

        animatorSet.play(scaleX).with(scaleY);
        animatorSet.play(alpha).with(scaleY);

        animatorSet.play(rotationX).with(alpha).with(rotationY);*/

        animatorSet.play(y).with(scaleX).with(scaleY).with(alpha);

        animatorSet.setDuration(5000);
        animatorSet.start();

        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }
}
===============从屏幕上部经历 移动 透明 旋转 伸缩 移到屏幕下部 =================

/** 组合动画* *///纵向移动ObjectAnimator y = ObjectAnimator.ofFloat(img, "y", 0, 300);//缩放ObjectAnimator scaleY = ObjectAnimator.ofFloat(img, "scaleY", 2, 1);ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "scaleX", 2, 1);//透明ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", 0, 1); //旋转ObjectAnimator rotationX = ObjectAnimator.ofFloat(img, "rotationX", 0, 360);ObjectAnimator rotationY = ObjectAnimator.ofFloat(img, "rotationY", 0, 360);AnimatorSet animatorSet =new AnimatorSet();//进行设置  animatorSet.play(y).with(scaleX).with(scaleY).with(alpha).with(rotationX) ;//.with(rotationY)//设置时间animatorSet.setDuration(3000);animatorSet.start();//播放完成进行跳转animatorSet.addListener(new Animator.AnimatorListener() {    @Override    public void onAnimationStart(Animator animation) {    }    @Override    public void onAnimationEnd(Animator animation) {            // 跳转        Intent intent = new Intent(SplashActivity.this,XiangQingActivity.class);          startActivity(intent);        finish();    }    @Override    public void onAnimationCancel(Animator animation) {    }    @Override    public void onAnimationRepeat(Animator animation) {    }});