I change imageview source with bitmap in code but it does not keep ratio of it. It uses dimensions of the old image.
I set bitmap by;
imageView.setImageBitmap(bitmap);
It stretches bitmap to fit dimensions of old image resource. I want to keep bitmap ratio.
Check out the imageView ScaleTypes: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
CENTER Center the image in the view, but perform no scaling.
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).
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).
FIT_CENTER Scale the image using CENTER.
FIT_END Scale the image using END.
FIT_START Scale the image using START.
FIT_XY Scale the image using FILL.
MATRIX Scale using the image matrix when drawing.
You want something like CENTER_CROP or CENTER_INSIDE
Related
I've got an ImageView with match_parent width and wrap_content height. When I set an image with a size of 1080x500, the ImageView takes up the entire screen in width, but for some reason takes up extra space in height.I know about AdjustViewbounds, but I'm wondering why imageView takes up extra space in height. I have a Pixel phone (1080x1920).
here is my layout
Check this out https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
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).
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).
FIT_CENTER
Scale the image using Matrix.ScaleToFit.CENTER
Matrix.ScaleToFit.CENTER: Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.
FIT_END
Scale the image using Matrix.ScaleToFit.END
Matrix.ScaleToFit.END: Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. END aligns the result to the right and bottom edges of dst.
FIT_START
Scale the image using Matrix.ScaleToFit.START
Matrix.ScaleToFit.START: Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. START aligns the result to the left and top edges of dst.
FIT_XY
Scale the image using Matrix.ScaleToFit.FILL
Matrix.ScaleToFit.FILL: Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src.
MATRIX
Scale using the image matrix when drawing.
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"
/>
I've good knowledge of ImageView scaling in Android. But couldn't understand the complete diff b/w scaleTypes: centerCrop and centerInside.
Need some clear explanation.
CENTER_CROP
Added in API level 1
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). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerCrop".
CENTER_INSIDE
Added in API level 1
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). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerInside".
Docs
CenterCrop may crop the image and will always fill the imageview. CenterInside will not crop the image and may leave some part of the imageview not covered by the image.
This is my xml code:
<ImageView
android:id="#+id/ColImgPath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:background="#drawable/border"/>
and this my java code, that it is inside listview:
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
imageView.setPadding(5, 5, 5, 5);
imageView.setVisibility(View.VISIBLE);
try {
Bitmap bmp = ((Bitmap) MyArrList.get(position).get("ImageThumBitmap"));
int bmpx = bmp.getWidth();
int bmpy = bmp.getHeight();
Log.i("INFO","X: "+bmpx+" Y: "+bmpy);
Bitmap photobitmap1 = Bitmap.createScaledBitmap(bmp, bmpx/2,bmpy/2, true);
imageView.setImageBitmap(photobitmap1);
//imageView.setImageBitmap((Bitmap) MyArrList.get(position).get("ImageThumBitmap"));
} catch (Exception e) {
// When Error
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
The problem is that I want to put the bitmap resized into bitmap but the imageview always appears squared.
The size of the bitmap is: X: 4160 Y: 3120 and the screen size is : WIDTH= 1080 HEIGHT= 1776
I want to put the width fill parent and resize the height keeping the aspect ratio. I tried some xml alternatives like `ScaleType.
Someone knows the way to put correctly the bitmap?
Does fitXy not work for the scale type?
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
It's an ImageView so you need to use android:src="#drawable/border" instead of android:background. Also try android:scaleType="fitXY" or cropCenter.
There are different Scale Type Available in Android ImageView
CENTER_INSIDE - used to set Bitmap with keeping bitmap original aspect ratio.
CENTER_CROP - used to set Bitmap in imageview to cover imageview without broken aspect Ratio , rather bitmap set from negative x , y value to cover imageview area.
FIT_XY - used to set whole bitmap into available ImageView height and width means bitmap takes Imageviews Aspect Ratio.
OR
if you want to resize bitmap then take height or width which you want to set and take ratio of height / width if take width or take width/ height if you have next height and multiply repetitive width and height to this ratio .It will change height and width of bitmap but keep Aspect Ratio of original Bitmap .
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