用图像调整位图的大小,将其用作背景
我正在尝试为 Android 制作一款游戏。
现在我必须将图像放入位图中并调整其大小以适合设备的宽度和高度。
当我尝试这样做时,我看到一张图片,该图片大于屏幕。我希望位图适合屏幕。
感谢您的帮助。
这是我的代码的一部分:
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) {
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'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) {
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚在 onDraw 中添加了这段代码,以根据屏幕动态调整大小(以处理纵向和横向模式)。
I just added this code in my onDraw to dynamically size based on the screen (to handle portrait and landscape modes).
矩形 dst = new 矩形(0, 0, canvas.getWidth(), canvas.getHeight());
Rect dst = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());