Load image from URI as layout background

I used Picasso to upload images from my company CDN to ImageView :

 ImageView imgView; //... Picasso.with(context).load(Uri.parse(url)).into(imgView); 

But now I need to load the image as the layout background:

 RelativeLayout theLayout; //... Picasso.with(context).load(Uri.parse(url)).into(...); 

Is this possible with Picasso? If not, should I use ImageView instead of Relativelayout ?

+5
source share
3 answers
  Picasso.with(getActivity()).load(your url).into(new Target(){ @Override public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) { yourlayout.setBackground(new BitmapDrawable(context.getResources(), bitmap)); } 

edit:

you may also need to override the following methods

 @Override public void onBitmapFailed(final Drawable errorDrawable) { Log.e("TAG", "Failed"); } @Override public void onPrepareLoad(final Drawable placeHolderDrawable) { Log.e("TAG", "Prepare Load"); } } 
+1
source

you can use glide to download a bitmap and set it as a background from any layout.

 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() } }); 

but it’s better to use imageView on top of relativelayout and make it match_parent and show the image on this image. this will help you directly use slide or picaso to load the image into the image without memory errors.

+3
source

Yes. You can use Picasso for this. Please check the following code:

 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"); } }) 
+1
source

Source: https://habr.com/ru/post/1274326/


All Articles