为什么View动画有时会剪辑?

时间:2011-01-23 06:50:28

标签: android animation clipping

我有一个包含子类SurfaceView和ImageView的FrameLayout。我想在ImageView上做一个TranslateAnimation。我能使其工作的唯一方法是向FrameLayout添加一个空的View。否则,在动画期间,ImageView会被剪裁(到动画开始时ImageView位置的边界)。

我很好奇为什么空的兄弟View允许ImageView正确地制作动画。使其工作的行在下面的代码中标有注释。

public class Test5 extends Activity {
    private static final String TAG = "Test5";
    private MySurfaceView mMSV;
    private ImageView mRectView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mMSV = new MySurfaceView(this);

        mRectView = new ImageView(this);
        ShapeDrawable sd = new ShapeDrawable(new RectShape());
        sd.getPaint().setColor(Color.RED);
        sd.setIntrinsicWidth(300);
        sd.setIntrinsicHeight(100);
        mRectView.setImageDrawable(sd);

        FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.addView(mMSV);
        frameLayout.addView(mRectView, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT));

        // I don't know why the following line prevents clipping during
        // animation. It simply adds a vacuous View.
        frameLayout.addView(new View(this));

        setContentView(frameLayout);
    }  // onCreate

    public class MySurfaceView extends SurfaceView implements
            SurfaceHolder.Callback {

        public MySurfaceView(Context context) {
            super(context);
            getHolder().addCallback(this);
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Canvas can = mMSV.getHolder().lockCanvas();
            can.drawColor(Color.GRAY);
            mMSV.getHolder().unlockCanvasAndPost(can);
        }

        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
                int arg3) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_UP) {
                Log.v(TAG, String.format("ACTION_UP, w=%d, h=%d", mRectView
                        .getWidth(), mRectView.getHeight()));
                Animation an = new TranslateAnimation(0f, 200f, 0f, 200f);
                // Animation an = new RotateAnimation(0f, 90f);
                an.setStartOffset(200);
                an.setDuration(1000);
                mRectView.startAnimation(an);
            }
            return true;
        }
    } // MySurfaceView
}  // Test5

3 个答案:

答案 0 :(得分:4)

这很有趣......我想在添加空白视图时会更改FrameLayout的大小。您的框架布局没有填充父级,我想知道您是否更改布局参数以填充父级,会发生什么?

我将您的代码简化为:

    FrameLayout frameLayout = new FrameLayout(this); 

    frameLayout.addView(mMSV); 
    frameLayout.addView(mRectView, 50, 50); 
    frameLayout.addView(new View(this)); //expands frame layout

看起来FrameLayout大小本身等于最后添加的子视图。如果在添加矩形之前设置addView(new View(this)),则会减小到50 x 50并剪切动画。我假设addView(new View(this));将FrameLayout扩展为全屏。

答案 1 :(得分:2)

我不知道你是怎么想出来的,但它似乎也适合我。我刚刚切换到使用SurfaceView,并注意到动画被剪裁了。添加空视图会停止裁剪。

答案 2 :(得分:1)

诀窍是将setClipChildren设置为 封闭视图的布局。

相关问题