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

发布于 2024-11-30 03:17:13 字数 946 浏览 6 评论 0原文

我正在尝试更改按钮上的图像并将它们转回原始图像,并在 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++);
    }
}

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

I'm trying to change images on buttons and turn them back to the original image, and to do it one after the other in 4 different images.

I have tried the following code, but it didn't work, the result causes only to one of the images to blink for a milisecond:

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++);
    }
}

Can anyone help me, and suggest why it doesn't working for me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2024-12-07 03:17:13

这对我有用。您确定 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?
    }
}

It worked for me. Are you sure that imagesTurnedOn/imagesTurnedOff are returning the correct values?

This solution leaves a lot to be desired in terms of timing -- it will be quite uneven. Perhaps something like this would work better (using an 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();
    }
}

Don't forget to handle this in onPause():

public void onPause() {
    super.onPause();
    if (mTaskHandler != null) {
        mTaskHandler.cancel(true);
        // May want to reset buttons too?
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文