gpt4 book ai didi

java - WebView setLayerType LAYER_TYPE_HARDWARE 不起作用

转载 作者:行者123 更新时间:2023-12-02 04:37:01 28 4
gpt4 key购买 nike

出于性能考虑,我对 WebView 使用 LAYER_TYPE_HARDWARE。但它无法正常工作。

enter image description here enter image description here

这是我的代码:

SmoothWebViewActivity.java

package uet.ducvu.androidexample;

import java.io.InputStream;
import java.util.Scanner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class SmoothWebViewActivity extends Activity {

private WebView mWebView;
private int mTextSize = 15;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smooth_web_view);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

mWebView = (WebView) findViewById(R.id.my_web_view);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);

//mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}

public void loadText(View view){
InputStream raw = getResources().openRawResource(R.raw.my_webview_template);
Scanner scanner = new Scanner(raw).useDelimiter("\\A");
String core = scanner.next();
String body = "Some data here";

scanner.close();
for (int i = 0; i < 1000; i++)
body += "<br/>" + i;
String html = String.format(core, mTextSize, body);

mWebView.loadDataWithBaseURL(null, html, null, "utf-8", null);
System.out.println("Hardware: " + mWebView.isHardwareAccelerated());
}

public void changeSize(View view){
mTextSize++;
mWebView.loadUrl("javascript:dynamicChangeSize(" + mTextSize + ");");
}
}

activity_smooth_web_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="uet.ducvu.androidexample.SmoothWebViewActivity"
tools:ignore="HardcodedText,ButtonStyle" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="loadText"
android:text="Load text" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="changeSize"
android:text="Change size" />

</LinearLayout>

<WebView
android:id="@+id/my_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

res/raw/my_webview_template.html

<html>
<head>
</head>

<body style="font-size:%dpx;">%s</body>

<script type="text/javascript">
function dynamicChangeSize(size){
document.body.style.fontSize = size + "px";
}
</script>
</html>

AndroidManifest.xml

<activity
android:name=".SmoothWebViewActivity"
android:label="@string/title_activity_smooth_web_view"
android:hardwareAccelerated="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

当我使用View.LAYER_TYPE_SOFTWARE时,一切正常。但是,当我切换到 View.LAYER_TYPE_HARDWARE 时,出现了一些问题:

  • 点击“加载文本”按钮,WebView什么也不显示,但只要点击进入网页,就会显示其内容。
    mWebView.loadDataWithBaseURL 之后插入 mWebView.scrollBy(0, 1); 可以解决这个问题,但我认为这只是一个“备份计划”
    启动应用程序并快速单击“加载文本”按钮,结果将显示。但点击按钮后仍然会导致显示空白页
  • “更改大小”功能在上述两种情况下都可以完美运行,但有时不起作用,需要点击 WebView 区域才能显示内容。
  • 我正在编写一个类似的WebView类(代码here),问题是相反的,页面总是在调用loadDataWithBaseURL(也使用LAYER_TYPE_HARDWARE)后显示,但是调用javascript函数(类似于更改大小)不会更改WebView的内容,...

    那么,是否缺少我需要与 LAYER_TYPE_HARDWARE 一起使用的配置?

最佳答案

这是一个老问题,但我注意到您已经在您的 list 中的 Activity 级别,使用 WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED 的窗口级别,和 View 级别使用mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);。尝试仅使用 View 级别加速,或仅使用 list 中的 android:hardwareAccelerated="true"。不是两者都在一起。

来自 Android 开发者页面...

There are two different ways to check whether the application is hardware accelerated:

View.isHardwareAccelerated() returns true if the View is attached to a hardware accelerated window. Canvas.isHardwareAccelerated() returns true if the Canvas is hardware accelerated

我希望这对某人有帮助。

关于java - WebView setLayerType LAYER_TYPE_HARDWARE 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30624791/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com