I have a list of images. I display it in the GridView using the adapter:

public View getView(int position, View convertView, ViewGroup parent) { Book book = getItem(position); ImageView preview; if (convertView == null) { // if it's not recycled, initialize some attributes preview = new ImageView(mContext); int height = Utilities.convertDpToPixels(175, mContext); int width = Utilities.convertDpToPixels(175, mContext); preview.setLayoutParams(new GridView.LayoutParams(width, height)); preview.setPadding(8, 8, 8, 8); preview.setImageResource(R.drawable.ic_launcher_background); } else { preview = (ImageView) convertView; } preview.setId(position); Bitmap bitmap = Utilities.loadImageFromStorage(book.getPath(), book.getTitle()); preview.setAdjustViewBounds(true); preview.setImageBitmap(bitmap); return preview; 

The problem is that I need the same cells, and after inserting a Bitmap there, they change their size to the size of a Bitmap. How can I fix the dimensions of View. Image to be cropped in the center. I thought to use the method of manually cutting the picture, but it will increase the process of loading the list.

  • Use imageView image size instead of wrap_content? - ahgpoug
  • @ahgpoug why wrap_content? I have the same layer parameters: int height = Utilities.convertDpToPixels(175, mContext); int width = Utilities.convertDpToPixels(175, mContext); preview.setLayoutParams(new GridView.LayoutParams(width, height)); int height = Utilities.convertDpToPixels(175, mContext); int width = Utilities.convertDpToPixels(175, mContext); preview.setLayoutParams(new GridView.LayoutParams(width, height)); - Ponomarenko Oleh
  • Where do you upload the image? In ImageView . Honestly, I didn’t read this code, but most likely the element has a wrap_content parameter. What prevents to pre-set its size? - ahgpoug
  • Yes, about the question of trimming the image in the center - use Glide for image processing. In it, you can immediately load the image into the ImageView and form it in the center, but in general, trim it in any way you like. + there will be no large memory leaks. - ahgpoug

1 answer 1

You yourself allow ImageView to adjust its size under the image

 preview.setAdjustViewBounds(true); 

Instead, put

 preview.setScaleType(CENTER_INSIDE или CENTER_CROP);