2012-08-07 70 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