我重用ImageViews为我的显示,但在某些时候,我没有值把它。

那么如何在Android中清除ImageView呢?

我试过了:

mPhotoView.invalidate();
mPhotoView.setImageBitmap(null);

他们都没有清除视图,它仍然显示以前的图像。


当前回答

如果你使用滑翔,你可以这样做。

Glide.with(yourImageView).clear(yourImageView)

其他回答

我以前和丹尼斯一起干过。谢泼德的解决方案:

viewToUse.setImageResource(0);

它可以工作,但它没有被记录,所以它并不清楚它是否会影响视图中的其他东西(如果你喜欢,你可以检查ImageView代码,我没有)。

我认为最好的解决办法是:

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

我最喜欢这个解决方案,因为在恢复状态方面没有任何棘手的问题,而且它也很清楚它在做什么。

我能指出你都试图设置和int它期望一个可绘制。

你不应该做以下事情吗?

imageview.setImageDrawable(this.getResources().getDrawable(R.drawable.icon_image));

imageview.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.icon_profile_image));

如果这些解决方案都没有清除你已经设置的图像(特别是setImageResource(0)或setImageResources(android.R.color.transparent),检查以确保当前图像没有设置为背景使用setBackgroundDrawable(…)或类似的东西。

你的代码只是将前景中的图像资源设置为你所设置的背景图像前面的一些透明的东西,并且仍然会显示。

我试图通过使用ImageView. setimageressource(0)来删除ImageView的图像,但它对我不起作用。 幸运的是,我在日志中看到了这条信息:

Caused by: java.lang.IllegalStateException: The specified child already has a parent. 
You must call removeView() on the child's parent first.

比如说,layoutmanager是你的布局实例,然后你需要那样做:

RelativeLayout layoutManager = new RelativeLayout(this);
ImageView image = new ImageView(this);

// this line worked for me
layoutManager.removeView(image);

我知道这很旧了,但我能用

viewToUse.setImageResource(0);

看起来不应该有用,但我一时兴起试了一下,效果很好。