机器人的WebView加载数据的性能非常缓慢 [英] Android webview loading data performance very slow

查看:181
本文介绍了机器人的WebView加载数据的性能非常缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我工作的一个应用程序,在此,我采用了Android的WebView。 每当我启动的WebView活动,装载数据串HTML格式的test.txt文件。 test.txt文件包含了近2.5 MB的数据,加载后test.txt文件,如果滑动屏幕来结束读取所有数据和preSS回来。 然后随后推出的WebView活动采取更多的时间来呈现数据。在第一次启动的WebView其服用最少的时间。 我没有收到任何错误/异常/崩溃发起这项活动的。

Hi I am working on one application, In that I am using Android WebView. Whenever I launch webview activity, loading data in string html format from test.txt file. test.txt file contains nearly 2.5 MB data, after loading test.txt file, if slide screen to end to read all data and press back. Then subsequent launch of webview activity taking more time to render data. In first launch of webview its taking minimal time. I am not getting any error/exception/crash in between launching this activity.

我使用的是Android API级别18以上

I am using Android API level 18 and above

注意:我做了很多研究这个问题。我试图解决方案从的Andr​​oid的WebView慢和的 Android的WebView功能效果没有用的。

Note: I did lot of research on this issue. I tried Solution from Android webview slow and Android WebView performance no use.

我不能让机器人:hardwareAccelerated =真正的< - 为什么,因为它有很多副作用(不过我用这一个,但没有改变,我发现在这个问题上) 。

I can't make android:hardwareAccelerated="true" <-- why because it has lot of side effects (Still I used this one, but no changes I found on this issue).

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

setRenderPriority() - >该方法是pcated在API层面18日$ P $。                         不建议调整线程的优先级,这样就不会在将来的版本中得到支持。

setRenderPriority() --> This method was deprecated in API level 18. It is not recommended to adjust thread priorities, and this will not be supported in future versions.

我的DataLoader.java类

My DataLoader.java class

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

我web_view.xml文件

my web_view.xml file

<?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>

请让我知道如果您有任何变通解决这个问题。

please let me know if you have any work around on this issue.

推荐答案

我觉得下面的效果最好:

I think the following works best:

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

Android的19具有铬引擎的WebView。我想它的工作原理与硬件加速更好。

Android 19 has Chromium engine for WebView. I guess it works better with hardware acceleration.

有关详细信息 的Andr​​oid 4.4奇巧,浏览器和Chrome浏览器的WebView

For more info Android 4.4 KitKat, the browser and the Chrome WebView

硬件加速也确实是在trick.You可以使用它在应用程序的不同层次。

Hardware Acceleration also do's the trick.You can use it in different levels in your application.

应用级

<application android:hardwareAccelerated="true" ...>

活动水平

<application android:hardwareAccelerated="true">
    <activity ... />
    <activity android:hardwareAccelerated="false" />
</application>

窗位

getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

查看水平

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

不过,在你的问题,你可以详细的副作用,这已经提到?

But as already mentioned in your question can you elaborate the side effects for this ?

这篇关于机器人的WebView加载数据的性能非常缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆