仅限XML TypedArray显示第一个图像

时间:2016-06-06 17:07:32

标签: java android xml android-drawable typed-arrays

这是我第一次尝试使用TypedArrays,我相信我已经正确编写了代码,但出于某种原因,我只能看到数组中的第一个图像。我有一个应该重复显示图像的循环。

我的activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".HomeScreen"
    android:background="@android:drawable/screen_background_dark_transparent"
    android:clickable="true"
    android:id="@+id/homescreen_view">

    <Button
        android:layout_width="100.0dp"
        android:layout_height="25.0dp"
        android:text="Help"
        android:id="@+id/instructionsButton"
        android:layout_alignParentTop="false"
        android:layout_alignParentStart="true"
        android:textColor="#05ffda"
        android:background="@android:color/holo_purple"
        android:longClickable="true" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:scaleType="centerCrop"
        android:id="@+id/soundEnable_button"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:longClickable="true"
        android:nestedScrollingEnabled="true"/>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="25dp"
        android:text="Highscores"
        android:id="@+id/highscores_button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_purple"
        android:textColor="#05ffda" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:text="Play Game"
        android:id="@+id/startGame_button"
        android:background="@android:color/holo_purple"
        android:textColor="#05ffda"
        android:layout_marginTop="175dp"
        android:singleLine="true"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="musicIcon">
        <item>@drawable/musicoff</item>
        <item>@drawable/musicon1</item>
        <item>@drawable/musicon2</item>
        <item>@drawable/musicon3</item>
        <item>@drawable/musicon4</item>
        <item>@drawable/musicon5</item>
    </array>
</resources>

和我的activity.java

import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class HomeScreen extends AppCompatActivity {

    ImageButton musicEnable_ImageButton;
    Handler musicIcon_animate = new Handler();
    int ArrayPos = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);


        final TypedArray musicIcons = getResources().obtainTypedArray(R.array.musicIcon);
        for(ArrayPos =0;ArrayPos<5;ArrayPos++) {
            musicIcons.getResourceId(ArrayPos, -1);
        }
        ArrayPos = 0;
        musicEnable_ImageButton = (ImageButton) findViewById(R.id.soundEnable_button);
        musicEnable_ImageButton.setImageResource(musicIcons.getResourceId(ArrayPos,-1));
        musicIcons.recycle();
        musicEnable_ImageButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                ArrayPos = 1;
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        musicEnable_ImageButton.setImageResource(musicIcons.getResourceId(ArrayPos,-1));
                        musicIcons.recycle();
                        if(ArrayPos < 5) {
                            ArrayPos++;
                        }
                        else{
                            ArrayPos = 1;
                        }
                        Log.d("test", "timer fired");
                        musicIcon_animate.postDelayed(this, 1);
                    }
                };

                musicIcon_animate.postDelayed(runnable, 1);

            }
        });
    }

1 个答案:

答案 0 :(得分:0)

您只能看到第一张图片,因为您在设置第一张图片后正在回收musicIcons TypedArrayTypedArray.recycle()删除对数组中资源的所有引用,使其成为空数组。您想要这样做,但只有在您从阵列中获得所需内容之后才能执行此操作。

在您的情况下,由于您在一段时间内使用资源,我建议您这样做:

List<Integer> musicIcons = new ArrayList<>();

final TypedArray typedArray = getResources().obtainTypedArray(R.array.musicIcon);

for(int i = 0; i < typedArray.length(); i++) {
    musicIcons.add(typedArray.getResourceId(i, -1);
}

typedArray.recycle();

然后像这样设置ImageView资源:

musicEnable_ImageButton.setBackgroundResource(musicIcons.get(ArrayPos));

但是,我也注意到,您似乎只是试图为图标设置动画,在这种情况下,您应该考虑使用AnimationDrawable。通过这种方式,您可以创建一个可绘制的XML文件:

<animation-list android:id="@+id/selected" android:oneshot="true">
    <item android:drawable="@drawable/musicoff" android:duration="50" />
    <item android:drawable="@drawable/musicon1" android:duration="50" />
    <item android:drawable="@drawable/musicon2" android:duration="50" />
    <item android:drawable="@drawable/musicon3" android:duration="50" />
    <item android:drawable="@drawable/musicon4" android:duration="50" />
    <item android:drawable="@drawable/musicon5" android:duration="50" />
 </animation-list>

然后将图像资源设置为此animation-list,并为其设置动画:

musicEnable_ImageButton.setImageResource(R.id.sound_button);

AnimationDrawable frameAnimation = (AnimationDrawable) musicEnable_ImageButton.getBackground();

frameAnimation.start();

在API 21+上更容易,您可以创建AnimatedStateListDrawable来包装animation-list(或列表)。然后将android:button的{​​{1}}属性设置为RadioButton,并在调用AnimatedStateListDrawable时观看其动画。