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

前言

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

分类

相关代码

  • 旋转动画
     /**
     * 背景光环旋转动效
     *
     * @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();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值