Android GridView 3 x 6 Image Cropping

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:

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.

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:


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.

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

Please no snark.

Thanks.


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?

Also I suggest you upload the item_layout xml file.

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.

链接地址: http://www.djcxy.com/p/31480.html

上一篇: 在Android TextView中对齐文本

下一篇: Android GridView 3 x 6图像裁剪