2011-09-13 8 views
1

私はアンドロイド用のゲームを作ろうとしています。画像でビットマップのサイズを変更し、背景として使用します。

私は画像をビットマップに入れて、デバイスの幅と高さに合わせてサイズを変更する必要があります。

私は、画像が画面よりも大きい画像を見るようにしようとします。ビットマップが画面に収まるようにしたい。

ありがとうございました。ここ

は、私のコードの一部です:

クラスGameView

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.foto); 
background = new BackGround(this,bmp); 

クラスの背景

public class BackGround { 

     private Bitmap bmp; 
     private int width; 
     private int height; 

     public BackGround(GameView gameView, Bitmap bmp) { 
      this.bmp = bmp; 
      this.width = bmp.getWidth(); 
      this.height = bmp.getHeight(); 
     } 
     public void onDraw(Canvas canvas) { 

      Rect src = new Rect(0, 0, width, height); 
      Rect dst = new Rect(0, 0, width, height); 
      canvas.drawBitmap(bmp, src, dst, null); 
     } 
} 

答えて

3

私は動的にサイズベースに私のonDrawでこのコードを追加しました(ポートレートモードとランドスケープモードを処理するため)。

final int canvasWidth = getWidth(); 
final int canvasHeight = getHeight(); 

int imageWidth = originalImage.getWidth(); 
int imageHeight = originalImage.getHeight(); 

float scaleFactor = Math.min((float)canvasWidth/imageWidth, 
           (float)canvasHeight/imageHeight); 
Bitmap scaled = Bitmap.createScaledBitmap( originalImage, 
              (int)(scaleFactor * imageWidth), 
              (int)(scaleFactor * imageHeight), 
              true); 
+0

これをonDraw()に追加しましたか?ああああ.. – dcow

+0

ハ、良い点。スケーリングが毎回発生するのを防ぐためのガードがあったと思うが、それ以来スケーリングコードをより適切な場所、つまりsurfaceCreated()に移動した。よりクリーンで効率的です。 – ProjectJourneyman

0

Rect dst = new Rectangle(0、0、canvas.getWidth()、canvas.getHeight());

関連する問題