【问题标题】:How to show splash image while loading activity如何在加载活动时显示启动图像
【发布时间】:2011-11-18 07:27:03
【问题描述】:

我有一个包含许多 UI 视图的活动。在其 onCreate 方法中,我发现单行 setContentView 需要 8-12 秒才能完成。所以我想在加载时显示我的徽标图像。我尝试了很多东西,但没有任何成功。我怀疑主要原因可能是在完成 setContentView 之前,什么都不能显示。

任何帮助将不胜感激。

更新:

我想很多人不知道在完成 setContentView 之前不能显示任何对话框。所以使用另一个启动活动对我没有帮助。

更新2

我在找到问题原因后忘记更新此问题。请参考以下问题:setContentView taking long time (10-15 seconds) to execute

【问题讨论】:

标签: android splash-screen


【解决方案1】:

使用AsyncTask

onPreExecute()中加入splash

doInBackground()做你的工作

并在onPostExecute()中关闭飞溅

【讨论】:

    【解决方案2】:

    下面是使用 CountDownTimer 类创建启动画面的简单代码

    public class SplashDialogActivity extends Activity {
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            // Be sure to call the super class.
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.layout);
          counter.start();
        }
        MyCount counter = new MyCount(5000, 1000);
     public class MyCount extends CountDownTimer{
                public MyCount(long millisInFuture, long countDownInterval) {
                super(millisInFuture, countDownInterval);
                }
    
                @Override
                public void onFinish() {
                    go_back();
                }
    
                @Override
                public void onTick(long millisUntilFinished) {
    
    
    
                  }
         }
    
     public void go_back()
            {
              counter.cancel();
    
                        Intent i=new Intent(this,account.class);
                        i.putExtra("first_time", true);
                        startActivity(i);
    
                this.finish();
            }
    }
    

    【讨论】:

    • 先生,您可以减少时间 MyCount counter = new MyCount(2000, 1000); 2000 表示 2 秒..所以这就是你可以控制闪屏显示时间的方法
    • 尝试使用 setContentView 两次在一个活动中显示两个屏幕(启动画面和主屏幕),在它们之间您可以使用线程或计时器
    • 如果 setContentView 需要一些时间,请尝试在运行时而不是在设计时加载数据或图像文件
    【解决方案3】:

    试试这个代码作为启动页面

    private Thread mSplashThread;    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.splesh);
    
        final Splash sPlashScreen = this;   
    
         mSplashThread =  new Thread(){
            @Override
            public void run(){
                try {
                    synchronized(this){
                        wait(5000);
                    }
                }
                catch(InterruptedException ex){                    
                }
    
                finish();
    
                Intent intent = new Intent();
                intent.setClass(sPlashScreen,Login.class);
                startActivity(intent);
                stop();                    
            }
        };
    
        mSplashThread.start();        
     }
    
    // Processes splash screen touch events
    @Override
    public boolean onTouchEvent(MotionEvent evt) {
    
         if(evt.getAction() == MotionEvent.ACTION_DOWN)
         {
             synchronized(mSplashThread){
                 mSplashThread.notifyAll();
             }
         }
         return true;
    }    
    

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多