为什么视图动画有时会被剪裁?

发布于 2024-10-13 07:22:24 字数 2635 浏览 5 评论 0原文

我有一个包含子类 SurfaceView 和 ImageView 的 FrameLayout。我想在 ImageView 上执行 TranslateAnimation。我可以让它工作的唯一方法是向 FrameLayout 添加一个空视图。否则,在动画过程中 ImageView 会被剪裁(到动画开始时 ImageView 位置的边界)。

我很好奇为什么空的同级视图允许 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

I have a FrameLayout containing a subclassed SurfaceView and an ImageView. I want to do a TranslateAnimation on the ImageView. The only way I can make it work is to add a vacuous View to the FrameLayout. Otherwise, the ImageView gets clipped (to the bounds of the ImageView's position at the start of the animation) during the animation.

I'm curious as to why the empty sibling View allows the ImageView to animate correctly. The line that makes it work is marked with a comment in the code below.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷血 2024-10-20 07:22:24

这很有趣......我猜想当添加空视图时 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 扩展到全屏。

This is interesting... I guess that the size of the FrameLayout is changed when a vacuous view is added. Your frame layout does not fill the parent, I wonder if you change the layout params to fill parent, what would happen?

I simplified your code to this:

    FrameLayout frameLayout = new FrameLayout(this); 

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

It seems that the FrameLayout size itself equal to the last added child view. If you set addView(new View(this)) before adding a rectangle then it reduces to 50 x 50 and animation is clipped. I assume that addView(new View(this)); expands FrameLayout to the full screen.

夏花。依旧 2024-10-20 07:22:24

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

I don't know how you figured that out, but it seemed to work for me, too. I had just switched to using a SurfaceView, and noticed that the animations were getting clipped. Adding an empty View stopped the clipping.

苦行僧 2024-10-20 07:22:24

诀窍是将 setClipChildren 设置为
包含视图的布局。

the trick was setting setClipChildren to the
layout that enclosed the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文