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

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

我试过了:

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

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


当前回答

正如kwasi写的和golu编辑的,你可以使用透明,而不是白色:

文件可拉的/ transparent.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@android:color/transparent"/>
</shape>

在活动、视图等内部:

view.setImageResource(R.drawable.transparent);

其他回答

editText.setText (" ");

imageView.setImageResource(0);

我可以通过定义一个可绘制对象(类似于blank_white_shape.xml)来实现这一点:

<shape xmlns:android="http://schemas.android.com/apk/res/android"       
       android:shape="rectangle">
    <solid android:color="@android:color/white"/>
</shape>

当我想清除图像视图时,我只需调用

 imageView.setImage(R.drawable.blank_white_shape);

这对我来说非常有用!

对于ListView项目图像,您可以在列表适配器中设置ImageView.setVisibility(View.INVISIBLE)或ImageView.setImageBitmap(null),用于“无图像”情况。

我试图清除ImageView中的Image和DrawableCache

ImgView.setImageBitmap(null);
ImgView.destroyDrawingCache();

我希望这对你有用!

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

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