在android中发布ImageView后台资源

时间:2014-04-16 12:33:27

标签: android imageview

我在XML文件中使用了一些ImageView并设置了它们的backgroundResources。

    <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" />

现在我正在改变编码的背景资源。像这样:

imageview.setBackgroundResource(R.drawable.small_red);

现在,当我从此屏幕退出并再次输入时,它会显示红色图像。但我想显示默认图像(我在XML文件中使用)。所以请告诉我如何释放红色图像,以便我们再次加载屏幕,然后使用默认图像设置imageview。

感谢。

2 个答案:

答案 0 :(得分:0)

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

如果您按照发布的代码执行此操作,则每次打开应用时,都会调用onCreate方法,然后使用灰色值再次setContentView(加载布局)。

一种方法是每次更改时使用SharedPrefferences存储颜色值,当您打开应用时,请检查SharedPrefferences颜色。

如果SharedPrefferencesnull,则表示没有颜色更改,您设置了默认的灰色。如果存储了颜色,请将其设置为您需要的图像/图像。

希望它有所帮助!祝你好运!

答案 1 :(得分:0)

添加以下方法。

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

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

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

相关问题