我正在使用picasso库从服务器加载图像。我可以使用Picasso将背景图像设置为ImageView。但我无法使用相同的方式设置背景图像。我也尝试了不同的方法来将Image设置为活动布局背景。 Plaese帮帮我。
我的代码如下:
Picasso.with(getApplicationContext()).load("https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg").into(new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Start Loading", Toast.LENGTH_SHORT).show();
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
// TODO Auto-generated method stub
loginLayout.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
}
@Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Failed Loading", Toast.LENGTH_SHORT).show();
}
});
答案 0 :(得分:1)
尝试使用setBackgroundDrawable而不是setBackground
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
// TODO Auto-generated method stub
loginLayout.setBackgroundDrawable(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
}
答案 1 :(得分:0)
尝试使用目标
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
activityLayout.setBackgroundDrawable(new BitmapDrawable(context.getResources(), bitmap));
}
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
}
};
Picasso.with(getActivity()).load(url).into(target);