我想在Kotlin中将圆角添加到我的自定义图像视图中

问题描述 投票:0回答:2

我想在kotlin中将圆角添加到我的自定义imageview中,并且已经创建了可绘制资源文件,但该文件无法工作并且无法找到使用该view.SetToOutline.plz的方法,可以帮助我获得圆角。 >

kotlin代码

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文件

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

可绘制文件

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

我想在kotlin中将圆角添加到我的自定义imageview中,并且已经创建了可绘制资源文件,但该文件无法工作并且无法找到使用view.SetToOutline.plz帮助的方法...

java android kotlin imageview
2个回答
0
投票
请不要重新发明轮子。那里有一个漂亮的库。https://github.com/vinc3m1/RoundedImageView

0
投票
您也可以使用Glide图像加载器库来实现
© www.soinside.com 2019 - 2024. All rights reserved.