Android: problems with stretching / scaling on the image button

I struggled with the fact that I think something simple. I set the image to the image button using the class. I want the image to fit the limitations of the screen, but without stretching. That is, if it is too wide to fit the screen, I want the height to be scaled to fit, so that it does not stretch. And if the height is too high, I want the height of the image to decrease, thereby also reducing the width.

I saw this question a couple of times, and I tried all the tips, but for me it doesn't really matter. I think this may have something to do with usnig ImageButton instead of ImageView?

Here are some important pieces of code (badly broken) ...

In my class:

ImageView img = (ImageButton) dialog.findViewById(R.id.fullsizeimage); img.setScaleType(ImageView.ScaleType.CENTER_INSIDE); //Changing ScaleType here has no effect img.setImageBitmap(useThisBitmap); 

And here is the contents of the xml layout ...

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageButton android:id="@+id/fullsizeimage" android:background="@null" android:layout_width="wrap_content" android:layout_height="fill_parent" android:adjustViewBounds="true" /> </RelativeLayout> 

Some of these junk files in the XML file are probably incorrect. But I changed it so many times, and it has a ZERO effect.

All tips are welcome.

+4
source share
1 answer

I think ImageButton will always stretch the background image to fill its entire background. ImageView can be easily clickable on its own, have you tried this?

A more complicated option is to use a custom view, which can draw your image on the canvas in any way convenient for you and may have an onTouch listener.

+3
source

Source: https://habr.com/ru/post/1343546/


All Articles