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

そして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);

私を助けてください

4

3 に答える 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);
于 2014-03-06T16:33:00.410 に答える
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 に答える