有没有人能给我指出如何使用Picasso编程方式更改XML布局的背景的示例? 我找到的所有示例都可以使用Picasso更新ImageView,但不能更改布局背景。
有没有人能给我指出如何使用Picasso编程方式更改XML布局的背景的示例? 我找到的所有示例都可以使用Picasso更新ImageView,但不能更改布局背景。
您可以使用 Picasso 的 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) {
}
});
更新
正如评论中@BladeCoder提到的,Picasso对目标对象持有弱引用,因此很可能被垃圾回收。
因此,根据其中一个问题上Jake Wharton的评论,我认为这可能是一个不错的解决方法:
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
}
}
Target
的强引用,否则在图片加载之前它可能会被垃圾回收,因为Picasso对Targets使用了弱引用。 - BladeCoder ImageView img = new ImageView(this);
Picasso.with(this)
.load(imageUri)
.fit()
.centerCrop()
.into(img, new Callback() {
@Override
public void onSuccess() {
myLayout.setBackgroundDrawable(img.getDrawable());
}
@Override
public void onError() {
}
});
以上的解决方案都不适用于我。但是@Thiha的解决方案最接近。以下内容对我有效:
final ImageView img = new ImageView(this);
Picasso.with(img.getContext()).load(url).into(img, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
collapsingToolbarLayout.setBackgroundDrawable(img.getDrawable());
}
@Override
public void onError() {
}
});
private RelativeLayout chatBG;
//初始化你的布局
chatBG = findViewById(R.id.chat_activity);
//使用Picasso库,最好在onStart()方法中使用以避免一些错误
Picasso.get().load("https://your/url")
.placeholder(R.drawable.yourplaceholder_img)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
chatBG.setBackground(new BitmapDrawable(bitmap));
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Toast.makeText(ChatActivity.this, "Error : loading wallpaper", Toast.LENGTH_SHORT).show();
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
<ImageView
android:id="@+id/imageView_background"
android:layout_width="match_parent"
android:contentDescription="@string/background"
android:layout_height="match_parent"
android:alpha="0.7"/>
Kt:
Picasso.get().load(R.drawable.background_green_forest).fit().centerCrop().into(view.imageView_background)
android:scaleType="fitXY"