lwb
2021-03-10 947e8d781f0dedbcf12e3f70ee68d462362939bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.secondworld.sdk.utils;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
 
import com.secondworld.sdk.R;
 
public class WebViewUtil {
 
    public static WebViewUtil I = new WebViewUtil();
 
    RelativeLayout webContainer;
    private WebView webView;
    private ImageButton btnClose;
 
    private boolean isShow;
 
    private boolean isValid() {
        return webContainer != null && btnClose != null && webView != null;
    }
 
    @SuppressLint("SetJavaScriptEnabled")
    public void init(Activity gameActivity, RelativeLayout webContainer) {
        this.webContainer = webContainer;
        webView = webContainer.findViewById(R.id.webView);
        btnClose = webContainer.findViewById(R.id.btnWebClose);
        btnClose.setOnClickListener(v -> close());
        close();
        //设置大小
        DisplayMetrics _dm = new DisplayMetrics();
        gameActivity.getWindowManager().getDefaultDisplay().getMetrics(_dm);
        final float scale = _dm.widthPixels * 1f / 1344;
        final int width = (int) (scale * 982);
        final int height = (int) (scale * 560);
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(
                webView.getLayoutParams());
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                margin);
        layoutParams.width = width;
        layoutParams.height = height;
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,
                RelativeLayout.TRUE);
        webView.setLayoutParams(layoutParams);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (isShow)
                    show();
            }
 
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http") || url.startsWith("https")) { //http和https协议开头的执行正常的流程
                    view.loadUrl(url);
                    return true;
                } else {  //其他的URL则会开启一个Acitity然后去调用原生APP
                    return super.shouldOverrideUrlLoading(view, url);
                }
            }
        });
    }
 
    /**
     * 打开网页
     *
     * @param url   地址
     * @param delay 是否延迟显示(加载完成才显示)
     */
    public void open(String url, boolean delay) {
        if (!isValid())
            return;
        webView.post(new Runnable() {
            @Override
            public void run() {
                webView.loadUrl(url);
                isShow = true;
                if (!delay)
                    show();
            }
        });
    }
 
    private void show() {
        webContainer.setVisibility(View.VISIBLE);
    }
 
    public void close() {
        if (!isValid())
            return;
        webView.clearFocus();
        webView.clearCache(true);
        webView.clearHistory();
        webView.clearFormData();
        isShow = false;
        webContainer.setVisibility(View.GONE);
    }
 
}