0

情况如下:我创建了一个加载https://cstudios.sk/pemobil/的 webview 应用程序。该网站在原生 android 浏览器和 chrome 上都能正常工作。在大多数手机上也可以,但在 Galaxy S4 上,系统不会渲染某些部分。稍后,在使用下,它会部分纠正自己,但不会完全纠正。

https://www.dropbox.com/s/c9jtb9fc4fvohls/2013-08-02%2000.14.42.png << 渲染不良页面的屏幕。

MainActivity.java:

    package sk.cstudios.pizzaextraonline;

import android.R.bool;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

    WebView pizzaExtraView;
    ProgressDialog loadingMessage;
    boolean loaded; 

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

        if (!isNetworkAvailable())
        {
            new AlertDialog.Builder(this)
            .setTitle(getResources().getString(R.string.nonet_title))
            .setMessage(getResources().getString(R.string.nonet_description))
            .setPositiveButton(getResources().getString(R.string.nonet_closeapp), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 
                    // continue with close app
                    finish();
                }
             })
            .setNegativeButton(getResources().getString(R.string.nonet_gosettings), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 
                    // go to settings
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.setClassName("com.android.settings", "com.android.settings.wifi.WifiSettings");
                    startActivity(intent);
                    finish();
                }
             })
             .show();
        } else {

        loaded = false;
        pizzaExtraView = (WebView) findViewById(R.id.webview);
        pizzaExtraView.setWebViewClient(new Cwebview());
        WebSettings webSettings = pizzaExtraView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setDatabasePath("data/data/com.xxx/databases");
        webSettings.setDomStorageEnabled(true);
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setSupportZoom(false);
        webSettings.setAllowFileAccess(true);
        webSettings.setSavePassword(true);
        webSettings.setSupportMultipleWindows(false);
        webSettings.setAppCacheEnabled(true);
        webSettings.setAppCachePath("");
        webSettings.setAppCacheMaxSize(5*1024*1024);
        webSettings.setRenderPriority(RenderPriority.HIGH);
        pizzaExtraView.loadUrl("https://cstudios.sk/pemobil/");
      }
    }

    @SuppressLint("NewApi")
    public class Cwebview extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //if (Uri.parse(url).getHost().equals("https://cstudios.sk/") || Uri.parse(url).getHost().equals("https://www.cstudios.sk/")) {
            if(true) {
                //pizzaExtraView.loadUrl(url);
                view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
                return false;
            }
            view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            /*Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);*/
            return true;
        }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if(!loaded) {
                loadingMessage = ProgressDialog.show(MainActivity.this,getResources().getString(R.string.loading_title),getResources().getString(R.string.loading_decription),true);       
                }
            }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            if(!loaded) {
            loadingMessage.dismiss();
            loaded = true;
            }
        }
    }

    private long lastpressed;
    private static final int PERIOD = 333;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            if((event.getEventTime() - lastpressed) < PERIOD)
            {
                Log.println(10, "testing", "doubletapped");
                finish();
            } else {
                lastpressed = event.getEventTime();
                Context context = getApplicationContext();
                Toast toast = Toast.makeText(context, getResources().getString(R.string.back_hint), Toast.LENGTH_SHORT);
                toast.show();
                pizzaExtraView.goBack();
                return true;
            }
        }

        return false;
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager 
              = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

}

和清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sk.cstudios.pizzaextraonline"
    android:versionCode="3"
    android:versionName="1.0.020803" android:installLocation="preferExternal">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />
    <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="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"
        android:hardwareAccelerated="true" >
        <activity
            android:name="sk.cstudios.pizzaextraonline.splash"
            android:label="@string/app_name"
            android:hardwareAccelerated="true" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name="sk.cstudios.pizzaextraonline.MainActivity"
            android:label="@string/app_name"
            android:hardwareAccelerated="true" 
            android:screenOrientation="portrait">
            <intent-filter>

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

    </application>

</manifest>

知道是什么导致了问题吗?您可以自由测试该站点,它尚未连接到订购系统。

4

1 回答 1

0

尝试改变

view.setLayerType(View.LAYER_TYPE_HARDWARE, null);

webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

或完全删除它。好像是硬件加速有问题

来自文档:

仅当为视图层次结构打开硬件加速时才使用 LAYER_TYPE_HARDWARE 如果硬件加速关闭,则硬件层的行为与软件层完全相同。

我会删除这一行

检查您的视图是否是硬件加速的:

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

...但话又说回来:

即使此方法返回 true,也不意味着每次调用 draw(android.graphics.Canvas) 都会使用硬件加速

还要检查您是否正在使用ProGuard,它可能会优化太多。建议将其关闭

还可以尝试为您的应用程序显式定义硬件加速:

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

所以,总的来说,玩所有这些可能的变量,也许一些配置会比你现在的更好

顺便说一句,那些以错误方式呈现的图片,它们是如何在网页上以 HTML 显示的?画布、Img 等?

于 2013-08-02T09:36:03.777 回答