首页图片的轮播
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/** * 广告图片自动轮播控件</br> * */ public class ImageCycleView extends LinearLayout { /** * 上下文 */ private Context mContext; /** * 图片轮播视图 */ private ViewPager mAdvPager = null ; /** * 滚动图片视图适配 */ private ImageCycleAdapter mAdvAdapter; /** * 图片轮播指示器控件 */ private ViewGroup mGroup; /** * 图片轮播指示个图 */ private ImageView mImageView = null ; /** * 滚动图片指示视图列表 */ private ImageView[] mImageViews = null ; /** * 图片滚动当前图片下标 */ private boolean isStop; /** * 游标是圆形还是长条,要是设置为0是长条,要是1就是圆形 默认是圆形 */ public int stype= 1 ; /** * @param context */ public ImageCycleView(Context context) { super (context); } /** * @param context * @param attrs */ @SuppressLint ( "Recycle" ) public ImageCycleView(Context context, AttributeSet attrs) { super (context, attrs); mContext = context; LayoutInflater.from(context).inflate(R.layout.ad_cycle_view, this ); mAdvPager = (ViewPager) findViewById(R.id.adv_pager); mAdvPager.setOnPageChangeListener( new GuidePageChangeListener()); // 滚动图片右下指示器视 mGroup = (ViewGroup) findViewById(R.id.viewGroup); } /** * 触摸停止计时器,抬起启动计时器 */ @Override public boolean dispatchTouchEvent(MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_UP){ // 开始图片滚动 startImageTimerTask(); } else { // 停止图片滚动 stopImageTimerTask(); } return super .dispatchTouchEvent(event); } /** * 装填图片数据 * * @param imageUrlList * @param imageCycleViewListener */ public void setImageResources(ArrayList<Integer> imageUrlList ,ImageCycleViewListener imageCycleViewListener, int stype){ this .stype=stype; // 清除 mGroup.removeAllViews(); // 图片广告数量 final int imageCount = imageUrlList.size(); mImageViews = new ImageView[imageCount]; for ( int i = 0 ; i < imageCount; i++) { mImageView = new ImageView(mContext); LayoutParams params= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.leftMargin= 30 ; mImageView.setScaleType(ScaleType.CENTER_CROP); mImageView.setLayoutParams(params); mImageViews[i] = mImageView; if (i == 0 ) { if ( this .stype== 1 ) // mImageViews[i].setBackgroundResource(R.drawable.banner_dian_focus); mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_focus); //换点 else mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_focus); } else { if ( this .stype== 1 ) // mImageViews[i].setBackgroundResource(R.drawable.banner_dian_blur); mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); else mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); } mGroup.addView(mImageViews[i]); } mAdvAdapter = new ImageCycleAdapter(mContext, imageUrlList ,imageCycleViewListener); mAdvPager.setAdapter(mAdvAdapter); mAdvPager.setCurrentItem(Integer.MAX_VALUE/ 2 ); startImageTimerTask(); } public void setImageResources2(ArrayList<Integer> imageUrlList ,ImageCycleViewListener imageCycleViewListener, int stype){ this .stype=stype; // 清除 mGroup.removeAllViews(); // 图片广告数量 final int imageCount = imageUrlList.size(); mImageViews = new ImageView[imageCount]; for ( int i = 0 ; i < imageCount; i++) { mImageView = new ImageView(mContext); LayoutParams params= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.leftMargin= 30 ; mImageView.setScaleType(ScaleType.CENTER_CROP); mImageView.setLayoutParams(params); mImageViews[i] = mImageView; if (i == 0 ) { if ( this .stype== 1 ) // mImageViews[i].setBackgroundResource(R.drawable.banner_dian_focus); mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_focus); //换点 else mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_focus); } else { if ( this .stype== 1 ) // mImageViews[i].setBackgroundResource(R.drawable.banner_dian_blur); mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); else mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); } mGroup.addView(mImageViews[i]); } mAdvAdapter = new ImageCycleAdapter(mContext, imageUrlList ,imageCycleViewListener); mAdvPager.setAdapter(mAdvAdapter); mAdvPager.setCurrentItem(Integer.MAX_VALUE/ 2 ); startImageTimerTask(); } /** * 图片轮播(手动控制自动轮播与否,便于资源控件) */ public void startImageCycle() { startImageTimerTask(); } /** * 暂停轮播—用于节省资源 */ public void pushImageCycle() { stopImageTimerTask(); } /** * 图片滚动任务 */ private void startImageTimerTask() { stopImageTimerTask(); // 图片滚动 mHandler.postDelayed(mImageTimerTask, 5000 ); } /** * 停止图片滚动任务 */ private void stopImageTimerTask() { isStop= true ; mHandler.removeCallbacks(mImageTimerTask); } private Handler mHandler = new Handler(); /** * 图片自动轮播Task */ private Runnable mImageTimerTask = new Runnable() { @Override public void run() { if (mImageViews != null ) { mAdvPager.setCurrentItem(mAdvPager.getCurrentItem()+ 1 ); if (!isStop){ //if isStop=true //当你退出后 要把这个给停下来 不然 这个一直存在 就一直在后台循环 mHandler.postDelayed(mImageTimerTask, 5000 ); } } } }; /** * 轮播图片监听 * * @author minking */ private final class GuidePageChangeListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged( int state) { if (state == ViewPager.SCROLL_STATE_IDLE) startImageTimerTask(); } @Override public void onPageScrolled( int arg0, float arg1, int arg2) { } @Override public void onPageSelected( int index) { index=index%mImageViews.length; // 设置当前显示的图片 // 设置图片滚动指示器背 if (stype!= 1 ) mImageViews[index].setBackgroundResource(R.drawable.cicle_banner_dian_focus); else mImageViews[index].setBackgroundResource(R.drawable.cicle_banner_dian_focus); for ( int i = 0 ; i < mImageViews.length; i++) { if (index != i) { if (stype!= 1 ) mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); else mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian_blur); } } } } private class ImageCycleAdapter extends PagerAdapter { /** * 图片视图缓存列表 */ private ArrayList<SmartImageView> mImageViewCacheList; /** * 图片资源列表 */ //private ArrayList<String> mAdList = new ArrayList<String>(); private ArrayList<Integer> mAdList = new ArrayList<Integer>(); /** * 广告图片点击监听 */ private ImageCycleViewListener mImageCycleViewListener; private Context mContext; // public ImageCycleAdapter(Context context, ArrayList<String> adList , ImageCycleViewListener imageCycleViewListener) { // this.mContext = context; // this.mAdList = adList; // mImageCycleViewListener = imageCycleViewListener; // mImageViewCacheList = new ArrayList<SmartImageView>(); // } public ImageCycleAdapter(Context context, ArrayList<Integer> adList , ImageCycleViewListener imageCycleViewListener) { this .mContext = context; this .mAdList = adList; mImageCycleViewListener = imageCycleViewListener; mImageViewCacheList = new ArrayList<SmartImageView>(); } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object obj) { return view == obj; } @Override public Object instantiateItem(ViewGroup container, final int position) { int imageUrl = mAdList.get(position%mAdList.size()); Log.e( "imageUrl:" ,position%mAdList.size()+ "--" +imageUrl+ "" ); SmartImageView imageView = null ; if (mImageViewCacheList.isEmpty()) { imageView = new SmartImageView(mContext); imageView.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); //test imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); } else { imageView = mImageViewCacheList.remove( 0 ); } imageView.setTag(imageUrl); container.addView(imageView); //imageView.setImageUrl(imageUrl); imageView.setBackgroundResource(imageUrl); // 设置图片点击监听 imageView.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { mImageCycleViewListener.onImageClick(position%mAdList.size(), v); } }); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { SmartImageView view = (SmartImageView) object; mAdvPager.removeView(view); mImageViewCacheList.add(view); } } /** * 轮播控件的监听事件 * * @author minking */ public static interface ImageCycleViewListener { /** * 单击图片事件 * * @param position * @param imageView */ public void onImageClick( int position, View imageView); } } |
我们再来看个基于安卓ViewPager的图片轮播控件
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package me.lanfog.myandroid.widget; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; public class PageFlipper extends ViewPager { private String TAG = PageFlipper. class .getSimpleName(); private List<View> views; private PagerAdapter adapter = new PagerAdapter() { @Override public Object instantiateItem(ViewGroup container, int position) { View v = views.get(position); container.addView(v); return v; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public int getItemPosition(Object object) { return views.indexOf(object); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); } @Override public int getCount() { return views == null ? 0 : views.size(); } }; private OnPageChangeListener listener = new OnPageChangeListener() { /** * 将控件位置转化为数据集中的位置 */ public int convert( int position){ return position == 0 ? views.size()- 1 : ( position > views.size() ? 0 : position- 1 ); } @Override public void onPageSelected( int position) { if (listener2 != null ){ listener2.onPageSelected(convert(position)); } } @Override public void onPageScrolled( int position, float percent, int offset) { if (listener2 != null ){ listener2.onPageScrolled(convert(position), percent, offset); } if (percent == 0 ){ if (position == 0 ) // 切换到倒数第二页 setCurrentItem(( views.size() - 2 ) % views.size(), false ); else if (position == views.size() - 1 ) // 切换到正数第二页 setCurrentItem( 1 , false ); } } @Override public void onPageScrollStateChanged( int state) { if (listener2 != null ){ listener2.onPageScrollStateChanged(state); } switch (state) { case SCROLL_STATE_IDLE: // 闲置 if (!handler.hasMessages(START_FLIPPING)) handler.sendEmptyMessageDelayed(START_FLIPPING, 3000 ); // 延时滚动 break ; case SCROLL_STATE_DRAGGING: // 拖动中 handler.sendEmptyMessage(STOP_FLIPPING); // 取消滚动 break ; case SCROLL_STATE_SETTLING: // 拖动结束 break ; } } }, listener2; private final int START_FLIPPING = 0 ; private final int STOP_FLIPPING = 1 ; private Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case START_FLIPPING: if (views.size() > 3 ) // 因为前后页是辅助页,所以此处3也就是只有1页 setCurrentItem((getCurrentItem() + 1 ) % views.size()); handler.sendEmptyMessageDelayed(START_FLIPPING, 3000 ); // 延时滚动 break ; case STOP_FLIPPING: handler.removeMessages(START_FLIPPING); break ; } } }; public PageFlipper(Context context, AttributeSet attrs) { super (context, attrs); init(); } public PageFlipper(Context context) { super (context); init(); } private void init(){ setOffscreenPageLimit( 1 ); // 最大页面缓存数量 setAdapter(adapter); // 适配器 super .setOnPageChangeListener(listener); // 监听器 handler.sendEmptyMessageDelayed(START_FLIPPING, 3000 ); // 延时滚动 } public void setViews( int [] ids){ this .views = new ArrayList<View>(); for ( int i= 0 ;i<ids.length+ 2 ;i++){ // 头部新增一个尾页,尾部新增一个首页 ImageView iv = new ImageView(getContext()); iv.setImageResource(ids[i == 0 ? ids.length- 1 : ( i > ids.length ? 0 : i- 1 )]); iv.setScaleType(ImageView.ScaleType.FIT_XY); this .views.add(iv); } setCurrentItem( 1 ); // 首页 this .adapter.notifyDataSetChanged(); } @Override public void setOnPageChangeListener(OnPageChangeListener listener) { this .listener2 = listener; } } |