简体   繁体   中英

Release ImageView background resources in android

i am using some ImageView in XML file and setting backgroundResources of them.

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

Now i am changing background resources from codding. like this :

imageview.setBackgroundResource(R.drawable.small_red);

Now when i exit from this screen and enter again it show me red image. but i want to show default image (which i use in XML file). So Please tell me that how can i release red image so that we screen again loads it sets imageview with default image.

Thanks.

I recommend you to change the background of the images fully programmatically.

If you do it like the code you posted, every time you open your app, you call onCreate method and you will setContentView (load your layout) again with the grey values.

One way is to use SharedPrefferences to store the color value every time you change it, and when you open your app you check the SharedPrefferences for the color.

If SharedPrefferences are null , means there is no color change and you set the default grey color. If there is a color stored, take it and set it to the image/images you need.

Hope it helps! Good Luck!

Add below methods.

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

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

You need to reset the imageview while exiting from activity and again at the time of resuming you need to set defalt image. So above two call back methods will accomplish your aim.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM