Java 更改应用程序背景动画的首选项

Java 更改应用程序背景动画的首选项,java,android,list,listpreference,Java,Android,List,Listpreference,我正在尝试在应用程序中进行设置,特别是ListPreference,以便能够更改应用程序的背景动画,但我无法这样做,如果选择选项1或选项2,则不会发生任何更改。。。我遵循了一个类似的教程,并试图让它为我工作,但到目前为止什么都没有。 这是java SharedPreferences backChooser = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); String values = backChoos

我正在尝试在应用程序中进行设置,特别是ListPreference,以便能够更改应用程序的背景动画,但我无法这样做,如果选择选项1或选项2,则不会发生任何更改。。。我遵循了一个类似的教程,并试图让它为我工作,但到目前为止什么都没有。 这是java

SharedPreferences backChooser = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String values = backChooser.getString("list", "1");
    img = (ImageView) findViewById(R.id.imageView1);
    img.setBackgroundResource(R.drawable.anim1);
    animation = (AnimationDrawable) img.getDrawable();
    animation.start();

    if (values.contentEquals("1")) {
        img.setBackgroundResource(R.drawable.anim1);
        animation = (AnimationDrawable) img.getDrawable();
        animation.start();
    }
    if (values.contentEquals("2")) {
        img.setBackgroundResource(R.drawable.anim2);
        animation = (AnimationDrawable) img.getDrawable();
        animation.start();
    }
下面是首选项的xml

<?xml version="1.0" encoding="utf-8"?>


这行代码

img.setBackgroundResource(R.drawable.anim1);
设置图像视图的“背景”

我们和这个代码一样

img.getDrawable();
img.getBackground();
获取ImageView的“可绘制”或其上的图像,而不是实际的“背景”图像

由于您希望检索您曾经设置为“img”背景的图像,因此下面的代码

img.getDrawable();
img.getBackground();

是您需要的。

您是否有机会在
onCreate()
中执行此操作?是的,为什么,我希望应用程序简单,只显示动画和声音查看我关于为什么以及如何解决的答案。这是一个动画,如果我使用getBackground执行此操作,动画不会运行