2014-10-27 122 views
0

我是Android應用程序中的新手,這是我的第一個應用程序。 我已經創建了啓動畫面和工程..但其後走了一個長長的白色空白屏幕約2-5秒,然後URL開始加載..Android Webview:加載url時出現閃屏

而我的問題是..如何使一些加載或進度加載網址時在啓動畫面中顯示吧?所以沒有更多的白色黑屏。對不起,我的英語不好。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.xxx.xxx" > 

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 


<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="xxx" 
    android:theme="@style/AppTheme" 
    android:hardwareAccelerated="true" /> 


    <activity 
     android:name="com.xxx.xxx.Splash" 
     android:label="xxx" 
     android:theme="@style/Theme.MyAppTheme" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".MyActivity" 
     android:hardwareAccelerated="true" 
     android:configChanges="orientation|screenSize" 
     android:label="xxx" > 

    </activity> 

</application> 

</manifest> 

activity_my.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    tools:context=".MyActivity"> 

    <WebView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/webView" 
     android:layout_gravity="center" 
     /> 

<TextView 
    android:id="@+id/display" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:visibility="gone" 
    /> 

<FrameLayout 
     android:id="@+id/customViewContainer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:visibility="gone"/> 
</LinearLayout>  

Splash.java

import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.os.Handler; 

public class Splash extends Activity { 

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

    final Handler handel = new Handler(); 
    handel.postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      Intent loadSplash = new Intent(Splash.this, MyActivity.class); 

      startActivity(loadSplash); 

      finish(); 
      } 
     }, 3000); 
    } 
} 

splashscreen.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#ffffff" > 

<ImageView 
android:id="@+id/imageView1" 

    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/splash" /> 

</RelativeLayout> 
+0

[初始屏幕而在Android應用一個網頁視圖加載URL](HTTP的可能的複製:// stackoverflow.com/questions/9589365/splash-screen-while-loading-a-url-in-a-webview-in-android-app) – 2016-03-26 20:56:12

回答

2

不要將Splash屏幕創建爲單獨的活動。在您的WebView活動中添加啓動功能,即您的頁面加載活動。收聽onPageFinished事件並隱藏飛濺圖像或動畫。

mWebView.setWebViewClient(new WebViewClient() { 

    public void onPageFinished(WebView view, String url) { 
     //hide splash functionality 
    } 
}); 

有了這個,你還可以得到頁面加載進度,如果你想顯示一個進度條

mWebView.setWebChromeClient(new WebChromeClient() { 
       public void onProgressChanged(WebView view, int progress) 
        { 
        if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){ 
         Pbar.setVisibility(ProgressBar.VISIBLE); 
        } 
        Pbar.setProgress(progress); 
        if(progress == 100) { 
         Pbar.setVisibility(ProgressBar.GONE); 
        } 
       } 
      }); 
+0

無法解析符號Pbar? – z4bl3nk 2014-10-27 07:27:52

+0

這是你的進度條,所以通過代碼或xml創建一個進度條來顯示你的活動並更新onProgressChanged事件 – 2014-10-27 07:32:05

0

這裏是如何做到這一點 -

wv.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onPageFinished(WebView view, String url) { 
      //hide loading image 
      findViewById(R.id.imageLoading1).setVisibility(View.GONE); 
      //show webview 
      findViewById(R.id.webView1).setVisibility(View.VISIBLE); 
     } 
    });  

    wv.loadUrl("http://yoururlhere.com"); 

你可以找到完整示例在這裏 - Splash Screen While Loading a URL

0

在這裏你去..作爲Jibran Khan說,不要創建單獨的飛濺活動。在你的webview上使用啓動功能。首先,通過爲WebViewClient擴展創建WebClient類,你會得到未實現的方法,如this`

{ 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 

    @Override 
    public void onLoadResource(WebView view, String url) { 

    } 

    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     progressBar1.setVisibility(View.VISIBLE); 
     webview.setVisibility(View.GONE); 
     super.onPageStarted(view, url, favicon); 
    } 

    public void onPageFinished(WebView view, String url) { 
     progressBar1.setVisibility(View.GONE); 
     webview.setVisibility(View.VISIBLE); 
    } 
} 

` 使用進度條負荷指標在上面的代碼中完成的。

不要忘記這樣做web.setWebViewClient(new MyWebViewClient());其中MyWebViewClient是你的WebClient類名

這裏是上面的代碼

<WebView 
    android:id="@+id/web" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:visibility="gone" /> 

<ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" /> 
+0

哪個ProgressBar代碼? – z4bl3nk 2014-10-27 08:09:27

+0

我已經爲相同的代碼添加了xml – Sumit 2014-10-27 09:01:41

+0

我把上面的xml並得到無法解析符號progressBar1? – z4bl3nk 2014-10-27 20:43:13

1

嘗試這兩種options.hope它會幫助你

XML

1.去除回調的處理程序

handel.postDelayed(new Runnable() { 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     Intent loadSplash = new Intent(Splash.this, MyActivity.class); 

     startActivity(loadSplash); 

     finish(); 
     handel.removeCallbacks(this); 

     } 
    }, 3000); 
  • 使用螺紋,而不是處理程序

    public class Splash extends Activity { 
    
    protected boolean _active = true; 
    protected int _splashTime = 3000; 
    Thread splashTread; 
    private boolean stop = false; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.splashscreen); 
        splashTread = new Thread() 
        { 
         @Override 
         public void run() { 
          try { 
           int waited = 0; 
           while (_active && (waited < _splashTime)) { 
            sleep(100); 
            if (_active) { 
             waited += 100; 
            } 
           } 
          } catch (InterruptedException e) { 
           // do nothing 
          } finally { 
           if (!stop) { 
            startActivity(new Intent(Splash.this, MyActivity.class)); 
            finish(); 
           } else 
            finish(); 
          } 
         } 
        }; 
        splashTread.start(); 
    } 
    

    }

  • +0

    我試過第二個代碼..啓動畫面的作品和之後..長白色的空白屏幕.. – z4bl3nk 2014-10-27 20:48:42

    +0

    非常感謝..現在的作品 – z4bl3nk 2016-06-21 06:42:25

    +0

    如果它的工作,請將它標記爲答案它也會幫助其他人或如果你改變任何代碼也請張貼 – Manju 2016-06-21 10:33:13