如何让 ImageView 填满屏幕的整个宽度?

发布于 2025-01-02 23:49:18 字数 1068 浏览 2 评论 0原文

我正在尝试从网络上获取图像并将其填充整个屏幕宽度。这似乎正在做我的计算,它总是返回 0。

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Integer width = metrics.widthPixels;

try {
    ImageView imageView = (ImageView) findViewById(R.id.imageView1);

    Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL("http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif").getContent());
    float height =bitmap.getHeight() * (width/bitmap.getWidth());

    bitmap = Bitmap.createScaledBitmap(bitmap, width, (int) height, true);
    imageView.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

如果由于以下行,此代码将使程序崩溃:

float height =bitmap.getHeight() * (width/bitmap.getWidth());

由于某种原因,如果位图的宽度大于屏幕宽度,它将始终返回 0,因为 Float 不允许有任何小数位。

一个例子是:

屏幕尺寸:800px;图片宽度:500px;图片高度:400px;

新图像的宽度应为 800 像素,高度为 640 像素。因为浮点数无法正常工作,而不是 400 x 1.6(即 800/500),所以代码会删除小数并执行 400 x 1。

有任何线索吗?

I'm trying to take an image from the web and have it fill the entire width of the screen. This seems to be working up to doing my calculations which are always returning 0.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Integer width = metrics.widthPixels;

try {
    ImageView imageView = (ImageView) findViewById(R.id.imageView1);

    Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL("http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif").getContent());
    float height =bitmap.getHeight() * (width/bitmap.getWidth());

    bitmap = Bitmap.createScaledBitmap(bitmap, width, (int) height, true);
    imageView.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

This code will crash the program if because of the line:

float height =bitmap.getHeight() * (width/bitmap.getWidth());

For some reason if the bitmap's width is greater than the screen width it will always return 0 because the Float isn't allowing for any decimal places.

An example would be:

Screen Size: 800px; Image Width: 500px; Image Height: 400px;

The new image should then have a width of 800px and height of 640px. Because the float isn't working correctly instead of having 400 x 1.6 (i.e.800/500), the code is dropping the decimal and doing 400 x 1.

Any clues?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

愿得七秒忆 2025-01-09 23:49:18

将整数除法的结果转换为浮点数:

float height = bitmap.getHeight() * ((float)width / bitmap.getWidth());

Cast the result of integer devision to float:

float height = bitmap.getHeight() * ((float)width / bitmap.getWidth());
把梦留给海 2025-01-09 23:49:18

Don't scale the bitmap yourself, let the ImageView do it.

See ImageView.setScaleType(). Use ScaleType.CENTER_CROP or CENTER_INSIDE, depending on what you need.

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