有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试在应用程序中进行设置,特别是一个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"?>

<ListPreference
    安卓:entries="@array/list"
    安卓:entryValues="@array/listvalues"
    安卓:key="list"
    安卓:summary="This is a list to choose from"
    安卓:title="List" />


共 (1) 个答案

  1. # 1 楼答案

    这行代码

    img.setBackgroundResource(R.drawable.anim1);
    

    设置ImageView的“背景”

    就像这个代码一样

    img.getDrawable();
    

    获取ImageView的“可绘制”或其上的图像,而不是实际的“背景”图像

    由于您想检索您曾经设置为“img”背景的图像,以下代码

    img.getBackground();
    

    就是你需要的