And*_*oid 12 android android-layout picasso
任何人都可以指出一个例子,说明如何使用Picasso以编程方式更改XML布局的背景?我发现的所有示例都能够使用Picasso更新ImageView,但不能更新布局背景.
mma*_*ark 34
你可以使用毕加索的目标:
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) {
}
});
Run Code Online (Sandbox Code Playgroud)
UPDATE
正如@BladeCoder在评论中提到的那样,Picasso对Target对象有一个弱引用,因此很可能是垃圾回收.
因此,在杰克沃顿对其中一个问题的评论之后,我认为这可能是一个很好的方法:
CustomLayout mCustomLayout = (CustomLayout)findViewById(R.id.custom_layout)
Picasso.with(this).load("http://imageUrl").into(mCustomLayout);
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
我使用ImageView作为临时ImageHolder.首先,使用毕加索将图像加载到ImageView中,然后使用getDrawable从此ImageView设置布局背景.
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() {
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16692 次 |
| 最近记录: |