在SurfaceView中缩放位图

时间:2012-08-07 11:52:50

标签: android scale surfaceview

这是我在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);我画了一个背景。但背景比屏幕更大......

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

1 个答案:

答案 0 :(得分:2)

在绘制位图时,您只设置位图的左侧和顶部属性。要缩放它,请调用宽度和高度为like this one

的重载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())