set bitmap to background of ImageView with imageView.setImageBitmap method - android

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.

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..

Try following code to set your own bitmap as background.
Bitmap bitmap = ...;
ImageView imageView = ...;
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setBackground(drawable);

try this..
Create drawable using bitmap & use setBackgroundDrawable(Drawable d) method for imageview.
Drawable d = new BitmapDrawable(getResources(),bitmap);
imageview.setBackgroundDrawable(d);

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.

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

call setBackgroundDrawable withe BitmapDrawable param

Related

How to set an image in the background of custom view

How can one set image from uri to the background of custom view without causing the stackoverflow error. As I couldn't find any appropriate answers, I hope to find it here.
Get that image via Picasso / Glide libraries as a Bitmap
Convert Bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
Set drawable to your custom view
yourCustomView.setBackground(drawable);

Converting bitmap to drawable in Android

I'm downloading the image from server and storing it in bitmap object. I want to set this image as background for button. But the button doesn't have the property setImageBitmap. So is there anyway I can set the background of button with the downloaded bitmap image? Like by converting the bitmap to drawable? Sorry, I'm new to Android, please bear with my mistakes (if any).
P.S : I want to use button control only. Because I want some text at the bottom of each buttons and I'm creating these buttons dynamically.
The best way to convert a Bitmap to drawable in android is as follows,
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
where bitmap is the name the Bitmap. Then set the drawable as your Button background as follows,
btn.setBackground(drawable);
N.B: Without specifying getResources() as the first argument, you may experience inconsistent image sizing across different screen densities.
for more info refer this
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
Simply use BitmapDrawable.
Drawable drawable=new BitmapDrawable(contact_pic);
Convert Bitmap to BitmapDrawable like this
Drawable drawable = new BitmapDrawable(getResources(),your_bitmap);
and then set it as background to button as ,
button.setBackgroundDrawable(drawable);
P.S. You can also do the same inline as ,
button.setBackgroundDrawable(new BitmapDrawable(getResources(),your_bitmap));
This is how to do it, I've used several times:
Drawable drawable = (Drawable)new BitmapDrawable(Bitmap);
Button button= new Button(this);
button.setBackground(drawable);

Clear Image in the Imageview

I went through many threads but i could not get the answer yet .
I am setting an image to the imageView programmatically as
imageview.setBackgroundResource(R.Drawable.image);
if i set image as the above , will the image get cleared if i give
imageview.setImageDrawable(null);
what does imageview.setBackgroundDrawable(null) meant ?
What is the difference between
imageview.setImageDrawable(null);
and
imageview.setImageBitmap(null);
and
imageview.setBackgroundDrawable(null);
setBackgroundDrawable() is deprecated. You should use setBackground instead.
Basically the difference is the parameter. In setImageBitmap() you have to pass a Bitmap object. In setImageDrawable() you have to pass a Drawable object. setBackground just change the background of the imageview.
The imageview can have a background and a image content. If you are defining the background and want to clear the imageview, you should use setBackground(null).
Did you tried with :
imageview.setImageResource(android.R.color.transparent);
I had same problem. Here,
imageview.setImageDrawable(null);
will clear the the background of an ImageView,
imageview.setImageBitmap(null);
will clear the ImageBitmap from an ImageView, and
imageview.setBackgroundResource(R.Drawable.image);
will set the Images for your path e.g your path (R.Drawable.image)
There are many ways to inflate & clear ImageView
If you have inflated using,
imageView.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.xyz));
then clear using,
imageView.setImageDrawable(null);
If you have inflated using,
imageView.setBackgroundResource(R.drawable.xyz);
then clear using,
imageView.setBackground(null);
So basically if you inflate ImageDrawable, then you remove ImageDrawable.
If you inflate backgroung, then you remove background. Similarly for ImageBitmap.
setImageBitmap(Bitmap bm) : Sets a Bitmap as the content of this
ImageView
setImageDrawable(Drawable drawable): Sets a drawable as the
content of this ImageView

How to prevent an image from being resized in Android

I am writing an application on android. I am downloading an image from the server and trying to set it as an ImageView background using setBackgroundDrawable.
Drawable drawable = Drawable.createFromPath(path)
imageView.setBackgroundDrawable(drawable);
It works but the image is resized (it is getting smaller). Does anyone have an idea about how to set an image view background without changing its size?
Edit:
I solved the problem by using:
File imgFile = new File(path);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
imageView.setScaleType (ImageView.ScaleType.MATRIX)
Set image scale type to Matrix it will scale using the image matrix when drawing.
Use these two properties:
android:scaleType
android:adjustViewBounds
Study about them in : ImageView

Android: How to: set the Image of ImageSwitcher by a Bitmap? setImageBitmap doesn't exist?

i can't find setImageBitmap from ImageSwitcher. is there a way to set it by a bitmap?
UPDATES1
then i found this -> set Image from file URI
But my case is, i have to draw something in the bitmap before it set to ImageSwitcher. So, is there no way to set it via bitmap? if no way, i have to output the image file from modified bitmap then use setImageURI. But this is wasting of memory.
UPDATES2
Alternative: Is there a way to dynamically store the image file from sdcard to R.drawable or R.raw to generate resource/drawable id. Then use it to setImageResource or setImageDrawable?
you can convert bitmap into drawable and assign drawable to image switcher as
Drawable drawable =new BitmapDrawable(bitmap);
mSwitcher.setImageDrawable(drawable);
You can wrap your Bitmap in BitmapDrawable and use ImageSwitcher.setImageDrawable.

Categories

Resources