How do I periodically change background image?

纵饮孤独 提交于 2019-12-23 12:04:35

问题


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?

Thanks.


回答1:


You could use View.postDelayed(Runanble r, long delayMillis). For example, something like:

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


来源:https://stackoverflow.com/questions/8146053/how-do-i-periodically-change-background-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!