问题描述
任何人都可以点我朝着人们可能如何使用编程毕加索更改XML布局的背景的例子吗?我发现所有的例子都能够使用更新的毕加索ImageView的 - 但不是一个布局的背景。
Can anyone point me towards an example of how one might change an XML layout's background programatically using Picasso? All the examples I've found are able to update an ImageView using Picasso - but not a layout background.
Unable使用毕加索从URL设置的LinearLayout BackgroundResource
Unable to set LinearLayout BackgroundResource from URL using Picasso
推荐答案
您可以用毕加索的目标:
You can use Picasso's Target:
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) {
}
});
更新
由于在评论中提及@刀片codeR,毕加索拥有的弱引用的目标对象,因此它很可能是垃圾回收。
As @BladeCoder mentioned in the comment, Picasso holds a weak reference to Target objects, hence it is likely to be garbage collected.
所以,关于的问题之一以下,我认为这可能是去一个好办法:
So, following Jake Wharton's comment on one of the issues, i think this could be a good way to go:
CustomLayout mCustomLayout = (CustomLayout)findViewById(R.id.custom_layout)
Picasso.with(this).load("http://imageUrl").into(mCustomLayout);
CustomLayout.java:
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
}
}
这篇关于如何加载布局背景使用毕加索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!