How to Clear an Imageview in Android

How to clear an ImageView in Android?

I used to do it with the dennis.sheppard solution:

viewToUse.setImageResource(0);

it works but it is not documented so it isn't really clear if it effects something else in the view (you can check the ImageView code if you like, i didn't).

I think the best solution is:

viewToUse.setImageResource(android.R.color.transparent);

I like this solution the most cause there isn't anything tricky in reverting the state and it's also clear what it is doing.

Clear Image in the Imageview

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

How to reset the resource of image view in android?

Just call it with a "0" parameter before doing anything else:

status_image.setImageResource(0)

How to clear ImageView correctly?

imgview.setImageResource(0);

or

imgview.setImageDrawable(null);

Clear/Reset entire ImageView canvas

Drawing shapes on Canvas in View.onDraw() is low-level function that directly changes pixels on the display in the final stage. So, if you once draw something, it's impossible to erase it. It's not buffered. There remain only mixed RGB(w/o A) pixels on the screen. The former pixel colors (originated from the "takenPicture") are already lost.

If you'd like to make shapes erasable, prepare another canvas backed by an ARGB-bitmap and draw all on it. Then draw the bitmap on the canvas in the end.

@Override
protected void onDraw(Canvas canvas)
{

// Prepare Bitmap and Canvas
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas draw_canvas = new Canvas(bitmap);

// Draw a rect.
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
draw_canvas.drawRect(new Rect(212,0,-720,600),paint);

// Clear
draw_canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC);

// Draw the bitmap.
canvas.drawBitmap(bitmap, 0, 0, null);
bitmap.recycle();
}

Delete/remove current image from ImageView?

Setting a resource to 0 did not work for me. The following did work though:

imageView.setImageBitmap(null);

Remove image set by Glide and use imageView.setImageBitmap()

After spending lots of time figuring out how to remove image set by Glide, and failing, decided to go old-school and first download the image in phone and set that image as bitmap in my application

asynchtask to download image

private class GetImages extends AsyncTask<Object, Object, Object>
{
private String requestUrl, imagename_;
private ImageView view;
private Bitmap bitmap;
private FileOutputStream fos;

private GetImages(String requestUrl, ImageView view, String _imagename_)
{
this.requestUrl = requestUrl;
this.view = view;
this.imagename_ = _imagename_;
}

@Override
protected Object doInBackground(Object... objects)
{
try
{
URL url = new URL(requestUrl);
URLConnection conn = url.openConnection();
bitmap = BitmapFactory.decodeStream(conn.getInputStream());
}
catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Object o)
{
view.setImageBitmap(bitmap);
}
}


Related Topics



Leave a reply



Submit