2011-09-13 6 views
1

나는 안드로이드 용 게임을 만들려고합니다.이미지로 비트 맵의 ​​크기를 조절하고, 배경으로 사용하십시오.

이제 이미지를 비트 맵에 넣고 장치의 너비와 높이에 맞게 크기를 조정해야합니다.

내가 그림을 볼 때 그림이 화면보다 큽니다. 비트 맵을 화면에 맞추기를 원합니다.

도움 주셔서 감사합니다.

이 클래스 GameView가

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

가 난 그냥 내 onDraw는이 코드를 추가

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

클래스 배경을 동적으로 크기를 기반으로하기 : 여기

내 코드의 일부이다 (세로 및 가로 모드를 처리하기 위해).

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

사각형의 DST = 새로운 사각형 (0, 0, canvas.getWidth() canvas.getHeight());

관련 문제