drogaleggera drogaleggera - 4 months ago 15
Android Question

The lowest dpi drawable is being used on a xxhdpi screen

I have two imagebuttons, with dimensions 256x256 dp and

scaleType="centerCrop"
.
When I see the preview in AS or when I launch the application on my nexus 5 the images are blurry. The images are of the right dimensions for the respective folders, up to xxxhdpi(192x192).

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/black">

<ImageButton
android:layout_marginTop="48dp"
android:layout_width="256dp"
android:layout_height="256dp"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:src="@mipmap/button_off"
android:background="@android:color/black"
android:scaleType="centerInside"
/>
<ImageButton
android:layout_width="256dp"
android:layout_height="256dp"
android:id="@+id/second_button"
android:layout_gravity="center_horizontal"
android:src="@mipmap/second_off"
android:background="@android:color/black"
android:scaleType="centerInside"
/>

</LinearLayout>

Answer

If I understood it right - in your XML layout file you set an ImageButton with width=256dp and height=256dp, and src=drawable/your_drawable. The xxxhdpi drawable has width and height 192px (I assume than mdpi - 48, hdpi - 72, xhdpi - 96, xxhdpi - 144, xxxhdpi - 192).

If so, than of course the images are blurred, because you set the ImageButtons width/height in mdpi (in xml to 256dp), but you used 144x144 (nexus 5 is xxhdpi) Drawable stretched (because of the scaleType=centerCrop).

If you don't want to have stretched images, here are some options for you (maybe not all):

  1. you need to change the scaleType to centerInside (or without scaleType) - you will get smaller buttons - the images are scaleDowned only if needed
  2. you need to make the ImageButton smaller - 48x48
  3. you need to add bigger drawable resources - your mdpi drawable should be as big as your ImageButton in xml (mdpi - 256x 256, hdpi - 384x384, xhdpi - 512x512, xxhdpi - 768x768, xxxhdpi - 1024x1024)
  4. you post some layout code (XML) in your question with detailed drawable resources resolution which you used and maybe we can make further investigation in your problem