从 URI 加载图像作为布局背景
Load image from URI as layout background
我已经使用 Picasso 从我公司的 CDN 将图像加载到 ImageView
:
ImageView imgView;
//...
Picasso.with(context).load(Uri.parse(url)).into(imgView);
但现在我需要加载一张图片作为布局背景:
RelativeLayout theLayout;
//...
Picasso.with(context).load(Uri.parse(url)).into(...);
毕加索可以吗?如果不是,我应该使用 ImageView
而不是 Relativelayout
吗?
您可以使用 glide 下载位图并将其设置为任何布局的背景。
Glide
.with(getApplicationContext())
.load("https://www.google.es/images/srpr/logo11w.png")
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
Drawable dr = new BitmapDrawable(resource);
theLayout.setBackgroundDrawable(dr);
// Possibly runOnUiThread()
}
});
但更好的方法是在 relativelayout
之上使用 imageView
并使其成为 match_parent
并在此图像视图上显示图像。这将帮助您直接使用 glide 或 picaso 在图像视图中加载图像而不会出现内存错误。
Picasso.with(getActivity()).load(your url).into(new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
yourlayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
编辑:
您可能还需要覆盖以下方法
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.e("TAG", "Failed");
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.e("TAG", "Prepare Load");
}
}
是的。您可以为此使用 Picasso。请检查以下代码:
Picasso.with(getActivity()).load(Uri.parse(url)).into(new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
relativeLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
})
我已经使用 Picasso 从我公司的 CDN 将图像加载到 ImageView
:
ImageView imgView;
//...
Picasso.with(context).load(Uri.parse(url)).into(imgView);
但现在我需要加载一张图片作为布局背景:
RelativeLayout theLayout;
//...
Picasso.with(context).load(Uri.parse(url)).into(...);
毕加索可以吗?如果不是,我应该使用 ImageView
而不是 Relativelayout
吗?
您可以使用 glide 下载位图并将其设置为任何布局的背景。
Glide
.with(getApplicationContext())
.load("https://www.google.es/images/srpr/logo11w.png")
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
Drawable dr = new BitmapDrawable(resource);
theLayout.setBackgroundDrawable(dr);
// Possibly runOnUiThread()
}
});
但更好的方法是在 relativelayout
之上使用 imageView
并使其成为 match_parent
并在此图像视图上显示图像。这将帮助您直接使用 glide 或 picaso 在图像视图中加载图像而不会出现内存错误。
Picasso.with(getActivity()).load(your url).into(new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
yourlayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
编辑:
您可能还需要覆盖以下方法
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.e("TAG", "Failed");
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.e("TAG", "Prepare Load");
}
}
是的。您可以为此使用 Picasso。请检查以下代码:
Picasso.with(getActivity()).load(Uri.parse(url)).into(new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
relativeLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
})