本文介绍了自ScaleAnimation / applytransformation不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的自定义ScaleAnimation:

This is my custom 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;
    }

}

我打电话是这样的,得到扩大,并通过切换按钮隐藏:

I am calling it like this, to get a expand and hide it via a toggle button:

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

它隐藏在第一次尝试以正确的方式查看。但每次我想通过按钮来展开它,它不会发生。如果触摸过意见,动画开始。我有什么改变?先谢谢了。

It hides the View at the first attempt the right way. But everytime I want to expand it via the Button, it does not happen. If touch the over views, the animation starts. What do I have to change? Thanks in advance.

推荐答案

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

I had the same issue but invalidate didn't work in my case. This solved my issue:

dp.requestLayout();

这篇关于自ScaleAnimation / applytransformation不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 21:07