Closed mob-rockstar closed 1 year ago
I am having the same issue:
<com.makeramen.roundedimageview.RoundedImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="fitCenter"
app:riv_border_color="@color/white"
app:riv_border_width="1dp"
app:riv_corner_radius="5dp"
app:riv_mutate_background="true" />
result is the original aspect ratio of the image with rounded corners and appropriate borders.
Is there some other way to make it a square?
To solve this for now I wrote this method and gave the result to the ImageView
fun centerCropBitmap(srcBmp: Bitmap, newSize: Size? = null): Bitmap {
val width = srcBmp.width
val height = srcBmp.height
var dstBmp: Bitmap
if (srcBmp.width >= srcBmp.height) {
dstBmp = Bitmap.createBitmap(
srcBmp,
width / 2 - height / 2,
0,
height,
height
)
} else {
dstBmp = Bitmap.createBitmap(
srcBmp,
0,
height / 2 - width / 2,
width,
width
)
}
if (newSize != null) {
dstBmp = Bitmap.createScaledBitmap(
dstBmp,
newSize.width,
newSize.height,
false
)
}
return dstBmp
}
val bitmap = <bitmap_from_somewhere>
val croppedImageBitmap = centerCropBitmap(bitmap, Size(size, size))
previewImage.setImageBitmap(croppedImageBitmap)
<com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto" android:scaleType="fitCenter" android:layout_width="80dp" android:layout_height="80dp" app:riv_corner_radius="40dp" app:riv_border_width="2dp" app:riv_border_color="@color/colorMain" app:riv_mutate_background="true" app:riv_oval="true" />
I have used above xml for circle image with border color. but if source image is not square, result is elliptic, not circle.