0

I asked this question on here i was trying to load wide and long images with Glide into a GridView, and wanted to load these different sized images more uniformly like Picasso does while keeping the aspect ratio as much as possible. The answer i got was its a long standing issue with GridViews and to try Jake Wharton's aspect ratio image view which i tried without any success. The other suggestion was to make a staggered RecyclerView, great answer, but a lot of changing

The reason for all of this was my images were too large for Picasso to load quickly and users were complaining that it was really slow, but Glide (which seems much quicker) loads the images to there aspect ratio and messes up the uniformity of my grid, you can see in the link: long images take the same space that 2 wide images do. Anyway, after lots of trial and error I am now using Glide like this:

int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
int myWidth = width / 2;

Glide.with(getActivity())
     .load(mThumbIds[position])
     .placeholder(R.drawable.placeholder)
     .override(myWidth, myWidth)
     .centerCrop()
     .into(imageView)
;

I'm new to Android, but this seems to work perfectly. And my question now is other than getWidth being deprecated, (the app is for API 16+) is there anything wrong with this (getting screen width dividing it and passing it to Glide's resize call)? I think it has to wait to measure the view, but it's still quicker than Picasso was. Can anyone offer anything better?

Community
  • 1
  • 1
Martin Seal
  • 616
  • 2
  • 14
  • 32
  • Glide doesn't wait for the layout in this case, because you use `.override()` which will be the resulting Bitmap's size. – TWiStErRob Oct 01 '15 at 11:20
  • 1
    I think you should go with those suggestions, both staggered `RecyclerView` and `AspectImageView` (or a more generic layout like [this](https://gist.github.com/TWiStErRob/f8190f1f55ec1e7777fc)) will help in the long run. Querying the display size is extremely error prone (margins, paddings, spacing, etc... is not taken into account); let the Android layout system do its job ;) – TWiStErRob Oct 01 '15 at 11:23
  • you need to use `display.getSize(size);` – karan Oct 01 '15 at 11:37

2 Answers2

0

for api less than honeycomb you can use your approach but for api higher then that you need to use getSize() method. I use the below code for this

    if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR2) {
         Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
    } else {
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
    }
karan
  • 8,637
  • 3
  • 41
  • 78
0

You can use getDisplayMetrics()

getActivity().getResources().getDisplayMetrics().widthPixels;
Kaushik
  • 6,150
  • 5
  • 39
  • 54