gpt4 book ai didi

android - 为什么 View 动画有时会被剪裁?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:28:48 26 4
gpt4 key购买 nike

我有一个包含子类 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

最佳答案

这很有趣...我猜想添加空 View 时会更改 FrameLayout 的大小。你的frame布局没有填满parent,不知道你把layout params改成fill parent,会怎么样?

我将您的代码简化为:

    FrameLayout frameLayout = new FrameLayout(this); 

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

似乎 FrameLayout 大小本身等于最后添加的 subview 。如果您在添加矩形之前设置 addView(new View(this)) ,那么它会减小到 50 x 50 并且动画被剪裁。我假设 addView(new View(this));将 FrameLayout 扩展到全屏。

关于android - 为什么 View 动画有时会被剪裁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4772663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com