我正在尝试在自定义视图中移动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
,则可绘制对象的边界会改变,onDraw
方法会被调用,但图像仍保持在屏幕上的原位置。以下构造函数将创建一个
BitmapDrawable
并重现此问题:public MyView() {
image = getResources().getDrawable(R.drawable.image);
image.setBounds(0, 0, 100, 100);
}
我该如何移动
BitmapDrawable
?