可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I would like the splash image to begin and stay for 3 seconds, and then disappear and continue or be replaced with the rest of the layout in the main.xml.
This is my code:
Main.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
ImageView splash = (ImageView) this.findViewById(R.id.splash);
main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- margin=0px, padding=20px -->
<!--textview padding=10dp, textSize=16sp-->
<!--px=pixel, dp=density indepen sp=scale indepen fontsize preference -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/splash2"/>
<ImageView
android:id="@+id/myImageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg_main"/>
<ImageView
android:id="@+id/myImageView0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bar_top"/>
<!--
<TextView android:id="@+id/textItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="10dp"
android:paddingLeft="110dp"
android:background="#00000000"
android:textColor="#ffffffff"
android:textSize="22sp"
android:text="Find Car"
android:enabled="false"
>
-->
<TabHost android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom = "@android:id/tabcontent"
/>
</RelativeLayout>
</TabHost>
</RelativeLayout>
回答1:
You can do this
ImageView splash = (ImageView) this.findViewById(R.id.splash);
splash.postDelayed(new Runnable(){
splash.setVisibility(View.GONE);
}, 3000);
Or maybe add an animation by invoking this method (from the Android docs) instead of setting visibility to GONE directly
private void fadeSplashOut() {
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
mContentView.setAlpha(0f);
mContentView.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
mContentView.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(null);
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
splash.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
splash.setVisibility(View.GONE);
}
});
}
回答2:
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Sets the layout of welcome_screen.xml
setContentView(R.layout.welcome_screen);
Thread timer= new Thread()
{
public void run()
{
try
{
//Display for 3 seconds
sleep(3000);
}
catch (InterruptedException e)
{
// TODO: handle exception
e.printStackTrace();
}
finally
{
//Goes to Activity StartingPoint.java(STARTINGPOINT)
Intent openstartingpoint=new Intent("x.y.z.START");
startActivity(openstartingpoint);
}
}
};
timer.start();
}
//Destroy Welcome_screen.java after it goes to next activity
@Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
finish();
}
回答3:
Use Handler to hold the UI for some time:
public class SplashActivity extends Activity {
/*Duration of wait*/
private final int SPLASH_DISPLAY_LENGTH = 2000;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* Create an Intent that will start the MainActivity. */
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
回答4:
There is One more Solution for it , You can create Different class for SplashScreen and make SplashScreen as yout Launcher activity but not the MainActivity.
Like This:
<activity
android:name=".SplashScreen"
android:label="@string/title_activity_splash_screen"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
</activity>
And thn in SplashSacreen.java , You write the code like this :
public class SplashScreen extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
Thn after in SplashScreen.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark" >
<ImageView
android:id="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/comp_logo" />
And then check it
回答5:
Make a new XML layout for your splash, called splash below in setContentView(R.layout.splash);
. Then make a new activity to play after the splash, I called it ACTIVITYTWO below but you can change that. Change the number in while (lTimer1 < 3000)
to change the length of the splash, with 1000 equaling 1 second.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
Thread lTimer = new Thread() {
public void run() {
try {
int lTimer1 = 0;
while (lTimer1 < 3000) {
sleep(100);
lTimer1 = lTimer1 + 100;
}
startActivity(new Intent("com.example.ACTIVITYTWO"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
finish();
}
}
};
lTimer.start();
}
}
回答6:
So a good way to do this would be to call asynctask and have it wait 3 seconds then on postProgress set the imageview with id splash to visibility gone.
So here are some resources...
http://developer.android.com/reference/android/os/AsyncTask.html
I can explain further if needed. also you might want to consider alternatives. I was simply offering a solution for your current setup.
i decided to include some code....
private class SplashScreen extends AsyncTask<ImageView, Void, Void> {
ImageView imgView;
protected Void doInBackground(ImageView... view) {
imgView = view[0];
wait(3000); // not sure if this works but u can fo a while loop etc if not
}
protected void onPostExecute(Long result) {
imgView.setVisibility(ImageView.GONE);
}
}
Then in your onCreate()
instantiate and execute like so....
new SplashScreen().execute(splash);
回答7:
For Kotlin :
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
Handler().postDelayed(Runnable {
val i = Intent(this@SplashActivity, HomeScreen::class.java)
startActivity(i)
finish()
}, 3000)
}
}
回答8:
try this
public class Welcome extends Activity
{
/** Called when the activity is first created. */
Handler mHandler,actHandler;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
new Thread(){
public void run(){
try{
Thread.sleep(3000);
}
catch(Exception ex){
Log.e("Welcome Exception :",ex.toString());
}
try{
Message msg=mHandler.obtainMessage();
mHandler.sendMessage(msg);
}
catch(NullPointerException ex){
Log.e("Handler Exception :",ex.toString());
}
}
}.start();
mHandler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
Intent i=new Intent(Welcome.this,M_chat.class);
startActivity(i);
finish();
}
};
}
}
回答9:
private void moveToHome(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, HomeScreen.class);
startActivity(i);
finish();
}
}, 4000);
}
回答10:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*Duration of wait*/
int SPLASH_DISPLAY_LENGTH = 10000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* Create an Intent that will start the MainActivity. */
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}