2

I have an image loading from a url using Glide, with the image I'm getting the image height. WHen I set the height, and width to match parent, the height is too high and there is empty space on bottom and top of image. The height is in pixels

This is how I'm setting height:

if (height != 0) {
            height = convertPixelsToDp(height, mContext);
            ViewGroup.LayoutParams params = viewHolder.listphoto.getLayoutParams();
            params.height = height;
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            viewHolder.listphoto.setLayoutParams(params);
}

XML:

         <ImageView
                android:id="@+id/listphoto"
                android:adjustViewBounds="true"
                android:layout_width="match_parent"
                android:background="#dddddd"
                android:layout_height="wrap_content"
                android:scaleType="fitCenter"
                />

I tried experimenting with adjustViewBounds and scaleType but nothing is working. It's not wrapping the top and bottom. How can I make this work? I've been working on this all night

UPDATE:

 public static float convertPixelsToDp(float px, Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
        return dp;
    }
franklinexpress
  • 1,149
  • 14
  • 44
  • This is probably because the height you're setting to your ImageView is larger than the actual height of your image. That's is why you are getting spaces on top and the bottom of the Image (fitCenter, remember?). Try figuring out what the height should be (with pxToDp(...)). – Nilesh Singh Dec 23 '16 at 19:18
  • I logged the height, and it's same as it shows on the camera resolution settings, and I dont use px to do, but I forgot to delete that line – franklinexpress Dec 23 '16 at 19:37
  • Yes, that is the problem. You shouldn't be using the height of the Image (in pixels). Rather try to convert it to the size the device needs (different screen sizes have different DPIs). Also, it will be better if you add pxToDp() method to your question. – Nilesh Singh Dec 23 '16 at 19:40
  • Oh I see, how would I convert ? – franklinexpress Dec 23 '16 at 19:52
  • this will help: https://developer.android.com/guide/practices/screens_support.html – Nilesh Singh Dec 23 '16 at 19:57
  • @NileshSingh added px to dp – franklinexpress Dec 23 '16 at 20:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131377/discussion-between-nilesh-singh-and-franklinexpress). – Nilesh Singh Dec 23 '16 at 21:49

4 Answers4

1

Okay. So, the answer is very simple. We just need to change the height of the ImageView with respect to the width of the image and the width of the screen. Doing this will set the image properly in the ImageView:

private float getHeight(float height, float width) {
    Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return (height * size.x / width);
}

If your ImageView has some margin or padding set to it, use this instead:

private float getHeight(float height, float width, int containerHeight) {
    return (height * containerHeight / width);
}

Then finally,

if (height != 0) {
        height = (int) getHeight(height, width);
        //or if has some margin or padding
        ViewGroup.LayoutParams params = viewHolder.listphoto.getLayoutParams();
        params.height = height;
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        viewHolder.listphoto.setLayoutParams(params);
}

Or if has padding or margin:

if (height != 0) {
        height = (int) getHeight(height,  width, viewHolder.listphoto.getMeasuredWidth());
        ViewGroup.LayoutParams params = viewHolder.listphoto.getLayoutParams();
        params.height = height;
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        viewHolder.listphoto.setLayoutParams(params);
}

Note: It will be a better if you go with the second one. It will ensure that even when you apply some margin/padding to your ImageView, the image sets properly in the ImageView ignoring the padding/margin.

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
0

add these attributes

android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
Hasif Seyd
  • 1,686
  • 12
  • 19
0
android:scaleType="fitXY"
android:adjustViewBounds="true"

I think these two property will resolve the problem and please remove

 android:scaleType="fitCenter"

Change width to

 android:layout_width="wrap_content"
Khushvinder
  • 298
  • 1
  • 9
0

android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"

This will work for you without giving scaleType.

Arth Tilva
  • 2,496
  • 22
  • 40