我正在制作启动画面,显示4至5秒,然后出现登录画面。
如何在启动画面中存储4到5张图像?每次启动应用程序时使用不同图像的Android启动画面
我想在用户每次启动应用程序时更改启动画面图像。
我该怎么做?
我正在制作启动画面,显示4至5秒,然后出现登录画面。
如何在启动画面中存储4到5张图像?每次启动应用程序时使用不同图像的Android启动画面
我想在用户每次启动应用程序时更改启动画面图像。
我该怎么做?
上的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]);
}
你可以使用无限数量的图像
你可以使用一个int数组和你要使用的drawables的id。存储您在SharedPreference
中显示的最后一个drawable的索引,并在应用程序启动时检索它,将其增加并再次存储。
我认为你可以在你的资源中有一些图像,然后你可以随机设置启动画面的图像。
这里是随机设置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);
在这个我不能设置布局(setContentView(R.layout.Splash))..? – Amardeepvijay
当然,你必须设置它。 – GhoRiser
得到这个错误:java.lang.RuntimeException:无法启动活动,android.util.AndroidRuntimeException:requestFeature()必须在添加内容之前调用 – Amardeepvijay