毕加索的圆角 [英] Rounded corners with Picasso

查看:69
本文介绍了毕加索的圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种合理的方法可以使毕加索的圆角化

Is there a reasonable way to do rounded corners with Picasso that

  1. 不会显着减慢绘图速度
  2. 使用硬件层
  3. 不为每个图像创建额外的位图
  4. 允许将下载的位图调整为目标图像视图的大小
  1. Doesn't significantly slow down drawing
  2. Works with hardware layers
  3. Doesn't create an extra bitmap for each image
  4. Allows resizing the downloaded bitmap into the size of the destination imageview

大多数关于圆角的毕加索建议都建议使用转换,但是我没有看到没有在转换中创建额外位图的示例.

Most of the picasso advice on rounded corners suggests that a transformation be used, but I haven't seen an example that doesn't create an extra bitmap as part of the transformation.

这似乎是因为Picasso仅使用位图,而做圆角的技巧则是可以有效地动态绘制圆角(大多数解决方案使用http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/).

This seems to be because Picasso only uses bitmaps, while the tricks to do rounded corners use the fact that you can dynamically draw the rounded corners on reasonably efficiently (most solutions use something along the lines of http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/).

使用Volley进行此操作有点棘手,但可以做到,只需将ImageView的类型更改为采用自定义可绘制对象的对象,即可绘制圆角.由于毕加索需要位图(至少只有一个位图->位图转换),因此不可行,因为可绘制对象到位图的转换会在过程中创建位图.

Doing this with Volley was a bit hacky but possible, by just changing the type of ImageView to something that took a custom drawable, which drew rounded corners. Since Picasso needs bitmaps (at least, there's only a bitmap -> bitmap transformation), this is out, since the conversion of the drawable to bitmap creates a bitmap in the process.

一种解决方案是自己修改分支中的毕加索作品,该作品中添加了位图->可绘制的变换,但是我想有一种更好的方法来解决这个问题.

One solution would be to do the work to modify picasso in a branch on my own that added a bitmap -> drawable transform, but I'd imagine there's a better way to go about this.

我不想在视图顶部绘制9个补丁来显示圆角.

I do not want to draw a 9-patch on top of a view to give the appearance of rounded corners.

推荐答案

我建议的答案是等待毕加索2.3,或者现在分叉他们的github,您实际上可以在其中获得BitmapDrawable.

the answer I would suggest would be to wait for Picasso 2.3, or fork their github now, where you can actually get at a BitmapDrawable.

到目前为止,我发现的一种方法是,可以将图像加载到Target对象中,以这种方式从位图创建自定义可绘制对象,然后将可绘制对象设置为ImageView,在该视图中无需创建新位图就可以绘制该对象

One approach I've found so far is that you can load images into a Target object, create a custom drawable from the bitmap that way, then set the drawable into the ImageView, where it'll draw without creating a new bitmap.

尽管如此,这种方法还是很糟糕的:

This approach sort of sucks for a few reasons though:

1)您必须管理目标对象.这些是弱引用(感谢),因此您必须自己跟踪它们. ck内存泄漏了.

1) You have to manage Target objects. These are weak-referenced (thankfully), so you have to keep track of them yourself. Ick. Memory leaks ahoy.

2)收到回调时,最好检查一下以确保世界状况仍然与图片相关,这是要使用毕加索避免的一部分.

2) When you get the callback, you had better check to make sure the state of the world is still relevant to the picture, which is part of what you want to avoid by using picasso.

简而言之,有些事情似乎阻止了更好的解决方案.

In short, there are a few things that seem to prevent a better solution.

1)毕加索将位图包装在PicassoDrawables中.这意味着您必须在自定义imageView中处理任意可绘制对象(如果走那条路),或者此类的特殊情况. 2)PicassoDrawable不会公开源位图,因此您必须将drawable转换为位图(需要创建一个新的位图afaict). 3)没有位图->可绘制的转换函数(有关原因,请参阅#1,很有可能).

1) Picasso wraps bitmaps in PicassoDrawables. This means you you have to handle arbitrary drawables in your custom imageView (if you go that route), or special case for this class. 2) PicassoDrawable doesn't expose the source bitmap, so you have to convert the drawable to a bitmap (requires creating a new bitmap, afaict). 3) There's no bitmap -> drawable transform function (see #1 for why, most likely).

很想听听我是否有什么缺失的消息,或者有人提出了更好的解决方案.现在,我最好的计划是要么执行上面建议的目标管理,要么派生毕加索的仓库,更改PicassoDrawable使其具有基础位图的公共访问器,然后在我的imageView中以这种方式转换为自定义可绘制的对象.

Would love to hear if there's something I'm missing, or someone has come up with a better solution. Right now my best plan is to either do the Target management proposed above, or fork the picasso repo, change PicassoDrawable to have a public accessor for the underlying bitmap, and do the conversion into a custom drawable that way in my imageView.

这篇关于毕加索的圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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