自定义ImageView的类不工作与毕加索的图像下载库 [英] Custom ImageView class not working with Picasso image downloading library

查看:113
本文介绍了自定义ImageView的类不工作与毕加索的图像下载库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从ImageView的扩展创造了CircularImageView类,这使得用彩色边框的图像循环。

I have recently extended from an ImageView to create a CircularImageView class which makes the image circular with a coloured border. This is done via the onDraw(canvas) method by drawing onto the canvas that is passed in:

//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);
    }   

所以,通过绘制或位图设置的图像时,该位工作。我也延长,所以我可以使用它与谷歌的凌空NetworkImageView这也适用。

So this bit works when setting the image via a drawable or bitmap. I have also extended it so I can use it with Google's Volley NetworkImageView which also works.

我的问题是当我尝试和我们我CircularImageView类毕加索一起下载图像库,因为我看着它作为替代抽射高出。什么发生是在第一行中我loadBitmap()函数一个ClassCastException获取BitmapDrawable时

My problem comes when I try and us my CircularImageView class alongside Picasso image downloading library as I am looking at it as an alternative to Volley. What occurs is a ClassCastException in my loadBitmap() function on the first line when getting the BitmapDrawable.

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

    if(bitmapDrawable != null)
        image = bitmapDrawable.getBitmap();
}

Initally前毕加索已下载的图片便将占位符图像就好了。但是,一旦图像已被下载了毕加索失败与一个ClassCastException为getDrawable()返回和PicassoDrawable而不是BitmapDrawable。

Initally before Picasso has downloaded the picture it rounds the placeholder image just fine. But as soon as the image has been downloaded by Picasso it fails with a ClassCastException as getDrawable() returns and PicassoDrawable rather than a BitmapDrawable.

我想保持舍的形象在我CircularImageView类的OnDraw(画布)方法的工作,因为它是很好的遏制和自动,而不是每一次都设置在ImageView的毕加索做处理。这可能吗?

I would like to keep the work to round the image in the onDraw(canvas) method in my CircularImageView class as it is nicely contained and automatic as opposed to doing the process when setting up the ImageView with Picasso each and every time. Is this possible?

在此先感谢。

推荐答案

有关循环使用的图像毕加索,使用这个类实现转型。

For circular images using Picasso, use this class that implements Transformation.

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

这篇关于自定义ImageView的类不工作与毕加索的图像下载库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆