2014-02-13 34 views

回答

1

上的onCreate(),你可以做到这一点,从您的5个背景随机选择一个背景:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Initialize your activity and it's componments 
    setContentView(R.layout.splash); 
    ... 

    // Randomise a background 
    int[] yourListOfImages= {R.drawable.image1, R.drawable.image2, R.drawable.image2, R.drawable.image3, R.drawable.image5}; 

    Random random = new Random(System.currentTimeMillis()); 
    int posOfImage = random.nextInt(yourListOfImages.length - 1); 

    ImageView imageView= (ImageView) findViewById(R.id.imageView); 
    imageView.setBackgroundResource(yourListOfImages[posOfImage]); 
} 

你可以使用无限数量的图像

+0

在这个我不能设置布局(setContentView(R.layout.Splash))..? – Amardeepvijay

+0

当然,你必须设置它。 – GhoRiser

+0

得到这个错误:java.lang.RuntimeException:无法启动活动,android.util.AndroidRuntimeException:requestFeature()必须在添加内容之前调用 – Amardeepvijay

2

你可以使用一个int数组和你要使用的drawables的id。存储您在SharedPreference中显示的最后一个drawable的索引,并在应用程序启动时检索它,将其增加并再次存储。

1

我认为你可以在你的资源中有一些图像,然后你可以随机设置启动画面的图像。

这里是随机设置ImageView的图像资源的例子:您的活动

ImageView imgView = new ImageView(this); 
Random rand = new Random(); 
int rndInt = rand.nextInt(n) + 1; // n = the number of images, that start at idx 1 
String imgName = "img" + rndInt; 
int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); 
imgView.setImageResource(id); 
相关问题