【问题标题】:ImageView with rounded corners in KotlinKotlin 中带圆角的 ImageView
【发布时间】:2019-09-20 18:21:10
【问题描述】:

我想在我的 Fragment 中有一个带圆角的 ImageView。 我有以下 Kotlin 代码:

val imageView: ImageView = root.findViewById(R.id.profile_view)
val pv = RoundedBitmapDrawableFactory.create(res, src)
pv.setCornerRadius = 0f
imageView.setImageDrawable(pv)

create 和 res 在 Android Stuido 中带有红色下划线。 创建说:

以下函数都不能被以下函数调用 提供的参数: - 位图? - 输入流 - 字符串

res 说:

需要表达式,但找到了包名。

希望有人能帮我解决这个问题。

问候,杰里米

【问题讨论】:

标签: android android-studio kotlin imageview rounded-corners


【解决方案1】:

请检查一下

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

并在你的布局中添加这个图像视图

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

希望能解决你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 2014-02-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2017-07-25
    相关资源
    最近更新 更多