1

30秒ごとに背景画像を変更する必要がありますが、適切な結果が得られません

スレッドと TimerTask で試しました。しかし、仕事はありません。port1、port2...などのイメージがあります。

TimerTask ではバックグラウンドがなくなりました。

class Task1 extends TimerTask {
    public void run() {
        System.out.println("Checking a");
        Random r = new Random();
        int i = r.nextInt(3) + 1;
        rl.setBackgroundResource(getResources().getIdentifier(
                    "port" + i, "drawable","com.samcom.breakingdowncd"));

    }
}

Timer timerBackground = new Timer();
timerBackground.scheduleAtFixedRate(new Task1(), 0, 30000);

機能していません。どんな助けでも大歓迎です。ありがとう

4

2 に答える 2

3

これを使えばうまくいく

    private static final long GET_DATA_INTERVAL = 1000;
    int images[] = {R.drawable.cloud1,R.drawable.cloud2};
    int index = 0;
    ImageView img;
    Handler hand = new Handler();

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        img = (ImageView) findViewById(R.id.image);
        hand.postDelayed(run, GET_DATA_INTERVAL);
    }

    Runnable run = new Runnable() {
        @Override
        public void run() {
            img.setBackgroundDrawable(getResources().getDrawable(images[index++]));
            if (index == images.length)
                index = 0;
            hand.postDelayed(run, GET_DATA_INTERVAL);
        }
    };

XML コード:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/line">
    <ImageView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/image"
    />
</LinearLayout>
于 2011-05-28T07:18:20.297 に答える