【发布时间】:2013-12-08 03:24:48
【问题描述】:
我最近从 ImageView 扩展创建了一个 CircularImageView 类,它使图像带有彩色边框的圆形。这是通过 onDraw(canvas) 方法在传入的画布上绘制完成的:
//load the bitmap
loadBitmap();
// init shader
if(image !=null)
{
shader = new BitmapShader(Bitmap.createScaledBitmap(image, viewWidth + (borderWidth * 2), viewHeight + (borderWidth * 2), true), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
int circleCenter = viewWidth / 2;
// circleCenter is the x or y of the view's center
// radius is the radius in pixels of the cirle to be drawn
// paint contains the shader that will texture the shape
canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth, paintBorder);
canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paintBackground);
canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter, paint);
}
所以当通过可绘制或位图设置图像时,此位有效。我还对其进行了扩展,因此我可以将它与 Google 的 Volley NetworkImageView 一起使用。
当我尝试将我的 CircularImageView 类与 Picasso 图像下载库一起使用时,我的问题就出现了,因为我正在将其视为 Volley 的替代品。获取 BitmapDrawable 时,第一行的 loadBitmap() 函数中会发生 ClassCastException。
private void loadBitmap()
{
BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();
if(bitmapDrawable != null)
image = bitmapDrawable.getBitmap();
}
在毕加索下载图片之前,它会很好地围绕占位符图像。但是一旦 Picasso 下载了图像,它就会失败并返回 ClassCastException,因为 getDrawable() 返回并且 PicassoDrawable 而不是 BitmapDrawable。
我想在我的 CircularImageView 类的 onDraw(canvas) 方法中保留对图像进行舍入的工作,因为它被很好地包含并且是自动的,而不是每次都使用 Picasso 设置 ImageView 时执行该过程。这可能吗?
提前致谢。
【问题讨论】:
标签: android image android-canvas bitmapimage picasso