ImageView to fit height, auto scale width, keep aspect ratio - android

I have a layout like this:
<LinearLayout
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginEnd="8dp"
android:gravity="start|center_vertical"
android:orientation="horizontal"
android:layout_weight="0.25">
<ImageView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:background="#drawable/reply_icon"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>
</LinearLayout>
I'm trying to get the ImageView to fit its height (20dp) and auto-adjust the width (while keeping its aspect ratio), however, it comes out looking stretched like this:
How can I fix this?

Add two attributes: (android:adjustViewBounds and android:scaleType=) to your ImageView for keep aspect ratio
android:adjustViewBounds="true"
android:scaleType="centerCrop"
Hope this help

Related

What exactly does adjustViewBounds do for the image output?

I have this code:
<ImageView
android:id="#+id/imageView3"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/house1"
android:scaleType="fitXY" />
and I have this code:
<ImageView
android:id="#+id/imageView3"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/house1"
android:scaleType="fitXY"
android:adjustViewBounds="true"/>
But I do not see any changes in output of the image.
What exactly does adjustViewBounds do for the image output?
When using adjustViewBounds you need to set either width or height of your image view to wrap content and the other to a fixed value. Then when you set adjustViewBounds to true, Android adjust your image based on the fixed height or width value you've set and keeps aspect ratio. The scale type plays also a role.

How to set maxHeight over DimensionRatio with ConstraintLayout?

I'm trying to display an image centered in parent with a dimension ratio of 1220:1000 AND a maximum height of 300dp (to keep the image small even with large screen)
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1220:1000"
app:layout_constraintHeight_max="200dp" ==> This line break the ratio (the image is not displayed)
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The app:layout_constraintHeight_max property break the ratio.
Is there is a way to do it?
If you want to enforce max_height and keep the dimension ratio at the same time, you need to constrain the width based on height. You can achieve that by adding W to your ratio:
app:layout_constraintDimensionRatio="W,1220:1000"
This will constrain the height first and then set the width accordingly to satisfy the ratio.
More info on how this dimension ratio works can be found in the documentation.
If you want to maximize the area with a dimension ratio and a max height, but you don't know if width or height is going to be adjusted, better don't to use layout_constraintDimensionRatio + layout_constraintHeight_max. They don't work well together.
Here I put my solution in the case you have an image 16:9 which will use full width while max height is not yet reached, otherwise it will respect max height, and width will be adjusted:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- This space view makes ImageView expands to its corresponding height -->
<Space
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageview" />
<com.a3.sgt.ui.widget.AspectRatioImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:adjustViewBounds="true"
app:dominantMeasurement="height"
app:aspectRatioEnabled="true"
app:aspectRatio="1.778"
android:scaleType="centerCrop"
app:layout_constraintHeight_max="400dp"
tools:src="#drawable/placeholder_glacier"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
I used a custom ImageView I had in my gist which adjusts to aspect ratio, in order to avoid using layout_constraintDimensionRatio which causes the problem together with layout_constraintHeight_max
Have you tried to add the tag maxHeight?
android:maxHeight="200dp"

Android resize ImageView with gravity=Left

I've an imageview with an icon on background, instead of generating all diferent sizes of icons for diferent sizes of screen i'm using only a big one and resizing it to fit the size I want.
If I set the layout_height to any value android automatically resize the width to keep aspect ratio (what is good) but the placeholder keeps the original width and the new resized icon is centered in the space it was supposed to fit
To demonstrate the situation i took this print
both left and right icons have the same dimension in original file
<ImageView
android:id="#+id/leftThumb"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:foregroundGravity="left"
android:src="#drawable/ic_left_thumb" />
<ImageView
android:id="#+id/rightThumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_right_thumb" />
setting only the imageview height and is it possible to make it resizes keeping gravity on left? (as in the print bellow)
It is quite simple.
Set your parent layout height as desired and its children height to "match_parent"
You can use "dimen.xml" files to control different sized screen dimensions, for now i have put a static value of "64dp" android:layout_height="64dp"
<RelativeLayout
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="64dp">
<ImageView
android:id="#+id/leftThumb"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:src="#drawable/ic_left_thumb" />
<ImageView
android:id="#+id/rightThumb"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="#drawable/ic_right_thumb" />
</RelativeLayout>
The answer was pretty easy... there is an property on ImageView that tells android how to deal when it needs to scale the image:
android:scaleType
so setting it properly i've
<ImageView
android:id="#+id/leftThumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="#drawable/ic_left_thumb"
/>
<ImageView
android:id="#+id/rightThumb"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitEnd"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_right_thumb" />
In this way, instead of fitting the icon at the center of the spaceholder, it's is going to adjust to the edges

Cropping an image in android to fit width and cut off height

I have an ImageView which needs to have a maximum height of 150dp and a width that matches the parent. However, what I need is for the actual image to be cropped, so the picture is as wide as the ImageView, but centered and cropped so the top and bottom of the picture aren't visible (sort of a preview). What I have currently:
<ImageView
android:adjustViewBounds="true"
android:background="#fafafa"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxHeight="150dp"
android:id="#+id/articlepreviewimage"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/naslovclanka"
android:layout_centerHorizontal="true"
/>
Also, it's crucial that the image stays in its' original proportions, so no putting it as background, no scaling etc. I just need it to fill up the width of the ImageView and show the center of the picture in that ImageView.
The way its' currently done, it scales the image so it fits into the height or width, but doesn't cut away anything. How to do this?
take a look at android:scaleType="centerCrop"
There are also a lot of other scale types that might fit what you what.
Try doing the following:
<ImageView
android:id="#+id/yourimageId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:adjustViewBounds="true"
android:layout_gravity="center_vertical"
android:clickable="true"
android:scaleType="fitXY"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:maxHeight="150dp"/>

How to handle image aspect ratio on different resolutions and rotation

What's the right and clean way to handle images aspect ratio?
In my app, if I rotate the screen, I lose the aspect ratio and my images appear ugly as hell
- see these images comparing my app and Play Store - http://imgur.com/a/GriwP#0
This is how I'm doing:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center" >
<Button
android:id="#+id/btnStark"
android:layout_marginLeft="4dp"
android:layout_marginRight="2dp"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#drawable/stark"
android:gravity="center|bottom"
android:layout_gravity="top" />
<Button
android:id="#+id/btnLannister"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#drawable/stark"
android:gravity="center|bottom"
android:layout_gravity="top" />
</LinearLayout>
How should I handle the height? Should I set a fixed value? Or is there any way it automatically keeps the aspect ratio?
The image I'm using is 400x300 pixels
Consider using ImageViews instead of buttons - they can be clickable just like buttons. You can set how the images scale using the scaleType attribute.
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
For example, with CENTER_INSIDE:
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).
So, something like:
<ImageView
android:id="#+id/btnStark"
android:layout_marginLeft="4dp"
android:layout_marginRight="2dp"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:src="#drawable/stark"
android:scaleType="centerInside"
android:gravity="center|bottom"
android:layout_gravity="top" />
A Lannister always pays his debts.
You can try using ImageButton instead of a simple Button and try different ScaleType modes to select what fits you better.

Categories

Resources