如何清除 Android 中的 ImageView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2859212/
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
How to clear an ImageView in Android?
提问by Pentium10
I am reusing ImageView
s for my displays, but at some point I don't have values to put it.
我正在将ImageView
s 重用于我的显示器,但在某些时候我没有值来放置它。
So how to clear an ImageView
in Android?
那么如何ImageView
在Android中清除一个?
I've tried:
我试过了:
mPhotoView.invalidate();
mPhotoView.setImageBitmap(null);
None of them have cleared the view, it still shows previous image.
他们都没有清除视图,它仍然显示以前的图像。
回答by Mario Lenci
I used to do it with the dennis.sheppardsolution:
我曾经用dennis.sheppard解决方案做到这一点:
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 codeif you like, i didn't).
它有效,但没有记录,因此不清楚它是否会影响视图中的其他内容(如果您愿意,可以检查ImageView 代码,我没有)。
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.
我最喜欢这个解决方案,因为在恢复状态时没有任何棘手的事情,而且它在做什么也很清楚。
回答by Kon
I had a similar problem, where I needed to basically remove ImageViews from the screen completely. Some of the answers here led me in the right direction, but ultimately calling setImageDrawable() worked for me:
我有一个类似的问题,我需要从屏幕上完全删除 ImageViews。这里的一些答案使我朝着正确的方向前进,但最终调用 setImageDrawable() 对我有用:
imgView.setImageDrawable(null);
(As mentioned in the comment, such usage is documented in the official docs: ImageView#setImageDrawable.)
(如评论中所述,官方文档中记录了这种用法:ImageView#setImageDrawable。)
回答by dennis.sheppard
I know this is old, but I was able to accomplish this with
我知道这很旧,但我能够做到这一点
viewToUse.setImageResource(0);
Doesn't seem like it should work, but I tried it on a whim, and it worked perfectly.
似乎它不应该起作用,但我一时兴起尝试了它,并且效果很好。
回答by Dan Lew
It sounds like what you want is a default image to set your ImageView to when it's not displaying a different image. This is how the Contacts application does it:
听起来你想要的是一个默认图像,当它不显示不同的图像时,将你的 ImageView 设置为。这就是联系人应用程序的工作方式:
if (photoId == 0) {
viewToUse.setImageResource(R.drawable.ic_contact_list_picture);
} else {
// ... here is where they set an actual image ...
}
回答by nandha
I tried this for to clear Image and DrawableCache in ImageView
我试过这个来清除 ImageView 中的 Image 和 DrawableCache
ImgView.setImageBitmap(null);
ImgView.destroyDrawingCache();
I hope this works for you !
我希望这对你有用!
回答by Brad English
If none of these solutions are clearing an image you've already set (especially setImageResource(0)
or setImageResources(android.R.color.transparent)
, check to make sure the current image isn't set as background using setBackgroundDrawable(...)
or something similar.
如果这些解决方案都没有清除您已经设置的图像(特别是setImageResource(0)
或setImageResources(android.R.color.transparent)
,请检查以确保当前图像没有设置为背景使用setBackgroundDrawable(...)
或类似的东西。
Your code will just set the image resource in the foreground to something transparent in front of that background image you've set and will still show.
您的代码只会将前景中的图像资源设置为您设置的背景图像前面的透明内容,并且仍然会显示。
回答by Ajay
Hey i know i am extremely late to this answer, but just thought i must share this,
嘿,我知道我对这个答案太晚了,但只是想我必须分享这个,
The method to call when u reset the image must be the same method that u called while u were setting it.
重置图像时调用的方法必须与设置图像时调用的方法相同。
When u want to reset the image source @dennis.sheepardanswer works fine only if u are originally setting the image in the bitmap using setImageResource()
当您想重置图像源时 @dennis.sheepard仅当您最初使用setImageResource()
for instance,
例如,
i had used setImageBitmap()
and hence setting setImageResource(0)
didn work, instead i used setImageBitmap(null)
.
我已经使用过setImageBitmap()
,因此设置setImageResource(0)
不起作用,而是我使用了setImageBitmap(null)
.
回答by Mojtaba Haddadi
if you use glide you can do it like this.
如果您使用滑翔,您可以这样做。
Glide.with(yourImageView).clear(yourImageView)
回答by kwasi
I was able to achieve this by defining a drawable (something like blank_white_shape.xml):
我能够通过定义一个可绘制对象(类似于 blank_white_shape.xml)来实现这一点:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white"/>
</shape>
Then when I want to clear the image view I just call
然后当我想清除图像视图时,我只需调用
imageView.setImage(R.drawable.blank_white_shape);
This works beautifully for me!
这对我来说效果很好!
回答by Arvis
For ListView item image you can set ImageView.setVisibility(View.INVISIBLE)
or ImageView.setImageBitmap(null)
in list adapter for "no image" case.
对于 ListView 项目图像,您可以为“无图像”情况设置ImageView.setVisibility(View.INVISIBLE)
或ImageView.setImageBitmap(null)
在列表适配器中。