0

I have simple ImageView in XML:

<ImageView
    android:id="@+id/imgItemFavorites"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/foo"
    android:src="@drawable/vote_favorite" />

Now, if I reference that view in code and do:

ImageView img = (ImageView) llGenericList.findViewById(R.id.imgItemFavorites);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.vote_favorite, null);
img.setImageBitmap(bitmap);

My ImageView ends up being smaller since loaded image is smaller. I presume this has something to do with autoscaling that's done because of tvdpi of Nexus 7, but can anyone give me idea how I can fix this in a robus way that won't screw up things on other devices?

nikib3ro
  • 20,366
  • 24
  • 120
  • 181
  • According to my knowledge you should prefer set Image background like: android:background= "@drawable/vote_favorite" try this one rather than android.src="@drawable/vote_favorite"; – Maulik Mar 06 '13 at 07:07
  • Make sure you are putting hdpi image in the drawable-hdpi folder only not any other folder. – Maulik Mar 06 '13 at 07:32

1 Answers1

1

There are a few things to do when creating images that should scale themselves to the 'native' resolution.

Firstly lets say you have a PNG file that is 200x100 pixels. Make this your 'baseline'.

Create your imageview with android:layout_width="200dp" and android:layout_height="100dp".

You will then need to supply 4 versions of the bitmap for each 'category' of device, and scale the bitmap in your supplied resources:

  • ldpi: 150px x 75px (Times mdpi by 0.75x)
  • mdpi: 200px x 100px
  • hdpi: 300px x 150px (Times mdpi by 1.5x)
  • xhdpi: 400px x 200px (Times hdpi by 2.0x)

Android will use the right png size from your resources when scaling. The important thing is specifying what the baseline size is in the layout.

Andrew Cranston
  • 396
  • 3
  • 3
  • I obviously forgot to mention that I am loading same image (from hdpi). – nikib3ro Mar 06 '13 at 03:59
  • TVDPI is smaller than HDPI (213 vs 240). So your hdpi resource will be visually smaller on screen. This is why it's best to put the baseline in dps in the xml markup. Also, if your setting the src in XML, you dont need to be using setImageBitmap... your just doubling up on your work. – Andrew Cranston Mar 06 '13 at 04:01