我的启动画面加载速度太慢

时间:2016-04-28 06:58:35

标签: android

我是Android开发的初学者,我面临的问题是“我的Android应用程序启动画面加载太慢”请帮我解决这个问题。

这是我的spash.java源代码:

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

@SuppressLint("NewApi")
public class Splash extends Activity {

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash);

            ActionBar actionBar = getActionBar();
            actionBar.hide();

            Thread t =new Thread(){
                public void run(){
                    try{
                        sleep(10000);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }finally{
                        Intent i =new Intent(Splash.this,MainActivity.class);
                        startActivity(i);
                    }
                }
            };
            t.start();
        }
        @Override
        public void onPause(){
            super.onPause();
            finish();
        }

    }

5 个答案:

答案 0 :(得分:3)

更改sleep time,将2000代替10000,这将持续2秒

答案 1 :(得分:1)

试试这些代码。这段代码可行。

<强> SplashScreen.java

public class SplashScreen extends Activity {

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;
    ImageView logo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen_one);

        logo = (ImageView) findViewById(R.id.imgLogo);

        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @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);
    }

}

XML文件如下: -

<强> splash_screen_one.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/logo_xelenlentro"/>

    <ProgressBar
        android:id="@+id/ProgressBar"
        style="@android:style/Widget.Holo.ProgressBar.Large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/imgLogo"
        android:layout_centerVertical="true"
        android:visibility="visible"></ProgressBar>


</RelativeLayout>

答案 2 :(得分:0)

尝试使用以下代码。

public class ViewSplashScreen extends AppCompatActivity implements View.OnClickListener,Runnable {
    RelativeLayout rltvGetstarted;
    Activity thisActivity;
    private Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }*/
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_splashscreen);
        mHandler = new Handler();
        mHandler.postDelayed(this, 1000);
        /*init*/
        init();
    }

    private void init() {
        thisActivity = this;
        rltvGetstarted = (RelativeLayout) findViewById(R.id.rltv_getstarted);
        rltvGetstarted.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v==rltvGetstarted){
            CM.startActivity(thisActivity,ViewMobileNo.class);
        }
    }

    @Override
    public void run() {
        //if user already login
        //CM.startActivity(thisActivity,ViewMobileNo.class);
        CM.startActivity(thisActivity,ViewBase.class);
        finish();
    }

}

答案 3 :(得分:0)

在您的代码中,您必须更改Thread睡眠时间,然后再加载...

请参阅以下已更新的代码,此处sleep(10000);更改sleep(2000);

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

@SuppressLint("NewApi")
public class Splash extends Activity {

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        ActionBar actionBar = getActionBar();
        actionBar.hide();

        Thread t =new Thread(){
            public void run(){
                try{
                    sleep(2000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent i =new Intent(Splash.this,MainActivity.class);
                    startActivity(i);
                }
            }
        };
        t.start();
    }
    @Override
    public void onPause(){
        super.onPause();
        finish();
    }

}

答案 4 :(得分:0)

希望这也可能有所帮助,因为启动画面会出现5秒的睡眠持续时间

Thread background = new Thread() {
        public void run() {

        try {
        sleep(5*1000);

        Intent i=new Intent(SplashScreen.this,
                MainActivity.class);
        startActivity(i);
        finish();

            } catch (Exception e) {

            }
        }
    };

    background.start();

了解详情 - &gt; Splash Screen