È possibile utilizzare target di Picasso:
Picasso.with(this).load("http://imageUrl").into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
mYourLayout.setBackground(new BitmapDrawable(bitmap));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
UPDATE
Come @BladeCoder menzionato nel commento, Picasso contiene un riferimento debole per target gli oggetti, quindi è probabile essere spazzatura raccolta
Così, seguendo Jake Wharton's comment su uno dei problemi, credo che questo potrebbe essere un buon modo per andare:
CustomLayout mCustomLayout = (CustomLayout)findViewById(R.id.custom_layout)
Picasso.with(this).load("http://imageUrl").into(mCustomLayout);
CustomLayout.java:
public class CustomLayout extends LinearLayout implements Target {
public CustomLayout(Context context) {
super(context);
}
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
setBackground(new BitmapDrawable(getResources(), bitmap));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
//Set your error drawable
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
//Set your placeholder
}
}
Il codice di questo esempio potrebbe non sempre funziona: si * deve * mantenere un riferimento concreto al vostro 'Target' o può essere garbage collection prima che l'immagine viene caricata perché Picasso mantiene debole riferimenti a obiettivi. – BladeCoder
@BladeCoder Hai ragione. Sto modificando la mia risposta per risolvere il problema. Grazie – mmark
Sto avendo un po 'di problemi usando Picasso's Target come hai mostrato sopra ... (qualsiasi assistenza è apprezzata) http://pastebin.com/uY2LnSDV – AndroidAndroidAndroid