Clear Image in the Imageview - android

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

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

set bitmap to background of ImageView with imageView.setImageBitmap method

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

Deep copy of a Drawable

I have an ImageView. In its onClick I get its Drawable:
Drawable dr = ((ImageView) v).getDrawable();
And set it to a dialog's ImageView:
zoomedImage.setImageDrawable(dr);
But when I close the dialog or the activity is resumed. The image at the original position gets stretched and is shown larger than its size, leading to only a portion of the image is visible in the ImageView.
Is this a case of deep copy or there is another problem?
If it is, how can do I deep copy the original Drawable so that I could set the copy to zoomed image?
Thanks in advance.
Finally I succeed!
I had similar problem, when I used color filter on my drawable it changed the drawable, its very close to the solution of the other people here, but only this worked for me:
Drawable drwNewCopy = dr.getConstantState().newDrawable().mutate();
I managed to copy the drawable using following code:
drawable.mutate().getConstantState().newDrawable();
Here mutate() makes the drawable mutable to avoid sharing its state, and getConstantState().newDrawable() creates a new copy.
Thus different ImageViews use different drawables and there's no stretching.
Use BitmapFactory to convert the drawable into bitmap separately make or perform changes on it.
The above solutions won't work for me, But it works
val myDrawable = DrawableCompat.wrap(view.background).mutate() as GradientDrawable
myDrawable.setColor(ContextCompat.getColor(view.context, R.color.White))

Disabling background image shrinking in Android

It seems background images are automatically shrunk in Android, for example, I use setBackgroundDrawable to set background of a view:
Drawable background = getBackground();
myView.setBackgroundDrawable(background);
Instead of shrinking, I want the image to be cropped to fit the screen size. How to do it? Thanks.
Finally I fixed this problem. I use BitmapDrawable instead of Drawable, since BitmapDrawable has method setGravity(int) which can set the gravity to position/stretch the object.

Categories

Resources