android - 自定义 Drawable 未绘制
问题描述
我正在尝试创建一个Drawable
将方形图像转换为圆形图像的自定义。图像已转换但未绘制。
我可以看到,如果我在单独的函数中返回,RoundedBitmapDrawable
它会显示ImageView
在draw
我的课
class RoundImage(context: Context?, bitmap: Bitmap): Drawable() {
private var dr: RoundedBitmapDrawable
init {
// give a round shape
dr = RoundedBitmapDrawableFactory.create(context!!.resources, bitmap)
dr.isCircular = true
dr.cornerRadius = bitmap.width / 2.0f
}
override fun draw(canvas: Canvas) {
// this draws nothing
dr.draw(canvas)
}
override fun setAlpha(alpha: Int) {
}
override fun setColorFilter(colorFilter: ColorFilter?) {
}
override fun getOpacity(): Int {
}
/**
* This returns a round drawable
*/
fun getDrawable(): Drawable {
return dr
}
}
我试着用
val roundImage = RoundImage(context, bitmap)
myPicture.setImageDrawable(roundImage)
解决方案
您还需要像这样覆盖onBoundsChange
方法:
override fun onBoundsChange(bounds: Rect) {
dr.bounds = bounds
}
或者,使用DrawableWrapper
可能是更好的选择。
class RoundImage(context: Context, bitmap: Bitmap) : DrawableWrapper(null) {
init {
// give a round shape
val dr = RoundedBitmapDrawableFactory.create(context.resources, bitmap)
dr.isCircular = true
this.drawable = dr
}
}
推荐阅读
- python - 如何将 AppsFlyer 连接到 Google Data Studio
- javascript - 在同一函数中停止 onclick() 事件
- python - 无法使用模型和表单将数据保存到 Django 中的数据库
- c# - 不能同时使用 Bind 和 ConfigureConsumer
- reactjs - npm run build 不为生产服务器创建构建文件夹
- angular - 创建 toast 时 showCloseButton 出错
- swift4 - 如何从swift 4中WKWebView中的evaluateJavaScript方法获取表单输入值?
- javascript - 如何从对象列表中创建单个对象?
- pandas - 重新格式化数据框列问题
- python - 从列表中删除密切匹配/相似的短语