1
1
package com .uuch .adlibrary ;
2
2
3
3
import android .app .Activity ;
4
+ import android .graphics .Color ;
4
5
import android .os .Handler ;
5
6
import android .support .v4 .view .PagerAdapter ;
6
7
import android .support .v4 .view .ViewPager ;
7
8
import android .util .DisplayMetrics ;
8
9
import android .view .LayoutInflater ;
9
10
import android .view .View ;
10
11
import android .view .ViewGroup ;
11
- import android .view .Window ;
12
12
import android .widget .ImageView ;
13
13
import android .widget .RelativeLayout ;
14
- import android .widget .Toast ;
15
14
16
15
import com .flyco .pageindicator .indicator .FlycoPageIndicaor ;
16
+ import com .uuch .adlibrary .bean .AdInfo ;
17
+ import com .uuch .adlibrary .utils .DisplayUtil ;
18
+
17
19
import java .util .List ;
18
20
19
21
/**
@@ -27,47 +29,44 @@ public class AdManager {
27
29
private View contentView ;
28
30
private ViewPager viewPager ;
29
31
private RelativeLayout adRootContent ;
30
- private int padding = 44 ;
31
32
private AdAdapter adAdapter ;
32
33
private FlycoPageIndicaor mIndicator ;
33
34
private AnimDialogUtils animDialogUtils ;
34
35
List <AdInfo > advInfoListList ;
35
- private boolean isShowing = false ;
36
+ /**
37
+ * 广告弹窗距离两侧的距离-单位(dp)
38
+ */
39
+ private int padding = 44 ;
40
+ /**
41
+ * 广告弹窗的宽高比
42
+ */
43
+ private float widthPerHeight = 0.75f ;
44
+
45
+ private OnImageClickListener onImageClickListener = null ;
36
46
37
47
38
48
public AdManager (Activity context , List <AdInfo > advInfoListList ) {
39
49
this .context = context ;
40
50
this .advInfoListList = advInfoListList ;
41
-
42
51
}
43
52
44
53
private View .OnClickListener imageOnClickListener = new View .OnClickListener () {
45
54
@ Override
46
55
public void onClick (View view ) {
47
56
48
57
AdInfo advInfo = (AdInfo ) view .getTag ();
49
- if (advInfo != null ) {
50
-
51
- Toast .makeText (context , "点击事件!!!" , Toast .LENGTH_SHORT ).show ();
58
+ if (advInfo != null && onImageClickListener != null ) {
59
+ onImageClickListener .onImageClick (view , advInfo );
52
60
}
53
61
}
54
62
};
55
63
56
- View .OnClickListener closeClickListener = new View .OnClickListener () {
57
- @ Override
58
- public void onClick (View view ) {
59
- isShowing = false ;
60
- }
61
- };
62
-
63
- public boolean isShowing () {
64
- return isShowing ;
65
- }
66
-
64
+ /**
65
+ * 开始执行显示广告弹窗的操作
66
+ * @param animType
67
+ */
67
68
public void showAdDialog (final int animType ) {
68
69
69
- isShowing = true ;
70
-
71
70
contentView = LayoutInflater .from (context ).inflate (R .layout .ad_dialog_content_layout , null );
72
71
adRootContent = (RelativeLayout ) contentView .findViewById (R .id .ad_root_content );
73
72
@@ -76,50 +75,41 @@ public void showAdDialog(final int animType) {
76
75
77
76
adAdapter = new AdAdapter ();
78
77
viewPager .setAdapter (adAdapter );
78
+
79
79
mIndicator .setViewPager (viewPager );
80
80
isShowIndicator ();
81
81
82
- animDialogUtils = AnimDialogUtils .getInstance (context ).initView (contentView , closeClickListener );
82
+ animDialogUtils = AnimDialogUtils .getInstance (context )
83
+ .setAnimBackViewTransparent (false )
84
+ .setDialogCloseable (true )
85
+ .setDialogBackViewColor (Color .parseColor ("#AA333333" ))
86
+ .initView (contentView );
83
87
setRootContainerHeight ();
84
88
85
89
// 延迟1s展示,为了避免ImageLoader还为加载完缓存图片时就展示了弹窗的情况
86
90
new Handler ().postDelayed (new Runnable () {
87
91
@ Override
88
92
public void run () {
89
93
animDialogUtils .show (animType );
90
-
91
94
}
92
95
}, 1000 );
93
96
}
94
97
95
- public void dismissAdDialog () {
96
- animDialogUtils .dismiss ();
97
- }
98
-
99
-
100
98
/**
101
- * 直接删除地图页的广告弹窗,此方法用在账户异常切换后,另一个账户首页加载的界面不是地图页,而切换之前的用户在地图页,且已经弹出了广告弹窗且未关闭
102
- * 这种场景,使用该方法删除地图页广告弹窗
103
- *
104
- * @param context
99
+ * 开始执行销毁弹窗的操作
105
100
*/
106
- public static void removeAdDialog (Activity context ) {
107
- if (context != null && context instanceof Activity ) {
108
- ViewGroup androidContentView = (ViewGroup ) context .getWindow ().findViewById (Window .ID_ANDROID_CONTENT );
109
- View rootView = androidContentView .findViewWithTag (AnimDialogUtils .ANIM_DIALOG_TAG );
110
- if (rootView != null ) {
111
- androidContentView .removeView (rootView );
112
- }
113
- }
101
+ public void dismissAdDialog () {
102
+ animDialogUtils .dismiss (AdConstant .ANIM_STOP_DEFAULT );
114
103
}
115
104
105
+
116
106
private void setRootContainerHeight () {
117
107
118
108
context .getWindowManager ().getDefaultDisplay ().getMetrics (displayMetrics );
119
109
int widthPixels = displayMetrics .widthPixels ;
120
110
int totalPadding = DisplayUtil .dip2px (context , padding * 2 );
121
111
int width = widthPixels - totalPadding ;
122
- final int height = (int ) (width / 3.0f * 4.0f );
112
+ final int height = (int ) (width / widthPerHeight );
123
113
ViewGroup .LayoutParams params = adRootContent .getLayoutParams ();
124
114
params .height = height ;
125
115
}
@@ -174,4 +164,30 @@ public Object instantiateItem(ViewGroup container, int position) {
174
164
}
175
165
176
166
167
+
168
+ // ######################## 点击事件处理操作类 ########################
169
+
170
+ public interface OnImageClickListener {
171
+
172
+ public void onImageClick (View view , AdInfo advInfo );
173
+
174
+ }
175
+
176
+ // ######################## get set方法 #########################
177
+
178
+ public AnimDialogUtils getAnimDialogUtils () {
179
+ return animDialogUtils ;
180
+ }
181
+
182
+ public void setPadding (int padding ) {
183
+ this .padding = padding ;
184
+ }
185
+
186
+ public void setWidthPerHeight (float widthPerHeight ) {
187
+ this .widthPerHeight = widthPerHeight ;
188
+ }
189
+
190
+ public void setOnImageClickListener (OnImageClickListener onImageClickListener ) {
191
+ this .onImageClickListener = onImageClickListener ;
192
+ }
177
193
}
0 commit comments