简体   繁体   中英

Is there a way to set icon(drawable) on button loaded from URL?

I'm trying to set an icon on my button which is loaded from URL. There is a method which works if the drawable already exists in R :

setCompoundDrawablesWithIntrinsicBounds();

but what should I do with image loading from web?

As Using Picasso You need to create a Picasso Target , load the image from the URL into the target and call setCompoundDrawablesWithIntrinsicBounds() on the TextView with the Bitmap from the onBitmapLoaded callback of Picasso Target.

You need to also keep in mind that the Picasso Targets are weakly referenced so you need to be careful and keep strong references to your target.

Here is implemented a widget called PicassoTargetableTextView that does all of that. You can use the PicassoTargetableTextView.load(left, top, right, bottom) to load drawables into the TextView at runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM