【发布时间】:2011-03-11 08:33:38
【问题描述】:
我正在尝试在自定义视图中移动 BitmapDrawable。它可以与ShapeDrawable 一起正常工作,如下所示:
public class MyView extends View {
private Drawable image;
public MyView() {
image = new ShapeDrawable(new RectShape());
image.setBounds(0, 0, 100, 100);
((ShapeDrawable) image).getPaint().setColor(Color.BLACK);
}
@Override
protected void onDraw(Canvas canvas) {
image.draw(canvas);
}
public void move(int x, int y) {
Rect bounds = image.getBounds();
bounds.left += x;
bounds.right += x;
bounds.top += y;
bounds.bottom += y;
invalidate();
}
}
但是,如果我使用BitmapDrawable,drawable 的边界会发生变化,onDraw 方法会被调用,但图像会停留在屏幕上的位置。
以下构造函数将通过创建 BitmapDrawable 来重现问题:
public MyView() {
image = getResources().getDrawable(R.drawable.image);
image.setBounds(0, 0, 100, 100);
}
如何移动BitmapDrawable?
【问题讨论】:
标签: android animation drawable