假设我的应用程序主屏幕有几张背景图片。每次应用程序启动时,我都希望随机选择背景图片,这在 android 中可能吗?
问问题
759 次
1 回答
2
简短的回答:是的。
长答案:
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);
}
于 2013-09-08T11:48:39.207 回答