Android 自定义ImageView类不适用于毕加索图像下载库

Android 自定义ImageView类不适用于毕加索图像下载库,android,image,android-canvas,bitmapimage,picasso,Android,Image,Android Canvas,Bitmapimage,Picasso,我最近从ImageView扩展到创建一个CircularImageView类,该类使图像具有彩色边框的圆形。这是通过onDraw(canvas)方法完成的,方法是在传入的画布上绘制: //load the bitmap loadBitmap(); // init shader if(image !=null) { shader = new BitmapShader(Bitmap.createScaledBitmap(image, viewW

我最近从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);
    }   
因此,当通过可绘制或位图设置图像时,此位起作用。我还对它进行了扩展,这样我就可以将它与谷歌的Volley NetworkImageView一起使用,它也可以工作

我的问题出现在我尝试和我们一起使用CircularImageView类和毕加索图像下载库时,因为我把它看作是凌空截击的替代品。当获取BitmapDrawable时,第一行的my loadBitmap()函数中出现ClassCastException

private void loadBitmap()
{
    BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();

    if(bitmapDrawable != null)
        image = bitmapDrawable.getBitmap();
}
最初,在毕加索下载图片之前,它将占位符图像旋转得很好。但是,一旦Picasso下载了图像,就会出现ClassCastException,因为getDrawable()返回,PicassoDrawable而不是BitmapDrawable

private void loadBitmap()
{
    BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();

    if(bitmapDrawable != null)
        image = bitmapDrawable.getBitmap();
}
我想在我的CircularImageView类中保留onDraw(canvas)方法中的图像环绕工作,因为它包含的内容很好,而且是自动的,而不是每次与毕加索一起设置ImageView时都执行此过程。这可能吗


提前感谢。

与毕加索合作时,您应该:

  • 将舍入应用为
    转换
    ,以便将舍入的位图缓存在内存中,或
  • 在自定义
    ImageView
    子类中使用着色器夹紧画布。拉金·卡金·罗曼(ragin‘cagin’Romain)的家伙概述了这项技术的细节

  • 试图从
    ImageView
    中拉出底层
    位图
    是一种反模式。如果您确实需要访问
    位图
    (并且您不需要,您应该使用上面的一个),请在您的视图上实现
    目标
    ,其
    onBitmapSuccess
    回调将提供它。

    对于使用毕加索的圆形图像,请使用实现转换的类

    Picasso.with(context).load(url).transform(new RoundedTransformation(radius, margin)).into(imageview);
    

    每次都会创建一个新的位图,不是吗?谢谢DesertIvy、droidx和Jake Wharton的帮助。这个项目被搁置了,现在又回到问题上来。我刚刚用完美的作品尝试了上述方法。