A Petrov
A Petrov

Reputation: 474

How to stretch ImageButton without the image getting blurred?

I create a new ImageButton via the "Resource Chooser" and I end up with this code:

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="94dp"
    android:src="@drawable/mybutton" />

Now, I want to make that button way bigger, so I do the following changes:

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="94dp"
    android:src="@drawable/aries" />

I also change the android:src="@drawable/mybutton" to android:background="@drawable/mybutton" to get rid of the grey button around my image and stretch the image instead.

Problem:

My problem is that when stretching out the image it still uses the file from the drawable-mdpi (as it should), so I end up with a stretched low resolution image. If I change the image from the drawable-mdpi folder with a high resolution one it looks great, but I am pretty sure that this is not the best solution out there as I would have to put an image with bigger resolution in each folder.

Upvotes: 0

Views: 454

Answers (2)

Nadeem Iqbal
Nadeem Iqbal

Reputation: 2374

Try Setting the background to null:

  <ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    android:src="@drawable/ic_launcher" />

Upvotes: 0

Nicklas Jensen
Nicklas Jensen

Reputation: 1474

Sound like what you need is a Nine-Patch Drawable.

A NinePatch is a PNG image in which you can define stretchable regions that Android scales when content within the View exceeds the normal image bounds. [...] when the View grows to accomodate the content, the Nine-Patch image is also scaled to match the size of the View.

Upvotes: 1

Related Questions