[英]resize a bitmap with a image, use it as background
i'm trying to make a game for android. 我正在尝试为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 类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) {
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). 我只是在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 );
Rect dst = new Rect(0,0,canvas.getWidth(),canvas.getHeight());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.