2012-08-07 72 views
0

这是我的代码在Sprite类:比例位图SurfaceView

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);我画的背景。但是背景比屏幕大...

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

回答

2

绘制时只设置位图的左侧属性和顶部属性。为了扩展它,叫过载drawBitmap方法与宽度和高度,like this one

为了让你的想法:

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); 
    } 
} 

在活动,其中创建类雪碧,你必须做一些事情,如:

Display display = getWindowManager().getDefaultDisplay(); 
Sprite sprite = new Sprite(bitmap, display.getWidth(), display.getHeight()) 
+0

当我不想要颜色,我必须在那里写'null'? – Stupe 2012-08-07 12:05:45

+0

你可以使用下面的方法:public void drawBitmap(Bitmap bitmap,Rect src,RectF dst,Paint paint),如果你不需要剪裁,将它传递给src null,然后创建一个dst矩形来定义绘制位图的区域(其左上角将设置位图的左上角及其宽度,高度将设置位图的宽度和高度) – victorvartan 2012-08-07 12:09:33

+0

以及如何获取屏幕尺寸? – Stupe 2012-08-07 12:10:50