android暂停动画

    我们都知道android动画模式原来是有两种,分别是tween animation和frame animation;但是在android3.0之后android有引入了另外一种属性动画Value animation,在tween和frame动画中,我们一旦启动了动画就不能对其中间过程进行监控,比如我们需要暂停操作什么的就不是很方便,虽然他们有一个end方法,但是end方法并不是明确的暂停而是停止,所以我们可以用属性动画解决暂停动画的问题。

    属性动画的简单使用:ObjectAnimation继承了ValueAnimation

private ObjectAnimator animator;
        animator = ObjectAnimator.ofFloat(disk, "csdn", 0f, 360.0f);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(3000);
        animator.addUpdateListener(new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                /*if (isPause) {
                    if (endValue > 360f) {
                        endValue -= 360f;
                    }
                    animator.setFloatValues(endValue, 360f + endValue);
                } else {
                    disk.setRotation(endValue);
                    endValue = (Float) animation.getAnimatedValue();
                }*/
                endValue = (Float) animation.getAnimatedValue();
                disk.setRotation(endValue);
            }
        });
可以看到属性动画在用法上和其他两种动画是差不多的,关键就是他有addUpdateListener方法,在这个方法中可以通过animation.getAnimatedValue获取当前时间的值,通过这个值就可以对控件进行操作,当然要暂停的话就可以调用valueAnimation的pause方法,ObjectAnimation也是可以调用的,如果我们需要继续动画就可以调用resume方法了,怎么样和activity是差不多的吧!的却很方便。

    问题:但是不知道在有些情况下动画貌似会失效,就是在onAnimationUpdate(ValueAnimation animation)中调用animation.getAnimationValue方法的时候得到的不是我们期望的值,而是起始或者是结束的值,这是也不知道是为什么,有时在4.2的系统上是没有问题的,但是在4.4系统上就有问题了;希望有解决的朋友分享下,那为了解决这个问题怎么办呢?

    解决1:其实看了属性动画的代码,可以猜到他就是在一定的时间内对某个值进行改变,如果通过这个改变的值设置控件的属性值,所以我们可以自己定义某个值然后进行改变,例如上述代码中注释掉的部分:

/*if (isPause) {
                    if (endValue > 360f) {
                        endValue -= 360f;
                    }
                    animator.setFloatValues(endValue, 360f + endValue);
                } else {
                    disk.setRotation(endValue);
                    endValue = (Float) animation.getAnimatedValue();
                }*/
    解决2:通过handler模仿属性动画对控件进行属性的设置:

private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what==0){
                mCurTextXForHighLightLrc += (200 * endX) / duration;
                if (mCurTextXForHighLightLrc <= endX) {
                    mCurTextXForHighLightLrc = 0;
                    this.removeMessages(0);
                } else{
                    this.sendEmptyMessageDelayed(0, 200);
                    invalidate();
                }
            }
            
        };
    };



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值