vinc3m1 / RoundedImageView

A fast ImageView that supports rounded corners, ovals, and circles.
Other
6.44k stars 1.25k forks source link

Circle imageview is not working if I set the scale type into fitCenter #222

Closed mob-rockstar closed 1 year ago

mob-rockstar commented 6 years ago

<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.

ka05 commented 6 years 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?

ka05 commented 6 years ago

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)