Android: layout_width and height crop my image - android

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.

Related

Android Layout - Scale Images to stay always at same place in different resolutions

I am trying to create a custom top view using anImageView where buttons stays always at the same position and scale in proportion of the resolution of the device. I have been trying to put my elements inside a RelativeLayout using margins without any success the button always move from it position and its not scaling.
How could achieve this?
This is the example I am trying to get for all resolutions:
and here my XML:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:srcCompat="#drawable/settings_red_btn"
android:id="#+id/settings_btn"
android:contentDescription="settings btn"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="9dp"
android:layout_marginTop="11dp"
android:layout_width="43dp"
android:layout_height="43dp" />
<ImageView
android:layout_width="match_parent"
app:srcCompat="#drawable/top"
android:id="#+id/imageView17"
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:adjustViewBounds="true" />
</RelativeLayout>
You have 2 options here :
You need to have the same padding right in the images themselves resources settings_red_btn & top. That way, you set to them alignParentRight = true, and they will align with the same padding.
You need to specify padding right for imageView17, according to screen resolutions. This is done by defining dimens.xml file on all resolutions. There are many blogs and stackoverflow hits on this. You can start with : http://android4beginners.com/2013/07/appendix-c-everything-about-sizes-and-dimensions-in-android/

Remove padding of ImageButton

How can I get an ImageButton that has a fixed height and is only as wide as it needs according to the ratio of its source image?
I tried the following:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="8pt"
android:minWidth="0pt"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/list_download"
android:background="#android:color/black"
android:padding="0pt"/>
I set background to black just to see the boundaries of it exactly, and here is the outcome.
I have seen some solutions that suggest to use negative paddings but it is not an elegant solution. There should be a better one.
I tried similar configurations on an ImageView too, but it also had extra padding. So it's not an ImageButton specific problem (i.e. it is not related to this nine-patch issue).
EDIT: If I change scaleType from fitCenter to fitStart, then the outcome is like this. So there is somehow a minimum width.
It's because you've set a limited height. That limits the image height, but when Android calculates the width of the content (the original unrezised image), it's wider.
You'll get the image without the black "padding" on the side if you use wrap_content for the height as well.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0pt"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/list_download"
android:background="#android:color/black"
android:padding="0pt"/>
So, if you want the picture to be a specific size, you could resize it in your drawable folder to the size you want.
you just need use android:scaleType="fitXY" and set padding 0dp.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:padding="0dp"/>
you can put it in a Frame like so :
<FrameLayout
android:layout_width="wrap_content"
android:background="#android:color/black"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/list_download"
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="8dp"/>
</FrameLayout>
it will be much easier to play with it like so

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

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 :
Yet I am achieving this :
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" />
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.
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" />
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.
You want to do it inside the XML file right?
<ImageView
android:layout_width="size of image (in density pixels)"
android:layout_height="size of image (in density pixels)" />
so for example:
<ImageView
android:layout_width="60dp"
android:layout_height="60dp" />

Android how to make ImageView with a minWidth have a border?

I want to diplay an image so that it takes up a minmum width of the screen and should scale in its aspect ratio (the image width might be smaller or larger than the minimun width) I also want to have a fixed size border around this image. This is my xml section for it:
<ImageView
android:id="#+id/detailed_view_boxArt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"
android:layout_alignParentLeft="true"
android:layout_below="#id/detailed_view_heading"
android:padding="10dp"
android:layout_marginLeft="5dp"
android:scaleType="centerInside"
android:background="#000"/>
This however does not produce what I want...the problem is that it does not scale the image horizontally far enough (ie. the 10px padding that I have seems a lot more on the right and left)
How would I produce the result that I want?
to set minimum width and heidht use the attributes android:minHeight="100dip" and android:minWidth="200dip".
to set the Border check my post.
Try this (I emphasized the important part):
<ImageView
android:id="#+id/detailed_view_boxArt"
*android:layout_width="match_parent"*
*android:layout_height="wrap_content"*
*android:adjustViewBounds="true"*
*android:scaleType="centerCrop"*
android:layout_alignParentLeft="true"
android:layout_below="#id/detailed_view_heading"
android:padding="10dp"
android:layout_marginLeft="5dp"
android:background="#000"/>

Categories

Resources