6

これはとても奇妙です、私はこのアニメーションコードを持っています:

public class ExpandAnimation extends Animation {
private View mAnimatedView;
private MarginLayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mWasEndedAlready = false;

/**
* Initialize the animation
* @param view The layout we want to animate
* @param duration The duration of the animation, in ms
*/
    public ExpandAnimation(View view, int duration) {
        setDuration(duration);
        mAnimatedView = view;
        mViewLayoutParams = (MarginLayoutParams) view.getLayoutParams();

        mMarginStart = mViewLayoutParams.rightMargin;
        mMarginEnd = (mMarginStart == 0 ? (0- view.getWidth()) : 0);
        view.setVisibility(View.VISIBLE);
        mAnimatedView.requestLayout();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
            // Calculating the new bottom margin, and setting it
            mViewLayoutParams.rightMargin = mMarginStart
                    + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
            // Invalidating the layout, making us seeing the changes we made
            mAnimatedView.requestLayout();

        // Making sure we didn't run the ending before (it happens!)
        } else if (!mWasEndedAlready) {
            mViewLayoutParams.rightMargin = mMarginEnd;
            mAnimatedView.requestLayout();
            mWasEndedAlready = true;
        }
    }
}

そして私はこのアニメーションを使用します:

View parent = (View) v.getParent();
View containerMenu = parent.findViewById(R.id.containerMenu);
ExpandAnimation anim=new ExpandAnimation(containerMenu, 1000);
containerMenu.startAnimation(anim);

このアニメーションは、レイアウトの非表示/表示を切り替えます。

デフォルトでは、非表示になっています。クリックするとアニメーションが動作し、表示されます。もう一度クリックすると、正しく縮小します。しかし、3回目は何もしません。デバッグしたところ、コンストラクターが呼び出されているが、呼び出されていない applyTransformationことがわかりました。 どういうわけか、画面の周りのレイアウトをクリックすると、アニメーションが突然開始されます。

何か案が?

編集 applyTransformationがトリガーされるタイミングを知っている人はいますか?

4

1 に答える 1

10

理由はわかりませんが、クリックしたり、レイアウトを操作したりすると、ようやくアニメーションが始まります。そこで、プログラムで回避策を追加しました。レイアウトにスクロールビューがあるので、スクロール位置を移動します。

hscv.scrollTo(hscv.getScrollX()+1, hscv.getScrollY()+1);

この直後containerMenu.startAnimation(anim);

これはうまくいきます、理由がわかりません。

また、一部のアニメーションはandroid> 4で問題なく動作することがわかりましたが、たとえば2.3では、同じ問題が発生し、拡張と縮小が機能しましたが、2回目の拡張はできませんでした。

parent.invalidate();

トリックをしました。

于 2013-03-27T11:18:28.830 に答える