Android ImageView设置Bitmap FitXY不起作用 [英] Android ImageView setting Bitmap FitXY doesn't work

查看:1437
本文介绍了Android ImageView设置Bitmap FitXY不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试设置位图很长时间,因此它适合 ImageView 边界。

I've been trying for a really long time to set the bitmap so it will fit the ImageView bounds.

它只能通过 scaleType:fitXY 起作用。我的位图图像大小低于 ImageView (保存在100X100像素),我希望我的位图图像适合并延伸到 ImageView

It just doesn't work via scaleType: fitXY. My bitmap images size are lower than the ImageView's (Saved on 100X100 pixels), I want my bitmap images to fit and stretch into the ImageView.

以下是一些代码:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/imageView" 
   android:layout_height="match_parent"
   android:layout_width="match_parent"
   android:src="@drawable/locked_image"
   android:scaleType="fitXY"
/>

以下是设置 ImageView 的代码(我在不同的地方设置 scaleType 进行测试):

Here's the code of setting the ImageView (I'm setting the scaleType in different places for testing):

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  final ImageView imageView;
  if (convertView == null) {
    imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
    imageView.setScaleType(ScaleType.FIT_XY);
  } else {
    imageView = (ImageView) convertView;
    imageView.setScaleType(ScaleType.FIT_XY);
  }

  imageLoader.displayImage(imagesIds[position], imageView, options);
  imageView.setScaleType(ScaleType.FIT_XY);
  return imageView;
}

以下是它的外观:

正如您所看到的, ImageView 的高度很好,但我希望它的宽度相同。我无法访问通过我的活动我的位图图像,所以请不要让我调整我的位图大小。我真的不明白为什么它不能正常工作。

As you can see, the Height of the ImageView is fine, but I want the Width of it to be the same. I do not have access to my bitmap image via my activity, so please don't ask me to resize my bitmap. I really don't understand why it just doesn't work as it should.

编辑:

只是为了让问题更容易理解。为每个设备计算 ImageView 高度。每个设备都不同。我希望我的 ImageView 的宽度与高度相同。我使用 Two-WayGridView 库来显示图像是否有任何区别。


Just to make the problem more understandable. The ImageView height is calculated for each device. It is different for each device. I want the width of my ImageView to be the same as the height as it is. I am using the Two-WayGridView library to show the images if it makes any difference.

推荐答案

使用 scaleType 不会改变ImageView的大小,因为您认为应该如此。它所做的只是将位图缩放到 of ImageView(它的效果非常好,从你的截图判断)。

Using scaleType will not change the size of your ImageView, as you seem to think it should. All it does is scale the bitmap up to the size of the ImageView(which it is doing quite well, judging by your screenshot).

如果你想制作方形ImageViews,你应该尝试查看这个问题

If you want to make square ImageViews, you should try looking at this question.

此外,我不确定我理解您为何使用 TwoWayGridView 库。它专门设计用于说明行/列的数量,并且它将拉伸项目以适应。这似乎与你想要的完全相反,因为你想让它们成为正方形。如果你覆盖链接的问题/答案中显示的每个项目的 onMeasure(),普通的GridView就可以工作。

Also, I'm not sure I understand why you're using the TwoWayGridView library. It's specifically designed to allow you to state the number of rows/columns, and it will stretch the items to fit. This seems to be the exact opposite of what you want, since you want to make them square. A normal GridView will work, provided you override the onMeasure() of each item like it shows in the linked quesion/answer.

这篇关于Android ImageView设置Bitmap FitXY不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆