Skip to content Skip to sidebar Skip to footer

Imageview With Rounded Corners In Kotlin

I want to have an ImageView with rounded corners in my Fragment. I've got the following Kotlin code: val imageView: ImageView = root.findViewById(R.id.profile_view) val pv = Rounde

Solution 1:

Please check this

package com.alok.myapplication

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.widget.ImageView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory


classRoundedImageView : ImageView {constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
    context,
    attrs,
    defStyleAttr
)

overridefunsetImageDrawable(drawable: Drawable?) {
    super.setImageDrawable(drawable)
    val radius = 0.1fval bitmap = (drawable as BitmapDrawable).bitmap
    val resourceId = RoundedBitmapDrawableFactory.create(resources, bitmap)
    resourceId.cornerRadius = bitmap.width * radius
    super.setImageDrawable(resourceId)
}
}

and add this imageview in your layout

 <com.alok.myapplication.RoundedImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/item_2"/>

I hope it will resolve your issue

Post a Comment for "Imageview With Rounded Corners In Kotlin"