如何定期更改背景图像? [英] How do I periodically change background image?

查看:113
本文介绍了如何定期更改背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一秒计时器上更改我的应用程序的背景图像(更改两个图像之间的背景)。我知道如何在按下按钮时更改图像,但我很难找到计时器的代码。我该怎么办?

I want to change the background image of my app on a one second timer (changing the background between two images) . I know how to change the image on a button press, but I'm having difficulties finding a code for the timer. What should I be doing?

谢谢。

推荐答案

你可以使用 View.postDelayed(Runanble r,long delayMillis)。例如:

public void onCreate() {
    ...
    ImageView backgroundImageView = findViewById(R.id.background);
    backgroundImageView.postDelayed(new Runnable() {
        static int i = 0;
        public void run() {
            ImageView.this.setImageResource(
                i++ % 2 == 0 ?
                    R.drawable.background_image1 :
                    R.drawable.background_image2);
            ImageView.this.postDelayed(this, 1000);
        }
    }, 1000);
}

这篇关于如何定期更改背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆