动画后设置背景视图出错。

7

我创建了一个继承自ViewCustomView。创建了一些CustomView并使用动画来移动和旋转它们。在更改backgroundResource后出现了错误,新的背景没有填满所有的CustomView。请查看以下代码:

    clearAnimation();
    AnimationSet animation = new AnimationSet(true);
    TranslateAnimation translateAnimation = new TranslateAnimation(0, destX - srcX, 0, destY - srcY);
    translateAnimation.setInterpolator(new LinearInterpolator());
    translateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
    animation.addAnimation(translateAnimation);

    final float finalX = destX;
    final float finalY = destY;

    animation.setAnimationListener(new AnimationListener() {

        public void onAnimationEnd(Animation arg0) {
            clearAnimation();
            setX(finalX);
            setY(finalY);

            RotateAnimation rotateAnimation = new RotateAnimation(0, degrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setInterpolator(new LinearInterpolator());
            rotateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
            rotateAnimation.setFillAfter(true);
            startAnimation(rotateAnimation);
            resetLayout();

            mMoveStatus = false;
            degree = degrees;

        }

        public void onAnimationRepeat(Animation arg0) {
        }

        public void onAnimationStart(Animation arg0) {
        }
    });


    startAnimation(animation);

并重新布局:

     lp = (LayoutParams) getLayoutParams();
    if (lp == null) {
        Log.d(TAG, "FIRST");
        lp = new LayoutParams((int) cardW, (int) cardH);
        lp.leftMargin = (int) mX;
        lp.topMargin = (int) mY;
    } else {
        Log.d(TAG, "LAST");
        lp.leftMargin = (int) mX;
        lp.topMargin = (int) mY;
    }
    setLayoutParams(lp);

请帮助我


1
错误?日志在哪里? - Egor
“backgroundResource”在哪里?请添加logcat错误。 - Sulfkain
3个回答

1
当您更改布局参数时,请确保还调用View.requestLayout()。但我认为您在这里不需要这样做。您只需要以下内容:
    AnimationSet animation = new AnimationSet(true);
    animation.setFillAfter(true);
    TranslateAnimation translateAnimation = new TranslateAnimation(0, destX - srcX, 0, destY - srcY);
    translateAnimation.setInterpolator(new LinearInterpolator());
    translateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
    animation.addAnimation(translateAnimation);
    RotateAnimation rotateAnimation = new RotateAnimation(0, degrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setInterpolator(new LinearInterpolator());
    rotateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
    rotateAnimation.setStartOffset((long) MGConstant.ANIMATE_DURATION);
    animation.addAnimation(rotateAnimation);
    startAnimation(animation);

1

使用ObjectAnimator来为视图添加动画效果,根据您的需求,它会在动画结束后保持视图不变,您不需要改变LayoutParams等内容。

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(customView,
            "translation", 0, destX - srcX, 0, destY - srcY);
objectAnimator.setDuration((long) MGConstantANIMATE_DURATION);

ObjectAnimator rotation = ObjectAnimator.ofFloat(customView,
            "rotation", 0, degree);
rotation .setDuration((long) MGConstantANIMATE_DURATION);

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration((long) MGConstantANIMATE_DURATION);
animatorSet.play(objectAnimator).after(rotation);

customView.setBackgroundResource(R.drawable.your_image);

1
使用下面的处理程序更改小部件属性。
 public void onAnimationEnd(Animation arg0) {
      new Handler.post(new Runnable(){
             clearAnimation();
             setX(finalX);
             setY(finalY);
             RotateAnimation rotateAnimation = new RotateAnimation(0, degrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
             rotateAnimation.setInterpolator(new LinearInterpolator());
             rotateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
             rotateAnimation.setFillAfter(true);
             startAnimation(rotateAnimation);
             resetLayout();

             mMoveStatus = false;
             degree = degrees;
         });

    }

这将解决您的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接