Android GridView 3 x 6图像裁剪

时间:2022-04-27 16:58:53

I would like to create a grid of square images, but I'm unable to get the images to appear as I need. I started with the GridView tutorial:

我想创建一个方形图像网格,但我无法让图像显示为我需要的。我从GridView教程开始:

http://developer.android.com/resources/tutorials/views/hello-gridview.html

http://developer.android.com/resources/tutorials/views/hello-gridview.html

which seemed close to what I want.

这似乎接近我想要的。

The only difference I can see is that I want exactly 18 images and they all need to be square in size. I set the number of columns to be 3 and then load the 18 images, which should then be in 6 rows. I want the images to fully fill each row and not leave any unwanted extra sized gaps. I don't know the size of the images until they are downloaded to the phone. Hopefully they'll be nearly square to start with, but I need to crop the center to make them perfectly square and then scale them to fully fill the 3 columns. I need the images to retain their aspect ratios.

我能看到的唯一区别是我想要18个图像,它们都需要是方形的。我将列数设置为3,然后加载18个图像,然后应该是6行。我希望图像完全填满每一行,不要留下任何不必要的额外大小的间隙。在将图像下载到手机之前,我不知道图像的大小。希望它们开始时几乎是方形的,但是我需要裁剪中心以使它们完全正方形,然后缩放它们以完全填充3列。我需要图像来保持它们的宽高比。

I can't seem to find settings or code to make this work. The images overlap, have gaps, don't scale properly, or something else is wrong.

我似乎无法找到设置或代码来使这项工作。图像重叠,有间隙,无法正确缩放或其他错误。

The closest I've gotten is by changing the GridView tutorial as follows when my emulator is set to 480x800:

我最接近的是当我的模拟器设置为480x800时,通过更改GridView教程如下:


imageView.setLayoutParams(new GridView.LayoutParams(480/3, 480/3));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
imageView.setAdjustViewBounds(true);

====

< GridView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:numColumns="3" 
    android:stretchMode="columnWidth" 
    android:gravity="fill|clip_horizontal|clip_vertical"
/ > 

This is almost correct. The square test images I'm using are missing a couple of pixels, the non square images are not cropped but instead fit inside with black boxing to retain ratio and the hardcoded values are brittle and won't work on all phones, orientations, or layouts. I find it interesting the CENTER_CROP documentation seems to claim it will do what I want by expanding to fit the image, but instead seems to CENTER_INSIDE.

这几乎是正确的。我正在使用的方形测试图像缺少几个像素,非方形图像不会被裁剪,而是用黑色拳击装入内部以保持比率,硬编码值很脆弱,不适用于所有手机,方向或布局。我觉得有趣的是CENTER_CROP文档似乎声称它会按照我想要的方式扩展以适应图像,但似乎是CENTER_INSIDE。

I feel close, but never quite getting what I want. Maybe I should be using something other than GridView? Any suggestions?

我感觉很亲密,但从来没有得到我想要的东西。也许我应该使用GridView以外的东西?有什么建议么?

Please no snark.

请不要嗤之以鼻。

Thanks.

谢谢。

1 个解决方案

#1


0  

Obviously CENTRE_CROP is the right answer, but can you upload a screenshot so that I can figure it out and help you with the issue you are facing?

显然CENTRE_CROP是正确的答案,但是你可以上传截图,以便我可以解决它并帮助你解决你面临的问题吗?

Also I suggest you upload the item_layout xml file.

另外我建议你上传item_layout xml文件。

Make sure you have used <ImageView> width to a specific size.

确保已将 宽度用于特定大小。

For the different screen size flexibility, I suggest you get the screen width programmatically, divide the width by 3 and deduct some padding from it and after that set the same value for both the image's width and height, this will give you a square image on any screen.

对于不同的屏幕尺寸灵活性,我建议你以编程方式获得屏幕宽度,将宽度除以3并从中扣除一些填充,然后为图像的宽度和高度设置相同的值,这将为您提供方形图像任何屏幕。

#1


0  

Obviously CENTRE_CROP is the right answer, but can you upload a screenshot so that I can figure it out and help you with the issue you are facing?

显然CENTRE_CROP是正确的答案,但是你可以上传截图,以便我可以解决它并帮助你解决你面临的问题吗?

Also I suggest you upload the item_layout xml file.

另外我建议你上传item_layout xml文件。

Make sure you have used <ImageView> width to a specific size.

确保已将 宽度用于特定大小。

For the different screen size flexibility, I suggest you get the screen width programmatically, divide the width by 3 and deduct some padding from it and after that set the same value for both the image's width and height, this will give you a square image on any screen.

对于不同的屏幕尺寸灵活性,我建议你以编程方式获得屏幕宽度,将宽度除以3并从中扣除一些填充,然后为图像的宽度和高度设置相同的值,这将为您提供方形图像任何屏幕。