简体   繁体   English

Android:如何移动BitmapDrawable?

[英]Android: How to move a BitmapDrawable?

I'm trying to move a BitmapDrawable in a custom view. 我正在尝试在自定义视图中移动BitmapDrawable It works fine with a ShapeDrawable as follows: 它适用于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();
    }
}

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. 但是,如果我使用BitmapDrawable ,drawable的边界会发生变化,则会调用onDraw方法,但图像会保留在屏幕上的位置。

The following constructor will reproduce the problem by creating a BitmapDrawable instead: 以下构造函数将通过创建BitmapDrawable来重现该问题:

public MyView() {
    image = getResources().getDrawable(R.drawable.image);
    image.setBounds(0, 0, 100, 100);
}

How can I move a BitmapDrawable ? 如何移动BitmapDrawable

The documentation for Drawable.getBounds() says the following: Drawable.getBounds()的文档说明如下:

Note: for efficiency, the returned object may be the same object stored in the drawable (though this is not guaranteed), so if a persistent copy of the bounds is needed, call copyBounds(rect) instead. 注意:为了提高效率,返回的对象可能是存储在drawable中的相同对象(虽然这不能保证),因此如果需要边界的持久副本,请调用copyBounds(rect)。 You should also not change the object returned by this method as it may be the same object stored in the drawable. 您也不应该更改此方法返回的对象,因为它可能是存储在drawable中的同一对象。

This is not cristal clear but it looks like we must not change value returned by getBounds() , it fires some nasty side effects. 这不是cristal clear,但看起来我们不能改变getBounds()返回的值,它会引发一些令人讨厌的副作用。

By using copyBounds() and setBounds() it works like a charm. 通过使用copyBounds()setBounds(),它就像一个魅力。

public void move(int x, int y) {
    Rect bounds = image.copyBounds();
    bounds.left += x;
    bounds.right += x;
    bounds.top += y;
    bounds.bottom += y;
    image.setBounds(bounds);
    invalidate();
}

Another way of moving a Drawable could be to move the Canvas on wich you are drawing: 移动Drawable的另一种方法是移动画布上的画布

@Override
protected void onDraw(Canvas canvas) {
    canvas.translate(x, y);
    image.draw(canvas);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在android中将BitmapDrawable转换为LayerDrawable - How to cast BitmapDrawable to LayerDrawable in android 如何在下次clickevents上显示android bitmapdrawable更改 - how to show android bitmapdrawable change on next clickevents 我如何在 android 中将 statelistDrawable 转换为 bitmapdrawable - How can i convert a statelistDrawable to bitmapdrawable in android 如何在Android中将ImageIcon转换为BitmapDrawable? - How do I convert an ImageIcon to BitmapDrawable in Android? Android-如何更改由BitmapDrawable创建的灰色背景 - Android - How to change grey background created by a BitmapDrawable 如何在Android中使用android.graphics.drawable.bitmapdrawable? - How to use android.graphics.drawable.bitmapdrawable in android? Android BitmapDrawable构造函数未定义 - Android BitmapDrawable Constructor Undefined 如何从Android中的BitmapDrawable或位图图像中删除白色背景色? - How to remove white background color from BitmapDrawable or bitmap image in android? 如何将TransitionDrawable转换为BitmapDrawable? - How to cast TransitionDrawable to BitmapDrawable? Android:无法将BitmapDrawable强制转换为ClipDrawable - Android: cannot cast BitmapDrawable to a ClipDrawable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM