未绘制自定义绘图



我正在尝试创建一个自定义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
}
}

最新更新