2

这是我的自定义 ScaleAnimation:

public class MyScaler extends ScaleAnimation {

    private static final String TAG = "myScaler";

    private View mView;

    private LayoutParams mLayoutParams;

    private int oldMargin, newMargin;

    private boolean mVanishAfter = false;

    public MyScaler(float fromX, float toX, float fromY, float toY, int duration, View view,
            boolean vanishAfter) {
        super(fromX, toX, fromY, toY);
        setDuration(duration);
        setFillAfter(true);
        setFillBefore(true);
        setFillEnabled(true);
        mView=view;

        int height = 200; // fiktive hoehe

        mLayoutParams = (LayoutParams) view.getLayoutParams();
        oldMargin = toY<fromY ? mLayoutParams.bottomMargin : -214;
        Log.d(TAG, "old Margin "+oldMargin);
        newMargin = toY<fromY ? oldMargin-height : height;
        Log.d(TAG, "new Margin "+newMargin);

    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        Log.d(TAG, "apply transofrmation");
        if (interpolatedTime < 1.0f) {
            int newBottomMargin=(int)(newMargin*interpolatedTime) + oldMargin;
            mLayoutParams.setMargins(mLayoutParams.leftMargin, mLayoutParams.topMargin,
                    mLayoutParams.rightMargin, newBottomMargin);
            Log.d(TAG,"margin change "+newBottomMargin);
            mView.getParent().requestLayout();
        } else if (mVanishAfter) {
            mView.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }

}

我这样称呼它,以通过切换按钮进行扩展和隐藏:

toggle.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick first "+first);
            if(first){
                myScale = new MyScaler(1.0f, 1.0f, 0.0f, 1.0f, 500, dp, false);
                ((TextView) v).setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.expander_ic_minimized, 0);
            }
            else{
                myScale = new MyScaler(1.0f, 1.0f, 1.0f, 0.0f, 500, dp, false);
                ((TextView) v).setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.expander_ic_maximized, 0);
            }
            first=!first;
            myScale.setAnimationListener(new AnimationListener(){

                @Override
                public void onAnimationEnd(Animation animation) {
                    first= !first;
                    Log.d(TAG, "change First"+first);

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }

                @Override
                public void onAnimationStart(Animation animation) {
                    Log.d(TAG, "started ani");
                }

            });

            dp.startAnimation(myScale);
            View parent = (View)dp.getParent();
             parent.invalidate();
        }
    });

它在第一次尝试时以正确的方式隐藏了视图。但是每次我想通过按钮扩展它时,它都不会发生。如果触摸视图,动画就会开始。我必须改变什么?提前致谢。

4

3 回答 3

8

我有同样的问题,但在我的情况下无效。这解决了我的问题:

dp.requestLayout();
于 2013-07-23T09:23:05.620 回答
3

对于那些打电话invalidate()requestLayout()不工作的人,请尝试

((View) view.getParent()).invalidate()

如此处所述:https ://stackoverflow.com/a/15657563/752781

于 2014-09-04T15:12:42.250 回答
2

在该方法帮助我之后,在invalidate()可见对象上调用该方法。ViewstartAnimation(Animation)

于 2013-06-07T13:24:10.930 回答