adjustViewBounds gives wrong width - Android - 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

Related

Setting width and height of ImageView using Aspect Ratio of the original image like whatsapp

I have a listview of the images of different sizes. Images are loaded using glide library.
Currently I have set the height and width of the imageview as wrap_content to get the original feel of the image, but it's not behaving properly.
Therefore I want to set imageview width and height using aspect ratio.
I have used different scaleType, but not getting desired result.
Any help is appreciated
I would suggest you use the DimensionConstraints from ConstraintLayout. You can define an aspect ratio using the app:layout_constraintDimensionRatio attribute, so your ImageView can be something like this:
<ImageView android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,2:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
So the image view height will always be half the value of the width.

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

How to create an ImageView that fills the parent height and displays an Image as big as possible?

I have an ImageView that is defined in the following way:
<ImageView
android:id="#+id/cover_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/title"
android:layout_above="#id/divider"
android:adjustViewBounds="true"
android:src="#drawable/image_placeholder"
android:scaleType="fitStart"/>
Now after downloading a new bitmap I change the drawable. The image now appears in the top left corner of the ImageView. Is there a way to have the image fill up the whole height that is possible and then adjust the width of the view to enable scaling the image without changing the ascpect ratio?
The image fills up all the space on a standard screen but on a WVGA Resolution the image takes only about half of the actual height of the ImageView.
If I'm understanding you correctly, what you need to use is the centerCrop scaleType. fitStart scales the image proportionally, but neither the width nor height will exceed the size of the view, and the image will, as you said, have a top|left gravity.
Using centerCrop scales the image proportionally, but causes the shortest edge of the image to match the size of the view, and if there is additional data on the long side that does not fit, it is simply cropped off. The gravity is, of course, center. The below worked for me:
<ImageView
android:src="#drawable/image_placeholder"
android:id="#+id/cover_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
/>
You can change scale type to fitXY via call to
imageView.setScaleType(ScaleType.FIT_XY);
simply do it in xml like
android:scaleType="fitXY"
Basically the answer here is that there is no predefined value for what you are trying to achieve. The solution is to create a Matrix that fits to your needs and call setImageMatrix(matrix) on your ImageView.

How do I display this image in 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

Categories

Resources