在android中发布ImageView后台资源

在android中发布ImageView后台资源,android,imageview,Android,Imageview,我正在XML文件中使用一些ImageView并设置它们的背景资源 <ImageView android:id="@+id/anim0image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="115px" android:layout_marginTop="15px"

我正在XML文件中使用一些ImageView并设置它们的背景资源

    <ImageView
    android:id="@+id/anim0image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="115px"
    android:layout_marginTop="15px"       
    android:background="@drawable/small_gray"
    android:visibility="visible" />

<ImageView
    android:id="@+id/anim1image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="160px"
    android:layout_marginTop="15px"
    android:background="@drawable/small_gray"
    android:visibility="visible" />
现在,当我退出此屏幕并再次进入时,它会显示红色图像。但我想显示默认图像(在XML文件中使用)。所以请告诉我,我如何才能释放红色图像,使我们的屏幕再次加载它设置默认图像的imageview


谢谢。

我建议您完全以编程方式更改图像的背景

如果你喜欢你发布的代码,每次打开你的应用程序时,你调用
onCreate
方法,你将
setContentView
(加载你的布局)再次使用灰色值

一种方法是在每次更改颜色值时使用
SharedEffects
来存储颜色值,当打开应用程序时,检查
SharedEffects
中的颜色

如果
SharedEffects
null
,则表示没有颜色变化,您可以设置默认的灰色。如果存储了颜色,则将其设置为所需的图像

希望有帮助!祝你好运

添加以下方法

@override
protected void onPause()
{
    imageview.setBackgroundResource(null);
}

@override
protected void onResume()
{
    imageview.setBackgroundResource(R.drawable.small_gray);
}

您需要在退出活动时重置imageview,在恢复活动时,您需要再次设置defalt image。因此,以上两种回调方法将实现您的目标。

您何时更改图像资源??在任何单击事件中?使用计时器更改图像资源。每秒钟更改一次。尝试重新设置ImageView以使用“onResume”ImageView.setBackgroundResource(R.drawable.small_gray)上的“small_gray”;方法,或使用“onPause”方法完成屏幕,以确保当我们返回屏幕时,您的ImageView将重新优化。void onPause(){this.finish();}@Eslam Yousef:我在xml file.ok中设置了默认的ImageView资源,但是您在代码的某个地方更改了它,因此,您需要在恢复屏幕时重新设置它,或者销毁屏幕,以便在重新访问时使用xml文件中的值再次初始化它。
@override
protected void onPause()
{
    imageview.setBackgroundResource(null);
}

@override
protected void onResume()
{
    imageview.setBackgroundResource(R.drawable.small_gray);
}