44import android .content .Context ;
55import android .content .Intent ;
66import android .content .IntentFilter ;
7- import android .util .Log ;
87import android .view .View ;
9- import android .view .ViewGroup ;
108import android .widget .FrameLayout ;
119
1210import androidx .annotation .NonNull ;
1715import com .zero .flutter_qq_ads .PluginDelegate ;
1816import com .zero .flutter_qq_ads .event .AdEventAction ;
1917import com .zero .flutter_qq_ads .load .FeedAdManager ;
20- import com .zero .flutter_qq_ads .utils .UIUtils ;
2118
22- import java .util .HashMap ;
2319import java .util .Map ;
2420
2521import io .flutter .plugin .common .MethodCall ;
2925/**
3026 * Feed 信息流广告 View
3127 */
32- class AdFeedView extends BaseAdPage implements PlatformView , View . OnLayoutChangeListener {
28+ class AdFeedView extends BaseAdPage implements PlatformView {
3329 private final String TAG = AdFeedView .class .getSimpleName ();
3430 @ NonNull
3531 private final FrameLayout frameLayout ;
3632 private final PluginDelegate pluginDelegate ;
3733 private int id ;
3834 private NativeExpressADView fad ;
39- private MethodChannel methodChannel ;
4035 private BroadcastReceiver receiver ;
4136
4237
4338 AdFeedView (@ NonNull Context context , int id , @ Nullable Map <String , Object > creationParams , PluginDelegate pluginDelegate ) {
4439 this .id = id ;
4540 this .pluginDelegate = pluginDelegate ;
46- methodChannel = new MethodChannel (this .pluginDelegate .bind .getBinaryMessenger (), PluginDelegate .KEY_FEED_VIEW + "/" + id );
4741 frameLayout = new FrameLayout (context );
48- FrameLayout .LayoutParams params = new FrameLayout .LayoutParams (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT );
49- frameLayout .setLayoutParams (params );
50- frameLayout .addOnLayoutChangeListener (this );
5142 MethodCall call = new MethodCall ("AdFeedView" , creationParams );
5243 showAd (this .pluginDelegate .activity , call );
5344 }
@@ -69,12 +60,11 @@ public void loadAd(@NonNull MethodCall call) {
6960 regReceiver (key );
7061 fad = FeedAdManager .getInstance ().getAd (key );
7162 if (fad != null ) {
72- View adView = fad .getRootView ();
73- if (adView .getParent () != null ) {
74- ((ViewGroup ) adView .getParent ()).removeAllViews ();
63+ if (frameLayout .getChildCount () > 0 ) {
64+ frameLayout .removeAllViews ();
7565 }
76- frameLayout .addView (adView );
7766 fad .render ();
67+ frameLayout .addView (fad );
7868 }
7969 }
8070
@@ -91,29 +81,13 @@ public void onReceive(Context context, Intent intent) {
9181 String event = intent .getStringExtra ("event" );
9282 if (AdEventAction .onAdClosed .equals (event ) || AdEventAction .onAdError .equals (event )) {
9383 AdFeedView .this .disposeAd ();
94- } else if (AdEventAction .onAdPresent .equals (event )) {
95- AdFeedView .this .resizeAdView ();
9684 }
9785 }
9886 };
9987 IntentFilter intentFilter = new IntentFilter (PluginDelegate .KEY_FEED_VIEW + "_" + key );
10088 LocalBroadcastManager .getInstance (activity ).registerReceiver (receiver , intentFilter );
10189 }
10290
103- /**
104- * 重新计算真实的广告 View 的宽高
105- */
106- private void resizeAdView () {
107- this .fad .measure (100 , 100 );
108- int mw = this .fad .getMeasuredWidth ();
109- int mh = this .fad .getMeasuredHeight ();
110- Log .d (TAG , "resizeAdView mw:" + mw + " mh:" + mh );
111- FrameLayout .LayoutParams params =new FrameLayout .LayoutParams (mw ,mh );
112- frameLayout .setLayoutParams (params );
113- frameLayout .requestLayout ();
114- setFlutterViewSize (mw ,mh );
115- }
116-
11791 /**
11892 * 移除广告
11993 */
@@ -134,39 +108,6 @@ private void disposeAd() {
134108 if (fad != null ) {
135109 fad .destroy ();
136110 }
137- // 更新宽高
138- setFlutterViewSize (0f , 0f );
139- }
140-
141- /**
142- * 设置 FlutterAds 视图宽高
143- *
144- * @param width 宽度
145- * @param height 高度
146- */
147- private void setFlutterViewSize (float width , float height ) {
148- int widthPd = UIUtils .px2dip (activity , width );
149- int heightPd = UIUtils .px2dip (activity , height );
150- Log .i (TAG , "onLayoutChange widthPd:" + widthPd + " heightPd:" + heightPd );
151- // 更新宽高
152- Map <String , Double > sizeMap = new HashMap <>();
153- sizeMap .put ("width" , (double ) widthPd );
154- sizeMap .put ("height" , (double ) heightPd );
155- if (methodChannel != null ) {
156- methodChannel .invokeMethod ("setSize" , sizeMap );
157- }
158- }
159-
160- @ Override
161- public void onLayoutChange (View v , int left , int top , int right , int bottom , int oldLeft , int oldTop , int oldRight , int oldBottom ) {
162- Log .i (TAG , "onLayoutChange left:" + left + " top:" + top + " right:" + right + " bottom:" + bottom );
163-
164- if (right > 0 && bottom > 0 ) {
165- int width = right - left ;
166- int height = bottom - top ;
167- Log .i (TAG , "onLayoutChange width:" + width + " height:" + height );
168- // setFlutterViewSize(width, height);
169- }
170111 }
171112
172113}
0 commit comments