I'm trying to move a BitmapDrawable
in a custom view. It works fine with a ShapeDrawable
as follows:
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();
}
}
However, if I use a BitmapDrawable
, the drawable's bounds change, the onDraw
method is called, but the image stays where it is on the screen.
The following constructor will reproduce the problem by creating a BitmapDrawable instead:
public MyView() {
image = getResources().getDrawable(R.drawable.image);
image.setBounds(0, 0, 100, 100);
}
How can I move a BitmapDrawable
?
The documentation for Drawable.getBounds() says the following:
This is not cristal clear but it looks like we must not change value returned by getBounds(), it fires some nasty side effects.
By using copyBounds() and setBounds() it works like a charm.
Another way of moving a Drawable could be to move the Canvas on wich you are drawing: