我正在尝试创建一个自定义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)
答案 0 :(得分:0)
您还需要像这样重写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
}
}