Bil*_*ill 5 animation android clipping
我有一个FrameLayout包含一个子类SurfaceView和一个ImageView.我想在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
Run Code Online (Sandbox Code Playgroud)
这很有趣......我猜想当添加空视图时 FrameLayout 的大小会改变。你的框架布局没有填充父级,我想知道如果你改变布局参数来填充父级,会发生什么?
我将你的代码简化为:
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(mMSV);
frameLayout.addView(mRectView, 50, 50);
frameLayout.addView(new View(this)); //expands frame layout
Run Code Online (Sandbox Code Playgroud)
看起来 FrameLayout 大小本身等于最后添加的子视图。如果您在添加矩形之前设置 addView(new View(this)) ,那么它会减小到 50 x 50 并且动画会被剪裁。我假设 addView(new View(this)); 将 FrameLayout 扩展到全屏。
归档时间: |
|
查看次数: |
2302 次 |
最近记录: |