admin
2022-03-12 222df7b655c51992580b832f5e06c6772d27d9d6
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.demo.library_flutter.nativeview;
 
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
 
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.demo.lib.common.util.common.DimenUtils;
import com.demo.lib.common.util.ui.MyActivityManager;
import com.demo.library_ad.TTAdManagerHolder;
import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.ADSize;
import com.qq.e.ads.nativ.NativeExpressAD;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.comm.util.AdError;
 
import java.util.List;
import java.util.Map;
 
import androidx.annotation.NonNull;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
 
class GDTExpressNativeView implements PlatformView {
    private Map<String, Object> params;
    private NativeExpressAD nativeExpressAD;
    private MethodChannel methodChannel;
 
    public GDTExpressNativeView(int viewId, Map<String, Object> params, BinaryMessenger binaryMessenger) {
        this.params = params;
        methodChannel = new MethodChannel(binaryMessenger, "ad-gdt-express-view-" + viewId);
        methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                if ("refresh".equalsIgnoreCase(call.method)) {
                    //刷新
                    loadAd();
                }
            }
        });
    }
 
    private FrameLayout fl_container;
    private NativeExpressADView nativeExpressADView;
 
    @Override
    public View getView() {
        if (fl_container == null) {
            fl_container = new FrameLayout(getView().getContext());
        }
 
        if (nativeExpressAD == null) {
            float width = Float.parseFloat(params.get("width") + "");
            float height = Float.parseFloat(params.get("height") + "");
            String pid = params.get("pid") + "";
            nativeExpressAD = new NativeExpressAD(getView().getContext(), new ADSize(DimenUtils.dipToPixels((int) width, getView().getContext()), ADSize.AUTO_HEIGHT), pid, new NativeExpressAD.NativeExpressADListener() {
                @Override
                public void onADLoaded(List<NativeExpressADView> list) {
                    if (nativeExpressADView != null) {
                        nativeExpressADView.destroy();
                    }
                    nativeExpressADView = list.get(0);
                    nativeExpressADView.render();
                    if (fl_container.getChildCount() > 0) {
                        fl_container.removeAllViews();
                    }
                    if (nativeExpressADView.getParent() != null) {
                        ((ViewGroup) nativeExpressADView.getParent()).removeAllViews();
                    }
                    // 需要保证 View 被绘制的时候是可见的,否则将无法产生曝光和收益。
                    fl_container.addView(nativeExpressADView);
                }
 
                @Override
                public void onRenderFail(NativeExpressADView nativeExpressADView) {
                    methodChannel.invokeMethod("loadFail", "渲染失败");
                }
 
                @Override
                public void onRenderSuccess(NativeExpressADView nativeExpressADView) {
 
                }
 
                @Override
                public void onADExposure(NativeExpressADView nativeExpressADView) {
 
                }
 
                @Override
                public void onADClicked(NativeExpressADView nativeExpressADView) {
 
                }
 
                @Override
                public void onADClosed(NativeExpressADView nativeExpressADView) {
                    methodChannel.invokeMethod("close", null);
                }
 
                @Override
                public void onADLeftApplication(NativeExpressADView nativeExpressADView) {
 
                }
 
                @Override
                public void onNoAD(AdError adError) {
                    methodChannel.invokeMethod("loadFail", adError.getErrorMsg());
                }
            });
 
            nativeExpressAD.setVideoOption(new VideoOption.Builder()
                    .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // WIFI 环境下可以自动播放视频
                    .setAutoPlayMuted(true) // 自动播放时为静音
                    .build());
        }
        loadAd();
        return fl_container;
    }
 
    private void loadAd() {
        if (nativeExpressAD != null) {
            nativeExpressAD.loadAD(1);
        }
    }
 
    @Override
    public void dispose() {
 
    }
}