search

Home  >  Q&A  >  body text

android ValueAnimator属性动画 onAnimationUpdate回调异常?

使用属性动画遇到异常以下是我的代码,点击一个View然后这个View执行一个高度变化的动画。但是动画没有按照预期出现。加上LOG以后发现动画的回调只调了两次,而且返回的值是最大值。各路大神求解!

 @Override
 public void onClick(View v) {
     switch (v.getId()) {
        case R.id.product_item_top_rl: 
        startAnimation(v);
        break;
     }
 }
 
private void startAnimation(final View v) {
    int height = v.getHeight();
    ValueAnimator va = ValueAnimator.ofInt(0, height);
    va.setDuration(1000);
    va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator animation) {
            Log.d(TAG, "animation : " + animation.getAnimatedValue());
            v.getLayoutParams().height = (Integer) animation.getAnimatedValue();
            v.requestLayout();
        }
    });
    va.start();
}

D/ChooseProductFragment: animation : 150
D/ChooseProductFragment: animation : 150

巴扎黑巴扎黑2770 days ago885

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 15:38:28

    Problem solved. It's because yesterday, in order to use espresso for UI automation testing, I turned off the three animations "Window Animation Scaling", "Excessive Animation Scaling", and "Animation Program Duration Adjustment" in the settings. Re-enable these animations and it will be normal.

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:38:28

    You did not specify the number and method of looping the animation, you should add

    //无限循环
    va.setRepeatCount(ValueAnimator.INFINITE);
    //从头开始动画
    va.setRepeatMode(ValueAnimator.RESTART);

    reply
    0
  • Cancelreply