我正在尝试为Android制作游戏。
现在我必须将图像放入位图并调整其大小以适应设备的宽度和高度。
当我试图让我看到图片大于屏幕时。我希望位图适合屏幕。
感谢您的帮助。
这是我的代码的一部分:
类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);
}
}
答案 0 :(得分: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 );
答案 1 :(得分:0)
Rect dst = new Rect(0,0,canvas.getWidth(),canvas.getHeight());