7

我创建了一个CustomViewextends View。创建一些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);

请帮我

4

3 回答 3

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);
于 2014-03-06T17:16:26.640 回答
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;
         });

    }

这将解决您的问题。

于 2014-03-07T08:00:18.367 回答
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);
于 2014-03-06T16:33:00.410 回答