Android webview加载数据性能非常慢

And*_*ner 33 android webview android-webview android-activity

嗨,我正在开发一个应用程序,因为我正在使用Android WebView.每当我启动webview活动时,从test.txt文件加载字符串html格式的数据.test.txt文件包含近2.5 MB的数据,加载test.txt文件后,如果幻灯片屏幕结束,则读取所有数据并按回.然后,随后启动webview活动需要更多时间来呈现数据.在第一次推出webview时,它花费的时间很少.在启动此活动之间,我没有收到任何错误/异常/崩溃.

我使用的是Android API等级18及以上版本

注意:我在这个问题上做了很多研究.我尝试从Android webview的解决方案Android WebView性能没有用.

我不能使android:hardwareAccelerated ="true" < - 为什么因为它有很多副作用(我仍然使用这个,但我没有在这个问题上发现任何变化).

我不能使用 webview.getSettings().setRenderPriority(RenderPriority.HIGH);

setRenderPriority() - >此方法在API级别18中已弃用.建议不要调整线程优先级,并且在将来的版本中不支持此方法.

我的DataLoader.java类

public class DataLoader extends Activity {

    private WebView webView = null;
    // Progress Dialog
    private ProgressDialog progressDialog;

    String dataContent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.web_view);
        webView = (WebView)findViewById(R.id.text);

        // Loading webview in Background Thread
        new LoadWebView().execute();
    }

    class LoadWebView extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(DataLoader.this);
            progressDialog.setTitle("Loading...");
            progressDialog.setMessage("Please wait.");
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }

        protected String doInBackground(String... args) {
            // AssetFileReader read the txt file and return data in the form of string
            dataContent = AssetFileReader.read(getApplicationContext(), "test.txt");

            return null;
        }

        protected void onPostExecute(String str) {
            // dismiss the dialog
            progressDialog.dismiss();

            if (dataContent != null) {
                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() {

                        WebSettings s = webView.getSettings();
                        s.setUseWideViewPort(true);
                        s.setSupportZoom(true);
                        s.setBuiltInZoomControls(true);
                        s.setDisplayZoomControls(false);
                        s.setJavaScriptEnabled(true);
                        webView.loadData(dataContent, "text/html", "utf-8");
                    }
                });
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        webView.clearCache(true);
        webView.clearHistory();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的web_view.xml文件

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

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/text">
    </WebView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如果你在这个问题上有任何解决方法,请告诉我.

Kin*_*ses 57

我认为以下效果最好:

if (Build.VERSION.SDK_INT >= 19) {
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}       
else {
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Run Code Online (Sandbox Code Playgroud)

Android 19为WebView提供了Chromium引擎.我猜它在硬件加速方面效果更好.

有关Android 4.4 KitKat,浏览器和Chrome WebView的更多信息

硬件加速也很有用.您可以在应用程序的不同级别使用它.

申请级别

<application android:hardwareAccelerated="true" ...>
Run Code Online (Sandbox Code Playgroud)

活动水平

<application android:hardwareAccelerated="true">
    <activity ... />
    <activity android:hardwareAccelerated="false" />
</application>
Run Code Online (Sandbox Code Playgroud)

窗口水平

getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Run Code Online (Sandbox Code Playgroud)

查看级别

myView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Run Code Online (Sandbox Code Playgroud)

但正如你在问题中已经提到的那样,你能详细说明这方面的副作用吗?

  • 请发布副作用!启用硬件加速以加载WebView我认为不是必需的. (3认同)

Raj*_*ela 6

我试过下面的代码,它对我来说工作得更快

// For API level below 18 (This method was deprecated in API level 18)
webview.getSettings().setRenderPriority(RenderPriority.HIGH); 

webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
if (Build.VERSION.SDK_INT >= 19) {
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}       
else {
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Run Code Online (Sandbox Code Playgroud)

  • setRenderPriority()方法现已弃用 (4认同)