开发者

resize a bitmap with a image, use it as background

开发者 https://www.devze.com 2023-04-04 09:19 出处:网络
i\'m trying to make a game for android. now I have to put a image into a bitmap and resize it to fit the width and height of the device.

i'm trying to make a game for android.

now I have to put a image into a bitmap and resize it to fit the width and height of the device.

When i try to make that i see a picture that the picture is greater than the screen. I want that the bitmap fits into the screen.

Thanks for your help.

here is a part of my code:

class GameView

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

class Background

public class BackGround {

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

       public BackGround(GameView gameView, Bitmap bmp) {
  开发者_JAVA技巧           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);
       }
}


I just added this code in my onDraw to dynamically size based on the screen (to handle portrait and landscape modes).

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消