7

I create a CustomView extends View. Create some CustomView and using animation to move and rotate them. After change backgroundResource and error happened, new background not fill all CustomView. Please see code:

    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);

and resetLauyout:

     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);

Please help me

sphair
  • 1,674
  • 15
  • 29
BaDo
  • 540
  • 8
  • 19

3 Answers3

1

When you change things like layout parameteres, make sure you also call View.requestLayout(). I don't think you need this here though. All you need is the following:

    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);
Tas Morf
  • 3,065
  • 1
  • 12
  • 8
1

use ObjectAnimator for animate the views, with respect to your requirement, It maintain the view after animation end, you dont want to change the LayoutParams etc.

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);
balaji koduri
  • 1,321
  • 9
  • 25
1

change your widget properties by using handler has below

 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;
         });

    }

This will resolve your problem.

Sachin Shelke
  • 449
  • 4
  • 12