I use an ImageView, I want the source image to fit to the TOP and RIGHT.
FitEnd scaling fits BOTTOM and RIGHT
all other scale enums don't help much as well
any idea?
Thanks
Try this code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|right">
<ImageView
android:scaleType="fitEnd"
android:adjustViewBounds="true" />
</LinearLayout>
This should get you the intended result.
I beleive you are looking for the fitStart or, in code, setScaleType(ImageView.ScaleType.FIT_START);
Using that you could simply wrap the image in a layout wrapper that is aligned to the right, such as a RelativeLayout with android:layout_alignParentRight="true"
Just use scaleType = MATRIX
e.g:
<ImageView
android:layout_width="%wanted width%"
android:layout_height="%wanted height%"
android:scaleType="matrix"
android:src="%your src%" />
Related
Problem: trying to give multi device support screen for a simple layout but in Nexus 6 its creating the problem,means not showing fit.
It's side is cutting down where as per as docs for nexus-6 resolution is 1440*2560 and image should be in drawable-xxxhdpi .so i keep a image with resolution of 1440*2560.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:id="#+id/imv" />
</RelativeLayout>
if i ll give match_parent so obvious it will stretch the image.
Please help me to get out of it.
Here is screen shots of screen -
http://s10.postimg.org/43od5j5h5/screen_shot_layout.png
Set the width of the Image to fill_parent (you can set margins if you want a bit smaller imageView or you could also just set a fixed width)
Use android:adjustViewBounds to keep aspect ratio
Set height to wrap_content (it will take whatever it takes to maintain aspect ratio)
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/icon"
android:id="#+id/imv"
android:scaleType="fitCenter" />
If you want to fit the image on the imageview, replace android:src with android:background in the ImageView in the xml code.
Try to use these lines of code in the layout
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
android:scaleType="fitXY"
android:id="#+id/imv" />
I have an image on my ImageView but I don't know how to make it bigger with XML. My image it's the following:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:src="#drawable/image"/>
I tried changing android:layout_width and android:layout_height attributes but if I put them with a number (for example, 200dp width and 100dp height) the ImageView it's on the left of the screen and not center.
I want to center the image and makes it a little bit bigger than the original. I couldn't find anything to makes it bigger at the same time it is displayed on the center of the screen.
Is it possible? How can I do that?
Thanks in advance!
Use scaletype and add android:adjustviewbounds="true" in your image view section.
Keep the size you want as the width and height of the ImageView and change the gravity of its parent to center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/img"
android:layout_marginTop="40dp"
android:src="#drawable/image"/>
</LinearLayout>
EDIT:
If this is what you want your layout to look like
then all you would need to do is add
android:layout_gravity="center_horizontal"
to your ImageView, and specify the width and height in dp
There are 3 attributes you can add to your imageview:
android:scaleType=""
android:scaleX=""
android:scaleY=""
ScaleType types can be seen here. Basically, you'll want to use CENTER_CROP.
scaleX and scaleY uses float as parameters, defining the scale for the two axis. See the reference here.
Both codes above works (Andrew Brooke and IntelliJ Amiya, Thanks!) but now I found a "trick" to avoid both of these methods. It's to create the ImageView using android:minWidth and android:minHeight so the final ImageView will be like:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:minWidth="100dp"
android:minHeight="70dp"
android:src="#drawable/image"/>
With that, you can center your ImageView with the size that you want.
I am working on a android card view. Here is the code that I have now:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:orientation="vertical"
android:background="#drawable/drawable_card_bg">
<ImageView
android:id="#+id/photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/test_img" />
....
and the UI looks like:
However this is not what I am looking for. I want the image to be placed on top of the card, with the top, right, left margins to be 10dip from the background card boarder.
Meanwhile, I want to keep the ratio of the image, so don't want to hard-code the width/height of the image view. I want the image width to fit the width of the background card, and the height should be adjusted according to the width.
How should I change the layout parameters to achieve this?
Thank you
Add android:adjustViewBounds="true" to your ImageView in xml in order to let the ImageView adjust its bounds.
It's very easy......... please do this thing. Replace your imageview code using this one
<ImageView
android:id="#+id/photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#drawable/test_img" />
If my answer will help you then support this.
Thanks!!!
I've got a problem with my application. I've a image which goes across the whole screen (ImageView) with this code in xml:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="#drawable/achtergrond"
android:scaleType="fitCenter" />
My problem is that the image is centered in the imageview itself. It needs to be centered up as you can see in the next picture:
The left image is what I have, with the image in the middle. The right is what I want, the image should be up in the whole layout, I need to set my image to match_parent to have it fill as background (I didn't set it as background because it would be stretched then).
I hope I'm missing something obvious! Thanks for helping me in advance!
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/achtergrond"
android:scaleType="fitStart" />
I have LinearLayout and I want to put into it ImageView. Is there any simple way how to put this image on the right side of the screen? Now, the image is placed to the left, padding and marking don't solve this, because this should be done dynamically, the image is each time different.
Thanks.
Have you tried setting the gravity attribute to right?
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:contentDescription="#string/imageDesc"
android:src="#drawable/image" />