如何在android中的splashscreen中显示随机图像?

时间:2015-03-12 13:51:51

标签: java android splash-screen

我每次打开应用程序时都会创建一个Android应用程序,当然还有加载/启动画面。我已经创建了一个启动画面xml和类。但只有一个图像。 我想要发生的是每次启动应用程序时加载不同的图像和随机文本。

这是我在splashscreen.xml上的代码



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/exit1" />
 
 </RelativeLayout>
&#13;
&#13;
&#13;

这是我在splashscreen.java中的代码

&#13;
&#13;
package travelph.project;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class SplashScreen extends Activity {
	// Randomise a background
    int[] yourListOfImages= {R.drawable.about1, R.drawable.background1
    		, R.drawable.bakya1, R.drawable.coconut1, R.drawable.exit1hdpi};

	private static final int TIME = 5 * 1000;// 5 seconds

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);    // Removes title bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);    // Removes notification bar
		setContentView(R.layout.splashscreen);
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
			    Random random = new Random(System.currentTimeMillis());
			    int posOfImage = random.nextInt(yourListOfImages.length + 1);
			    ImageView imageView= (ImageView) findViewById(R.id.imageView);
			    imageView.setBackgroundResource(yourListOfImages[posOfImage]);
			    
				Intent intent = new Intent(SplashScreen.this, MainMenu.class);
				startActivity(intent);
				SplashScreen.this.finish();

				overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

			}
		}, TIME);
		
		new Handler().postDelayed(new Runnable() {
			  @Override
			  public void run() {
			         } 
			    }, TIME);

	}

	
	@Override
	public void onBackPressed() {
		this.finish();
		super.onBackPressed();
	}
}
&#13;
&#13;
&#13;

代码片段真的会有很大的帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

下面的代码应该严格地在onCreate(例如setContentView下方,新Handler创建之上),而不是postDelayed内 - &gt; run()

Random random = new Random(System.currentTimeMillis());
int posOfImage = random.nextInt(yourListOfImages.length + 1);
ImageView imageView= (ImageView) findViewById(R.id.imageView);
imageView.setBackgroundResource(yourListOfImages[posOfImage]);

你在TIME和immidaitelly之后设置图像,之后你正在调用另一个活动的意图并且当前正在完成

答案 1 :(得分:0)

您需要更改

imageView.setBackgroundResource(yourListOfImages[posOfImage]);

imageView.setImageResource(yourListOfImages[posOfImage]);