Android动画之旋转,缩放,位移,渐变,ObjectAnimator以及ValueAnimator

前言

之前没有完整的看过动画,最近项目中用到,在此总结一下,当是做个记录吧。

分类

Android动画之旋转,缩放,位移,渐变,ObjectAnimator以及ValueAnimator_第1张图片

相关代码

  • 旋转动画
     /**
     * 背景光环旋转动效
     *
     * @param imageView 需要动效的控件
     * @return
     */
    public static Animation getAnimatorRotateAchievements(ImageView imageView) {
        Animation rotate = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();
        rotate.setInterpolator(lin); //设置插值器
        rotate.setDuration(20000);//设置动画持续周期
        rotate.setRepeatCount(-1);//设置重复次数
        rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        imageView.setAnimation(rotate);

        return rotate;
    }
  • 缩小动画
     /**
     * 退出缩小动效
     *
     * @param relativeLayout 需要动效的控件
     * @return
     */
    public static AnimatorSet getAnimatorNarrowAchievements(RelativeLayout relativeLayout) {
        final AnimatorSet animatorSetsuofang = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(relativeLayout, "scaleX", 1f, 0f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(relativeLayout, "scaleY", 1f, 0f);
        animatorSetsuofang.setDuration(TIME_EXIT_ACHIEVEMENT);
        animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
        animatorSetsuofang.play(scaleX).with(scaleY);
        return animatorSetsuofang;
    }
  • 位移动画
     /**
     * 从控件所在位置移动到控件的底部
     *
     * @return
     */
    public static TranslateAnimation moveToViewBottom() {
        TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
        mHiddenAction.setDuration(AnimationUtils.TIME_INTER_ACHIEVEMENT);
        return mHiddenAction;
    }
    /**
     * 从控件的底部移动到控件所在位置
     *
     * @return
     */
    public static TranslateAnimation moveToViewLocation() {
        TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mHiddenAction.setDuration(AnimationUtils.TIME_INTER_ACHIEVEMENT);
        return mHiddenAction;
    }
  • 渐变动画
     /**
     * 渐变动画
     *
     * @param relativeLayout
     * @return
     */
    public static ObjectAnimator getAlphaAnimator(RelativeLayout relativeLayout) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(relativeLayout, "alpha", 1, 0);
        animator.setDuration(TIME_EXIT_ACHIEVEMENT);
        animator.setInterpolator(new DecelerateInterpolator());
        return animator;
    }
  • ObjectAnimator动画
     /**
     * 组合动画:先缩小后放大
     *
     * @param imageView
     * @return
     */
    public static AnimatorSet getAnimatorSet(ImageView imageView) {
        //组合动画
        final AnimatorSet animatorSetsuofang = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.7f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 0.7f, 1f);
        animatorSetsuofang.setDuration(500);
        animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
        //两个动画同时开始
        animatorSetsuofang.play(scaleX).with(scaleY);
        return animatorSetsuofang;
    }
  • ValueAnimator动画
    private void initAnimation() {
        ValueAnimator myAnim = ValueAnimator.ofInt(0, 100, 0);
        myAnim.setInterpolator(new LinearInterpolator());
        myAnim.setDuration(2000);
        myAnim.setRepeatCount(-1);
        myAnim.addUpdateListener(animation -> {
            int value = (int) animation.getAnimatedValue();
              //value变化值在0-100-0之间

            }
        });
        myAnim.start();
    }

 

你可能感兴趣的:(Android)