2014-10-27 183 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