首页 > 解决方案 > 如何使用 picasso 库从 url 设置 drawableleft 图像?

问题描述

我需要drawableLeft从 url 加载图像。这可能吗?

<TextView
     android:id="@+id/txt_check_opportunity"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerVertical="true"
     android:layout_marginLeft="0dp"                        
     android:drawableLeft="@drawable/ic_star_yellow_24dp"                        
     android:layout_toRightOf="@+id/checkbox_opportunity"
     android:text="@string/opportunity"
     android:textSize="@dimen/text_large_size"
     android:textStyle="bold" />

标签: picasso

解决方案


您需要创建一个 Picasso Target,将图像从 URL 加载到目标中,并使用来自Picasso Target 的回调调用setCompoundDrawablesWithIntrinsicBoundsTextView 。BitmaponBitmapLoaded

您还需要记住,毕加索目标被弱引用,因此您需要小心并保持对目标的强引用。

我已经实现了一个名为PicassoTargetableTextView的小部件,它可以完成所有这些工作。您可以PicassoTargetableTextView.load(left, top, right, bottom)在运行时使用 将可绘制对象加载到 TextView 中。


推荐阅读