动画

补间动画:代码实现

//补间动画代码实现
    private void initAttribute() {
        //渐变
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.5f);
        alphaAnimation.setDuration(5000);


        TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
        translateAnimation.setDuration(5000);

        //缩放动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(5000);

        //旋转
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
        rotateAnimation.setDuration(5000);

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(translateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(rotateAnimation);

        mAttributeIv.startAnimation(animationSet);
    }

属性动画:代码实现

private void initTween() {
        Animator scaleXAnimator = ObjectAnimator.ofFloat(mAttributeIv, "scaleX", 1, 0.5f);
        scaleXAnimator.setDuration(2000);
        Animator scaleYAnimator = ObjectAnimator.ofFloat(mAttributeIv, "scaleY", 1, 0.5f);
        scaleYAnimator.setDuration(2000);
        Animator rotationXAnimator = ObjectAnimator.ofFloat(mAttributeIv, "rotationX", 0, 360);
        rotationXAnimator.setDuration(2000);
        Animator rotationYAnimator = ObjectAnimator.ofFloat(mAttributeIv, "rotationY", 0, 360);
        rotationYAnimator.setDuration(2000);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scaleXAnimator)
                .with(scaleYAnimator)
                .before(rotationXAnimator)
                .after(rotationYAnimator);
        animatorSet.start();
    }

帧动画:代码实现

private ImageView mFramIv;
private AnimationDrawable animationDrawable;

private void initView() {
        mFramIv = (ImageView) findViewById(R.id.iv_fram);
        initFram();
    }

private void initFram() {
        mFramIv.setBackgroundResource(R.drawable.select);
        animationDrawable = (AnimationDrawable) mFramIv.getBackground();
    }
 @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            animationDrawable.stop();
            animationDrawable.start();
            return true;
        }
        return super.onTouchEvent(event);
    }

属性动画:

//AnimatorSet通过before、with、after三个方法可以组合多个属性动画,with表示与给定动画同时执行,before在给定动画执行之前执行,after表示在给定动画执行之后执行.
//属性动画也可以在xml定义动画集

//个人理解:
before:在...之前
after:在...之后

一般指定只一次:如果定义第二次意思就会相反
private void initTween() {
        Animator alpha = ObjectAnimator.ofFloat(mTweenIv, "Alpha", 0.5f, 1);//透明度

        Animator rotationX = ObjectAnimator.ofFloat(mTweenIv, "RotationX", 0, 360);//旋转   倒数第一
        Animator rotationY = ObjectAnimator.ofFloat(mTweenIv, "RotationY", 0, 360);//旋转   倒数第二

        Animator translationX = ObjectAnimator.ofFloat(mTweenIv, "TranslationX", 200, 0);//平移X轴
        Animator translationY = ObjectAnimator.ofFloat(mTweenIv, "TranslationY", 0, 200);//平移Y轴

        Animator scaleX = ObjectAnimator.ofFloat(mTweenIv, "ScaleX", 0, 1);//缩放
        Animator scaleY = ObjectAnimator.ofFloat(mTweenIv, "ScaleY", 0, 1);//缩放

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(5000);//设置动画时间
//        animatorSet.playTogether(alpha, rotationX, rotationY, translationX, translationY, scaleX, scaleY);//动画合集
        animatorSet.play(alpha)
                .with(scaleX)//在...后
                .with(scaleY)
                .before(rotationX)
                .after(rotationY)
                .before(translationX)
                .after(translationY)
        ;
        animatorSet.start();//开始动画
    }

你可能感兴趣的:(动画)