Why is the view animation sometimes cropped?

I have a FrameLayout containing subclasses of SurfaceView and ImageView. I want to do TranslateAnimation on ImageView. The only way to make it work is to add an empty view to FrameLayout. Otherwise, the ImageView gets cropped (to the borders of the ImageView position at the beginning of the animation) during the animation.

I am curious why the empty Sibling View allows 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
+5
source share
3 answers

... , FrameLayout , . , , , , ?

:

    FrameLayout frameLayout = new FrameLayout(this); 

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

, FrameLayout . addView ( (this)) , 50 x 50, . , addView ( (this)); FrameLayout .

+4

, , , , . SurfaceView , . .

+2

the trick set setClipChildren into a layout that closed the view.

+1
source

All Articles