我想制作一个随机的启动画面,这样每次打开应用程序时都会加载另一个图像。
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash_screen);
private int[] splashImages = {R.drawable.splash1, R.drawable.splash2, R.drawable.splash3};
Random random = new Random(System.currentTimeMillis());
int postOfImage = random.nextInt(splashImages.length -1);
有人可以告诉我该怎么做吗?
答案 0 :(得分:1)
public class MainActivity extends Activity {
private static int[] splashImages = { R.drawable.splash1, R.drawable.splash2, R.drawable.splash3 };
ImageView imgSplash;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
imgSplash = (ImageView) findViewById(R.id.splash);
int selected;
selected = randomBox();
if (selected == sharedPreferences.getInt("SELECTED", 0)) {
selected = randomBox();
}
Editor editor = sharedPreferences.edit();
editor.putInt("SELECTED", selected);
editor.commit();
for (int i = 0; i < splashImages.length; i++) {
if (selected == i) {
imgSplash.setImageResource(splashImages[i]);
}
}
}
public static int randomBox() {
Random rand = new Random();
int pickedNumber = rand.nextInt(splashImages.length);
return pickedNumber;
}
}