用图像调整位图的大小,将其用作背景

发布于 2024-12-04 15:05:22 字数 887 浏览 3 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风吹 2024-12-11 15:05:22

我刚刚在 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 );

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 );
丶视觉 2024-12-11 15:05:22

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文