Java 从图像视图 Android 中删除图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6643432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:33:39  来源:igfitidea点击:

Remove the image from a imageview Android

javaandroidbitmapimageview

提问by ChyBy

I'm trying to make an ImageViewthat holds a gallery of images. By touching the user request to load the next image. If the next image isn't found in the server or takes time to load I need the old image to be empty.

我正在尝试制作一个ImageView包含图像库的图像。通过触摸用户请求加载下一张图片。如果在服务器中找不到下一个图像或需要时间加载,我需要旧图像为空。

setVisibility(View.GONE)or setVisibility(View.INVISIBLE)don't work for me because when invisible/gone I stop the onTouch()detecting (and the user is locked to current image).

setVisibility(View.GONE)或者setVisibility(View.INVISIBLE)对我不起作用,因为当隐形/消失时我停止onTouch()检测(并且用户被锁定到当前图像)。

How can I make the ImageViewto load a empty bitmap or clear (remove) current bitmap?

如何使ImageView加载空位图或清除(删除)当前位图?

回答by Ribose

Try:

尝试:

imageView.setImageResource(0);

This will set the image view to use no resource.

这将设置图像视图不使用资源。

回答by james

Certainly imageView.setImageResource(0) works. It has never failed for me and I have used it many times.

当然 imageView.setImageResource(0) 有效。它对我来说从来没有失败过,我已经使用过很多次了。

setImageResource is usually passed the reference R.drawable,(the reference for the picture), which is stored as an int, but displayed in the R.java class as a hex value, 0xf2fs... So assuming this reference exist it will show a picture, if you later pass that same imageview a reference which does not exist the old picture will no longer show. So, if you pass it 0, or 5 or an int which does not match a resource referenced in your R.java class it will remove the picture completely from the src of the imageView. So if you are passing 0 to the old reference of the imageView.

setImageResource 通常传递引用 R.drawable,(图片的引用),它存储为 int,但在 R.java 类中显示为十六进制值,0xf2fs...因此假设此引用存在,它将显示一张图片,如果您稍后传递相同的图像视图,则旧图片将不再显示不存在的参考。因此,如果您传递给它 0、5 或一个与 R.java 类中引用的资源不匹配的 int,它将从 imageView 的 src 中完全删除图片。因此,如果您将 0 传递给 imageView 的旧引用。

回答by tristan2468

I always use

我总是用

imageView.setImageDrawable(null);

回答by Variag

From what I've noticed, the "working" or not of certain method when clearing image depends on the method used to populate ImageView.

据我所知,清除图像时某些方法的“工作”与否取决于用于填充 ImageView 的方法。

So if you set img.setImageBitmap(bmp)then to clear you should use img.setImageBitmap(null). When you img.setImageResource(resId)then to clear you should use img.setImageResouce(0). Etc.

所以如果你设置img.setImageBitmap(bmp)然后清除你应该使用img.setImageBitmap(null). 当你img.setImageResource(resId)然后清除你应该使用img.setImageResouce(0). 等等。