ViewAnimator动画

最近研究动画时,发现一个比较好用的一个设置动画的ViewAnimator,感觉挺是实用的,记录下:

build.gradle中加入:

compile 'com.github.florent37:viewanimator:1.0.5'

简单的一些用法:

1.一个方法多个视图动画:

ViewAnimator
       .animate(image)//第一个动画
            .translationY(-1000, 0)//Y轴方向移动
            .alpha(0,1)//透明图
       .andAnimate(text)//加入第2个视图的动画
            .dp().translationX(-20, 0)//X轴方向移动-20dp,然后回到原来的位置
            .decelerate()//减速
            .duration(2000)//动画持续时间
       .thenAnimate(image)
            .scale(1f, 0.5f, 1f)//从1缩小至0.5再放大至1
            .accelerate()//加速
            .duration(1000)
       .start();

效果图:


如果不用ViewAnimator,使用最原始的动画属性设置,代码带长:

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
  ObjectAnimator.ofFloat(image,"translationY",-1000,0),
  ObjectAnimator.ofFloat(image,"alpha",0,1),
  ObjectAnimator.ofFloat(text,"translationX",-200,0)
);
animatorSet.setInterpolator(new DescelerateInterpolator());
animatorSet.setDuration(2000);
animatorSet.addListener(new AnimatorListenerAdapter(){
    @Override public void onAnimationEnd(Animator animation) {

      AnimatorSet animatorSet2 = new AnimatorSet();
      animatorSet2.playTogether(
          ObjectAnimator.ofFloat(image,"scaleX", 1f, 0.5f, 1f),
          ObjectAnimator.ofFloat(image,"scaleY", 1f, 0.5f, 1f)
      );
      animatorSet2.setInterpolator(new AccelerateInterpolator());
      animatorSet2.setDuration(1000);
      animatorSet2.start();

    }
});
animatorSet.start();

2.多个视图添加相同的动画:

ViewAnimator
       .animate(image,text)
       .scale(0,1)
       .start();

3.添加监听:

ViewAnimator
       .animate(image)
       .scale(0,1)
       .onStart(() -> {})//动画开始监听
       .onStop(() -> {})//动画结束监听
     .start();

4.设置宽高:

ViewAnimator
       .animate(view)
       .waitForHeight() //wait until a ViewTreeObserver notification
       .dp().width(100,200)
       .dp().height(50,100)
       .start();

5.设置颜色:

ViewAnimator
       .animate(view)
       .textColor(Color.BLACK,Color.GREEN)
       .backgroundColor(Color.WHITE,Color.BLACK)
       .start();

6.设置旋转动画:

ViewAnimator
       .animate(view)
       .rotation(360)
       .start();

7.自定义动画:

ViewAnimator
       .animate(text)
       .custom(new AnimationListener.Update<TextView>() {
            @Override public void update(TextView view, float value) {
                  view.setText(String.format("%.02f",value));
            }
        }, 0, 1)
       .start();

8.动画取消:

ViewAnimator viewAnimator = ViewAnimator
       .animate(view)
       .rotation(360)
       .start();

viewAnimator.cancel();

9.还有很多的动画属性,如下效果图:


详细可见,github地址:https://github.com/florent37/ViewAnimator

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值