0

这是我在Sprite课堂上的代码:

public class Sprite {
    int x, y, vx, vy;
    private Bitmap texture;

    public Sprite(Bitmap texture) {
        this.texture = texture;
    }

    public int getWidth() {
        return texture.getWidth();
    }

    public int getHeight() {
        return texture.getHeight();
    }

    public void draw(Canvas c) {
        c.drawBitmap(texture, x, y, null);
    }
}

background.draw(c);我一起画一个背景。但是背景比屏幕大...

如何将其缩放到屏幕大小?

4

1 回答 1

2

绘制位图时只设置位图的 left 和 top 属性。要缩放它,请调用具有宽度和高度的重载 drawBitmap 方法,就像这个

给你的想法:

public class Sprite {
    int width, height;
    private Bitmap texture;

    public Sprite(Bitmap texture, width, height) {
        this.texture = texture;
        this.width = width;
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void draw(Canvas c) {
        c.drawBitmap(texture, null, new RectF(0, 0, width, height), null);
    }
}

在创建 Sprite 类的 Activity 中,您必须执行以下操作:

Display display = getWindowManager().getDefaultDisplay();
Sprite sprite = new Sprite(bitmap, display.getWidth(), display.getHeight())
于 2012-08-07T12:02:42.930 回答