Android:如何更改我的ExpandAnimation类以正确处理动画?

时间:2012-07-05 09:12:41

标签: android android-animation

这是我从Udnic拍摄的动画类 - 感谢m8, 但我正在使用它“不是从关闭位置而是从打开”而且它是跳跃而且行为不正确。

我该怎么办?

    public class ExpandAnimation extends Animation {
    private View mAnimatedView;
    private LinearLayout.LayoutParams mViewLayoutParams;
    public int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;
    private ImageView mImageView;
    /**
     * 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, ImageView imageView) {
        setDuration(duration);
        mImageView = imageView;
        mAnimatedView = view;
        mViewLayoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();

        mMarginStart = mMarginEnd = 0;
        // decide to show or hide the view
        mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);
        //mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0);

        mMarginStart = mViewLayoutParams.bottomMargin;
        if(mMarginStart != 0)
            mMarginStart = 0 - view.getHeight();
        mMarginEnd = (mMarginStart == 0 ? (0 - view.getHeight()) : 0);

        view.setVisibility(View.VISIBLE);
        mImageView.setImageResource(R.drawable.expand_open);
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }


    @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.bottomMargin = 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.bottomMargin = mMarginEnd;
            mAnimatedView.requestLayout();

//          if (mIsVisibleAfter) {
            if(mMarginEnd != 0) {
                mAnimatedView.setVisibility(View.GONE);
                mImageView.setImageResource(R.drawable.expand_close);
            }
            mWasEndedAlready = true;
        }
    }
}

0 个答案:

没有答案