0

In my code I want to set a default image for my imageview. All my images are of 174px X 174px. So, I want the default image to be same size. This is my xml for image view

//....code

<ImageView
    android:id="@+id/ivCover"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:contentDescription="Default Image"
    android:src="@drawable/default_cover" />


//....some other code

Now, my default_cover.jpg is also 174px X 174px. But it shows a smaller image. I've tried all

android:scaleType="centerInside"
android:scaleType="center"
android:scaleType="matrix"

even when most of them were senseless for me but still I tried all of 'em & none of them works. Then I specified the height & width of image by specifying

android:layout_width="174px"
android:layout_height="174px"

(I know using dp is better than px but because this image size is fixed & I was just testing it). So it shows

default image which doesn't look good[default image]

while when loaded from an external url using

Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url_source_for_image).getContent());
ivCover.setImageBitmap(bitmap);

desired image

clear image with desired image quality[]

The two pictures may not look very different here but the difference is very clear on my android device. And please don't say that I didn't try other solutions.. Here is a list of links of so itself..

ImageView fit without stretching the image

Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?

ImageView one dimension to fit free space and second evaluate to keep aspect ration

Resizing ImageView to fit to aspect ratio

Maximum width and height for ImageView in Android

Image in ImageView is stretched - Android

Android: How to prevent image from being scaled in ImageView or ImageButton?

Community
  • 1
  • 1
Shivam Mangla
  • 92
  • 6
  • 16
  • Which drawable folder is your bitmap in? If you put a 50x50 bitmap in drawable-ldpi and a 174x174 bitmap in drawable-hdpi, Android will decide which one to display based on the resolution of the device, not on the size you specified. If you want show a specific bitmap to be used on all devices, regardless of resolution, put it in res\drawable-nodpi. – DevOfZot Jun 26 '13 at 16:32
  • Images are in res\drawable-xxhdpi because I think android smartly down scales the image size to show in low resolution devices. – Shivam Mangla Jun 26 '13 at 16:34
  • Right, so maybe it's scaling the image down to lower resolution, and then scaling it back up to fit your specified size. Does it work if you put it in drawable-nodpi instead? – DevOfZot Jun 26 '13 at 16:36
  • If I set height & width to wrap_content then it just shows a smaller image & when I specify those attributes = 174px then it again shows those distorted pixels. – Shivam Mangla Jun 26 '13 at 16:43
  • Oh yes! It worked. Actually I had copied that image previously. This time.. I moved it & it works fine now. I wonder that I never came across this no-dpi thing before. Thanks a lot. – Shivam Mangla Jun 26 '13 at 16:47
  • I'll mark your answer as accepted. If you want then please add it as an answer. :) – Shivam Mangla Jun 26 '13 at 16:48
  • No worries, glad to help. I think no-dpi is not that popular, so you don't see it mentioned that often. Sure saved me a lot of headaches when I found it. – DevOfZot Jun 26 '13 at 16:52

1 Answers1

3

Sounds like the answer is to use drawable-nodpi. For future reference, if you have the same named image in both the ldpi/hdpi/etc and in no-dpi, I think the no-dpi one will be ignored.

DevOfZot
  • 1,362
  • 1
  • 13
  • 26