How do I display this image in Android? - android

How do I display an image with the following configuration in Android?
Retain original aspect ratio (the image can be scaled, but not stretched)
Fill the width of the parent
Aligned to the bottom of the parent
The following does not work:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:src="#drawable/background" />
Using the above xml the ImageView takes the width of the parent, but the image inside the ImageView does not.

Some points.
If you want to manteint aspect ratio instead of fitCenter you should try centerInside:
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).
Remember that devices using your application might have different screen sizes.
If your background image is something that can be stretched I would recommend you try a NinePatchDrawable

Related

adjustViewBounds gives wrong width - Android

I am trying to display an image which will have the width of the parent view and the height will be calculated to keep the aspect ratio.
The problem is that the width of the image expands beyond parent view width, like the image is zoomed.
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/imageview_mainimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
How can I fix this?
This happens because your SimpleDraweeView doesn't have the same aspect ratio as your loaded image, and adding android:scaleType="centerCrop"will make the SimpleDraweeView try to load the content cropping the image to best fit the smallest of the 2 dimensions. You can either expect the image to have a certain ratio fresco:viewAspectRatio="1.33", or use a different option for scaleType

Display image with width as match parent and height as 200dp without stretching in android

I want to display the image in image view with height=200dp and width=match_parent.By using height as 200dp and width as match_parent, the image width is same as it is taken.So I have tried android:scaleType="fitXY" but the image is stretching which is not required.How can i display image in ImageView as I want?
Xml:
<ImageView
android:id="#+id/ProfilePicIV"
android:layout_marginTop="20dp"
android:layout_height="170dp"
android:layout_width="match_parent"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_below="#+id/cropView"
/>
This is my xml code.But I am not able to display image as I want.I tried with all scale types.android:scaleType="fitXY" will display as I wanted but with the image stretching.
Use scaleType that best suits your picture. android:scaleType property for this.
1. ImageView.ScaleType CENTER
Center the image in the view, but perform no scaling.
2. ImageView.ScaleType CENTER_CROP
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 larger than the corresponding dimension of the view (minus padding).
3. ImageView.ScaleType 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).
4. ImageView.ScaleType FIT_CENTER
Scale the image using CENTER.
5. ImageView.ScaleType FIT_END
Scale the image using END.
6. ImageView.ScaleType FIT_START
Scale the image using START.
7. ImageView.ScaleType FIT_XY
Scale the image using FILL.
8.ImageView.ScaleType MATRIX
Scale using the image matrix when drawing.
You can use this.
<ImageView
android:id="#+id/ProfilePicIV"
android:layout_marginTop="20dp"
android:layout_height="200dp"
android:layout_width="match_parent"
android:layout_gravity="center"
android:scaleType="fitXY"
app:srcCompat="#drawable/YourImage"
android:adjustViewBounds="true"
android:layout_below="#+id/cropView"
/>

Image not stretching inside imageView android

I am having a layout in with root Relative layout and and ImageView inside it when i set it source the Image is not fitting inside into Image view leaving Top and and bottom padding which is in preview but not defined in xml. I want to know how to stretch image to fit completely in ImageView.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/place_demo_2"
android:id="#+id/back_ground_place"
android:layout_alignParentBottom="true"
android:layout_marginBottom="400dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Manila Main"
android:id="#+id/visit_place_title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp"
android:textColor="#fff"/>
</RelativeLayout>
Using Code:
YourImageView.SetScaleType(ImageView.ScaleType.FIT_XY);
Using xml:
android:scaleType="fitXY"
See also below snapshots for other alternative options.
Use android:scaleType in your ImageView
android:scaleType="fitXY"
Check other available options for scaleType here
You're going to want to use android:scaleType in your XML file. There are several options you will probably want to play with until you get the exact product you are looking for but, if you don't care about aspect ratio, it sounds like you might want to use android:scaleType="fitXY".
You can find more information here:
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Below are the rest of the options:
ImageView.ScaleType="CENTER"
Center the image in the view, but perform no scaling.
ImageView.ScaleType="CENTER_CROP"
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 larger than the corresponding dimension of the view (minus
padding).
ImageView.ScaleType="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).
ImageView.ScaleType="FIT_CENTER"
Scale the image using CENTER.
ImageView.ScaleType="FIT_END"
Scale the image using END.
ImageView.ScaleType="FIT_START"
Scale the image using START.
ImageView.ScaleType="FIT_XY"
Scale the image using FILL.
ImageView.ScaleType="MATRIX"
Scale using the image matrix when drawing.
either make width of Imageview to wrap_content
or
you have to set android:scaleType of Imageview to fitXY
Add to ImageView attr android:scaleType="fitXY"
That's because you have aligned the ImageView to parent's bottom BUT there is a margin at the bottom of 400dp.
Long story short, remove
android:layout_alignParentBottom="true"
android:layout_marginBottom="400dp"
Also set the layout_height to match_parent if you want the image to fill the whole screen.
P. S. Also fill_parent is deprecated, so use match_parent instead.

Increase the size of Image in android app

I have attached a image in my android app but when i launch the app it is very small.Please suggest how to increase its size in app so that its clearly visible.
Below is the snippet code of my activity_main.xml :
<ImageView
android:id="#+id/power_image"
android:src="#mipmap/ic_launcher"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_above="#+id/logButton"
android:scaleType="fitCenter"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:visibility="visible"
/>
Change your scaleType to centerCrop and it will be worked. Here is full guide about ScaleType in ImageView in Android. The following is a list of all the most common types of scaleType:
center : Displays the image centered in the view with no scaling.
centerCrop : Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining
the image aspect ratio; centers the image in the view.
centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller
than the view, then this is the same as center.
fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly
match the view, and the result is centered inside the view.
fitStart Same as fitCenter but aligned to the top left of the view.
fitEnd Same as fitCenter but aligned to the bottom right of the view.
fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.
matrix Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be
used to apply transformations such as rotations to an image.
In the imageView you have set dimensions as 200dp x 200dp for the imageView.
Set a background color for the imageView eg yellow. This will let you know the amount of area the imageView covers. In case if the imageView doesn't cover the area desirable to you, increase the height and width of the imageView.
In case if the imageView covers enough space but the image is still small and covers only a portion of the yellow area (bg of imageView). Then you can use scaleType = fitxy in imageView, if you dont want to maintain aspect ratio else go for scaleType = centerCrop or centerInside

Android scaleType aspectFill

What is the value for android:scaleType that will achieve the effect of scale aspect fill? In other words, I need the image to keep it's aspect ratio, but it should fill the canvas. This will obviously lead to part of the image being cut off (if canvas and image aren't similar rectangles) but I want it this way.
Well, you could look at the documentation. CENTER_CROP seems to be what you're looking for:
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 larger than the corresponding dimension of the view (minus padding).
The value you appear to want is 'CENTER_CROP'.
Center_crop is what you're searching for. This property will expand the image until fill the container, preserving in center and ratio.
<ImageView
android:layout_width="100dp"
android:layout_height="400dp"
android:src="#drawable/ic_launcher"
android:scaleType="centerCrop"/>

Categories

Resources