2011-01-23 56 views
5

我有一个FrameLayout包含一个子类的SurfaceView和一个ImageView。我想在ImageView上做一个TranslateAnimation。我能做到这一点的唯一方法就是向FrameLayout添加一个空洞的View。否则,在动画过程中,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 

回答

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(新视图(this)),那么它会缩小为50 x 50,并且剪切动画。我假设addView(new View(this));将FrameLayout扩展到全屏。

+0

在'setContentView`调用中添加LayoutParams以填充父项不会改变任何内容。 – Bill 2011-01-23 16:34:22

2

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

1

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

相关问题