gpt4 book ai didi

java - 将背景图像更改为随机

转载 作者:行者123 更新时间:2023-12-02 06:45:29 25 4
gpt4 key购买 nike

假设我的应用程序主屏幕有几张背景图片。每次应用程序启动时,我都希望随机选择背景图片,这在 Android 中可能吗?

最佳答案

简短回答:是的。

长答案:

private static final int NUM_BACKGROUNDS = 5; // or whatever
private Random mRandom = new Random();

public void onCreate(Bundle state) {
View v = findViewById(/* your background view id */);
int res;
int i = mRandom.nextInt(NUM_BACKGROUNDS);
switch (i) {
case 0: res = R.drawable.bg0; break;
case 1: res = R.drawable.bg1; break;
case 2: res = R.drawable.bg2; break;
case 3: res = R.drawable.bg3; break;
case 4: res = R.drawable.bg4; break;
default: throw new IllegalArgumentException("oops?");
}
v.setBackgroundResource(res);
}

关于java - 将背景图像更改为随机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18683275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com