How to handle image aspect ratio on different resolutions and rotation - android

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.

Related

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

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

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

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"/>

stop image from resizing itself

I have an image that seems to change its size depending on the parents height, well the parent/parents height. Point is I do not want the image to change size. Is there some way to stop it from thinking for itself?
<ImageView
android:id="#+id/ivSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:src="#drawable/img_save"
android:layout_gravity="left"
android:contentDescription="#string/ivSave" />
wrap_content means that the view will resize automatically. To set a fixed size use:
android:layout_width="100dp" and android:layout_height="100dp"
Use 'dp' instead of 'px', because these are device-independent.
try
android:scaleType="centerInside"
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/img_save"
android:scaleType="centerInside">
</ImageView>
click here for more image scale type

Android: layout_width and height crop my image

i have this little layout i defined to inserted in every page of my app:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#null"
android:src="#drawable/sa_info"
android:layout_alignParentRight="true"/>
i've sa_info.png that is 118x118 but i want it to look much smaller on the screen. if i try to set width and height of the imagebutton to the desired size (lets say 48dp) the resulting image when i run the app is cropped to the center:
so the only way i have to get the desired result is scale the image itself to the desired resolution.
what i'm doing wrong, is there a better way to do this?
Try adding
android:scaleType="fitCenter" -> http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
android:adjustViewBounds="true" -> Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

Categories

Resources