Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing Image to fit Imageview

I've look at MANY questions on Stackoverflow and tried many workaround but it still didn't work.

What I wanna achieve :

enter image description hereenter image description here

Yet I am achieving this :

enter image description here

This is my current xml code:

<ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/text"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:scaleType="centerInside"
    android:src="@drawable/ic_action_search" />
like image 310
MrYanDao Avatar asked Aug 07 '12 11:08

MrYanDao


People also ask

How do I resize an image in ImageView to keep the aspect ratio?

However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="..." . src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView.

How do I fit a picture on android?

FIT_XY. Scale the image using Matrix. ScaleToFit#FILL . From XML, use this syntax: android:scaleType="fitXY" .

Which attribute is used to set an image in ImageView?

src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.


3 Answers

The best way to achieve this is to scale the image (bitmap) using a Matrix.

An alternative is to use a library I previously created which does this for you:

https://github.com/laurencedawson/ZoomableImageView

This will automatically scale the image to fit the screen and also add pinch to zoom.

like image 96
Ljdawson Avatar answered Oct 05 '22 04:10

Ljdawson


add

<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/text"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/ic_action_search" />
like image 28
Azahar Avatar answered Oct 05 '22 05:10

Azahar


CenterInside, Resize Image According To view dimensions and its Resolutions. So if an Image Has size of 300*450 and we have set this image to an imageview of scale type centerinside, with dimensions 200*400, then it will make size of image to 200*300, According to the target dimensions, so scaling it down make 200 is minimum in width and height. I hope I am clear enough. In cases where you need to set dimensions of exact size use FItXY, as suggested by Adeel.

like image 32
jeet Avatar answered Oct 05 '22 06:10

jeet