我正在尝试为 Android 制作游戏。
现在我必须将图像放入位图中并调整其大小以适应设备的宽度和高度。
当我尝试制作时,我看到一张图片,图片比屏幕大。我希望位图适合屏幕。
感谢您的帮助。
这是我的部分代码:
类 GameView
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.foto);
background = new BackGround(this,bmp);
类(class)背景
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);
}
}
最佳答案
我刚刚在我的 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 );
关于android - 使用图像调整位图大小,将其用作背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7404539/