在Kotlin中带有圆角的ImageView [英] ImageView with rounded corners in Kotlin
本文介绍了在Kotlin中带有圆角的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的Fragment中有一个带有圆角的ImageView. 我有以下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 = RoundedBitmapDrawableFactory.create(res, src)
pv.setCornerRadius = 0f
imageView.setImageDrawable(pv)
Android Stuido用红色下划线标出
create和res. 创建说:
create and res are red underlined by Android Stuido. create says:
以下任何一个函数都不能用以下命令调用 提供的参数: -位图? -InputStream -字符串
None of the following functions can be called with the following arguments supplied: - Bitmap? - InputStream - String
res说:
期望表达,但找到包名称.
Expression expected, but a package name found.
我希望有人可以帮助我解决该问题.
I hope somebody can help me to fix that problem.
致谢,杰里米
推荐答案
请选中
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
class RoundedImageView : 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
)
override fun setImageDrawable(drawable: Drawable?) {
super.setImageDrawable(drawable)
val radius = 0.1f
val 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
这篇关于在Kotlin中带有圆角的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文