set bitmap to background of ImageView with imageVi

2020-05-17 00:04发布

问题:

I have an ImageView. I am using imageView.setImageBitmap to set my image as background to ImageView. But it sets my image to ImageView's source (i.e. android:src) , but it doesn't set my image to ImageView's background (i.e. android:background).

When I use imageView.setImageBitmap, it looks like as if I used imageView.setImageResource not imageView.setBackgroundResource.

How can I handle this if I want to set my image to background using imageView.setImageBitmap. I know by I can do this by making custom ImageView. But is it possible without custom ImageView? If its possible, please let me know how to do it.

回答1:

I have tested and it's done

I think you are getting Bitmap

So you have to convert Bitmap to BitmapDrawable

like

  BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap)

then just set bitmap with below function

  imageView.setBackground(ob);

by this way you can do it..



回答2:

Try following code to set your own bitmap as background.

Bitmap bitmap = ...;
ImageView imageView = ...;
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setBackground(drawable);


回答3:

try this..

Create drawable using bitmap & use setBackgroundDrawable(Drawable d) method for imageview.

Drawable d = new BitmapDrawable(getResources(),bitmap);

imageview.setBackgroundDrawable(d);


回答4:

Please Use the following line to set image background.

imageview.setBackground(getResources().getDrawable(R.drawable.uporder));

Here uporder is an image resource present in your drawablefolder.



回答5:

I prefer to use this because is not deprecated and it works in the lowers versions of android.

ImageView imageView;
Bitmap bitmap;
Drawable drawable = new BitmapDrawable(getResources(),bitmap);
imageView.setImageDrawable(drawable);


回答6:

call setBackgroundDrawable withe BitmapDrawable param