如何在ImageButtons上一个接一个地更改图像?

时间:2011-08-17 18:52:14

标签: android animation imagebutton

我正在尝试更改按钮上的图像并将它们转回原始图像,并在4个不同的图像中依次进行。

我已经尝试了以下代码,但它不起作用,结果只导致其中一个图像闪烁一毫秒:

ArrayList<Integer> scenario = new ArrayList<Integer>();

...

void delayedPlay(){
    // each button should be posted in 1 second spacing
    int count = 1;
    for (final int btnid : scenario){
        // turn off
        final Runnable r2 = new Runnable(){
            public void run(){ 
                imagebuttons[btnid].setImageBitmap(imagesTurnedOff.get(btnid)); 
                }
        };

        // turn on and call turn off
        Runnable r1 = new Runnable(){
            public void run(){ 
                imagebuttons[btnid].setImageBitmap(imagesTurnedOn.get(btnid));
                imagebuttons[btnid].postDelayed(r2, 1000);
                }
        };

        // post the above delayed
        imagebuttons[btnid].postDelayed(r1, 1000 * count++);
    }
}

任何人都可以帮助我,并建议为什么它对我不起作用?

1 个答案:

答案 0 :(得分:1)

它对我有用。你确定imagesTurnedOn / imagesTurnedOff正在返回正确的值吗?

这个解决方案在时间方面有很多不足之处 - 它会非常不均匀。也许这样的事情会更好(使用AsyncTask)

public void deplayedPlay2() {
    if (mTaskHandler == null) {
        mTaskHandler = new AsyncTask<Void, Void, Void>() {
            @Override
            public Void doInBackground(Void... params) {
                long now = System.currentTimeMillis();
                try {
                    for (final int btnid : mScenario) {
                        Log.d(TAG,
                                "ON: " + btnid + " (" + (System.currentTimeMillis() - now) + ")");
                        mButtons[btnid].post(new Runnable() {
                            public void run() {
                                mButtons[btnid]
                                        .setBackgroundDrawable(GoodbyeAndroidActivity.this
                                                .getResources()
                                                .getDrawable(
                                                        R.drawable.on_icon));

                            }
                        });
                        Thread.sleep(1000);
                        Log.d(TAG,
                                "OFF: " + btnid + " (" + (System.currentTimeMillis() - now) + ")");
                        mButtons[btnid].post(new Runnable() {
                            public void run() {
                                mButtons[btnid]
                                        .setBackgroundDrawable(GoodbyeAndroidActivity.this
                                                .getResources()
                                                .getDrawable(
                                                        R.drawable.off_icon));
                            }
                        });
                    }
                } catch (InterruptedException ex) {
                    Log.d(TAG, "Interrupted.");
                }
                return null;
            }

            @Override
            public void onPostExecute(Void param) {
                Log.d(TAG, "Done!");
                mTaskHandler = null;
            }
        };
        mTaskHandler.execute();
    }
}

不要忘记在onPause()中处理此问题:

public void onPause() {
    super.onPause();
    if (mTaskHandler != null) {
        mTaskHandler.cancel(true);
        // May want to reset buttons too?
    }
}