ImageButton Stretching Background Image

2019-07-03 01:44发布

问题:

I am trying to create an ImageButton with no border but I am running into an issue with the sizing of the image button.

Using Eclipse ADT I drag an ImageButton to the layout and select my background image. The image button appears as follows:

As you can see there is a border between the background image and the image button perimeter which I would like to get rid of. To do so, I set the padding property to 0dp. The image button now looks like this:

Finally, I set the scale type to fitXY:

Notice that the image is stretched and is no longer the size of the original image. (I want the ImageButton size to be exactly the same size as the background image.)

Here is a snapshot of all 3 images next to each other to make the difference more clear:

I have tried re-sizing the image, setting the max size properties, etc. but nothing seems to prevent the image button from stretching the background image.

How do I prevent the image from stretching like it is?

UPDATE

I also tried setting scale type to matrix but here is what happened:

回答1:

Just set your image as android:background for button and set layout_height and layout_width for button to wrap_content



回答2:

Maybe this might help, but I also know its best practice to make different sized images from android development.

https://developers.google.com/appengine/docs/java/images/overview



回答3:

Use an ImageView instead and set it to be clickable. Set the scaleType to matrix (which will cause the ImageView to be as big as the image inside of it).

The problem with ImageButton is that it adds a background that causes padding, etc.



回答4:

You can set android:background="@null"