Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView adjustViewBounds not working

I have an ImageView with android:layout_width=100dp, android:layout_height=wrap_content and android:adjustViewBounds=true

It's source is a 50 x 50 px picture. But the aspect ratio is not preserved - height of the ImageView is 50px, not 100px (i.e. adjustViewBounds is not working). If I have a 200x200px picture it works - width and height are 100px. This code results in a 100px wide and 50px tall picture but the src image is square:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent">      <ImageView         android:id="@+id/photo"         android:src="@drawable/icon"         android:layout_width="100dp"         android:layout_height="wrap_content"         android:scaleType="fitXY"         android:adjustViewBounds="true" />  </LinearLayout> 
like image 222
fhucho Avatar asked Oct 10 '11 22:10

fhucho


People also ask

What is adjustViewBounds in Android?

android:adjustViewBounds—If set to true, the attribute adjusts the bounds of the ImageView control to maintain the aspect ratio of the image displayed through it.

Can ImageView be clickable?

For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.

Can ImageView be used as button?

ImageView is used when we want to work with images or we want to display them in our application. So, this article will give you a complete idea of using an ImageView as a Button in android studio. So, without wasting further time let's go to the article and read how we can achieve this task.


2 Answers

The issue is that adjustViewBounds will not increase the size of the ImageView beyond the natural dimensions of the drawable. It will only shrink the view to maintain aspect ratio; if you provide a 500x500 image instead of a 50x50 image, this should work.

If you're interested in the spot where this behavior is implemented, see ImageView.java's onMeasure implementation.

One workaround is to implement a custom ImageView that changes this behavior in onMeasure.

like image 165
Roman Nurik Avatar answered Oct 18 '22 19:10

Roman Nurik


There's a more simple way. Make your ImageView like this:

<ImageView   android:layout_width="match_parent"   android:layout_height="match_parent"   android:scaleType="fitCenter"   android:adjustViewBounds="true"/> 

This way drawable will stretch to fit in the ImageView center by preserving the aspect ratio. We just have to calculate the right height to make it proportional so we don't have any blank space:

private void setImageBitmap(Bitmap bitmap, ImageView imageView){     float i = ((float)imageWidth)/((float)bitmap.getWidth());     float imageHeight = i * (bitmap.getHeight());     imageView.setLayoutParams(new RelativeLayout.LayoutParams(imageWidth, (int) imageHeight));     imageView.setImageBitmap(bitmap); } 
like image 43
vladexologija Avatar answered Oct 18 '22 20:10

vladexologija