public class ImageCycleView extends LinearLayout {
public static boolean isenaled = true;
//时间
public static int time;
/**
* 上下文
*/
private Context mContext;
/**
* 图片轮播视图
*/
private ViewPager mAdvPager = null;
/**
* 滚动图片视图适配
*/
private ImageCycleAdapter mAdvAdapter;
/**
* 图片轮播指示器控件
*/
private ViewGroup mGroup;
/**
* 图片轮播指示个图
*/
private ImageView mImageView = null;
/**
* 滚动图片指示视图列表
*/
private ImageView[] mImageViews = null;
/**
* 图片滚动当前图片下标
*/
private boolean isStop;
ArrayList<String> imageNameList;
/**
* @param context
*/
public ImageCycleView(Context context) {
super(context);
}
/**
* @param context
* @param attrs
*/
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());
mAdvPager.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
// 开始图片滚动
startImageTimerTask();
break;
default:
// 停止图片滚动
stopImageTimerTask();
break;
}
return false;
}
});
// 滚动图片右下指示器视
mGroup = (ViewGroup) findViewById(R.id.viewGroup);
}
/**
* 装填图片数据
*
* @param imageUrlList
* @param imageCycleViewListener
*/
public void setImageResources(ArrayList<AdEntity> imageUrlList,
ImageCycleViewListener imageCycleViewListener) {
// 清除
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) {
mImageViews[i]
.setBackgroundResource(R.drawable.indicators_now);
} else {
mImageViews[i]
.setBackgroundResource(R.drawable.indicators_default);
}
mGroup.addView(mImageViews[i]);
}
mAdvAdapter = new ImageCycleAdapter(mContext, imageUrlList,
imageCycleViewListener);
mAdvPager.setAdapter(mAdvAdapter);
startImageTimerTask();
}
/**
* 图片轮播(手动控制自动轮播与否,便于资源控件)
*/
public void startImageCycle() {
startImageTimerTask();
}
/**
* 暂停轮播—用于节省资源
*/
public void pushImageCycle() {
stopImageTimerTask();
}
/**
* 图片滚动任务
*/
private void startImageTimerTask() {
stopImageTimerTask();
// 图片滚动
mHandler.postDelayed(mImageTimerTask, time * 1000);
}
/**
* 停止图片滚动任务
*/
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, 3000);
}
}
}
};
/**
* 轮播图片监听
*
* @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;
// 设置当前显示的图片
// 设置图片滚动指示器背
mImageViews[index]
.setBackgroundResource(R.drawable.indicators_now);
for (int i = 0; i < mImageViews.length; i++) {
if (index != i) {
mImageViews[i]
.setBackgroundResource(R.drawable.indicators_default);
}
}
}
}
private class ImageCycleAdapter extends PagerAdapter {
/**
* 图片视图缓存列表
*/
private ArrayList<SmartImageView> mImageViewCacheList;
/**
* 图片资源列表
*/
private ArrayList<AdEntity> mAdList = new ArrayList<AdEntity>();
/**
* 广告图片点击监听
*/
private ImageCycleViewListener mImageCycleViewListener;
private Context mContext;
public ImageCycleAdapter(Context context, ArrayList<AdEntity> adList,
ImageCycleViewListener imageCycleViewListener) {
this.mContext = context;
this.mAdList = adList;
mImageCycleViewListener = imageCycleViewListener;
mImageViewCacheList = new ArrayList<SmartImageView>();
}
@Override
public int getCount() {
// return mAdList.size();
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
AdEntity entity = mAdList.get(position % mAdList.size());
//String imageUrl = mAdList.get(position % mAdList.size());
SmartImageView imageView = null;
if (mImageViewCacheList.isEmpty()) {
imageView = new SmartImageView(mContext);
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// test
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// 设置图片点击监听
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (isenaled == true) {
mImageCycleViewListener.onImageClick(position % mAdList.size(), v);
}
}
});
} else {
imageView = mImageViewCacheList.remove(0);
}
imageView.setTag(entity.getAdimg());
container.addView(imageView);
imageView.setImageUrl(entity.getAdimg());
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);
}
来源:https://blog.csdn.net/u014434239/article/details/99677958