1

I want to add rounded corners to my custom imageview in kotlin and has created the drawable resource file ready and not working and not able to find out the way to use the view.SetToOutline.plz help me out to have the rounded corners

kotlin code

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatImageView

class DynamicHeightImageView : AppCompatImageView {
    private var whRatio = 0f

    constructor(context: Context) : super(context)

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

    fun setRatio(ratio: Float) {
        whRatio = ratio
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (whRatio != 0f) {
            val width = measuredWidth
            val height = (whRatio * width).toInt()
            setMeasuredDimension(width, height)
        }
    }

}


xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <com.georgcantor.wallpaperapp.util.DynamicHeightImageView
            android:id="@+id/pictureImageView"
            android:layout_width="match_parent"
            android:layout_margin="5dp"
            android:background="@drawable/rounded_corner"
            android:layout_height="match_parent"
            android:contentDescription="@string/image_item"
            android:foreground="?android:attr/selectableItemBackground"
            android:scaleType="fitXY" />

</RelativeLayout>

drawable file

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

    <stroke
        android:width="1dp"
        android:color="@color/off_white" />

    <solid android:color="@color/colorDetailPrimaryDark" />

</shape>```

2 Answers2

0

Please don't reinvent the wheel.There is a pretty library out there..https://github.com/vinc3m1/RoundedImageView

you can make use of this

Abhay Paul
  • 31
  • 4
  • https://stackoverflow.com/a/58033719/12152049. Can u help me in coding like this ...... It is similar to my problem but it is not working to my file – AorkGames 360 Feb 28 '20 at 20:06
0

Also you can do it with Glide image loader library

fun loadImage (imageUrl:String, myImageView:ImageView) {
   val options:RequestOptions = RequestOptions().transform(RoundedCorners(10))
   Glide.with(context)
    .load(imageUrl)
    .apply(requestOptions)
    .into(myImageView)
}
Narek Hayrapetyan
  • 1,731
  • 15
  • 27
  • https://stackoverflow.com/a/58033719/12152049. Can u help me in coding like this ...... It is similar to my problem but it is not working to my file – AorkGames 360 Feb 28 '20 at 20:05