直接贴代码了
先看看布局:
接下来看看怎么实现的
package com.yoolink.widget;
import java.util.ArrayList;
import com.epay.impay.ui.youle.bmjfsh.R;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 广告图片自动轮播控件
*
* @author chen xiangxiang
*/
public class ImageCycleView extends LinearLayout {
/**
* 上下文
*/
private Context mContext;
/**
* 图片轮播视图
*/
private ViewPager mAdvPager = null;
/**
* 滚动图片视图适配
*/
private ImageCycleAdapter mAdvAdapter;
/**
* 图片轮播指示器控件
*/
private ViewGroup mGroup;
/**
* 图片轮播指示个图
*/
private ImageView mImageView = null;
private TextView number = null;
/**
* 滚动图片指示视图列表
*/
private ImageView[] mImageViews = null;
/**
* 图片滚动当前图片下标
*/
private int mImageIndex = 0;
/**
* 手机密度
*/
private float mScale;
private boolean isStop;
private TextView[] mTextViews;
private TextView mTextView;
// private ViewGroup mGroup2;
ArrayList imageNameList;
/**
* @param context
*/
public ImageCycleView(Context context) {
super(context);
}
/**
* @param context
* @param attrs
*/
public ImageCycleView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mScale = context.getResources().getDisplayMetrics().density;
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);
// imageName = (TextView) findViewById(R.id.viewGroup2);
}
/**
* 装填图片数据
*
* @param imageUrlList
* @param imageCycleViewListener
*/
public void setImageResources(ArrayList imageUrlList, ImageCycleViewListener imageCycleViewListener) {
// this.imageNameList = imageNameList;
// 清除
mGroup.removeAllViews();
// 图片广告数量
final int imageCount = imageUrlList.size();
mImageViews = new ImageView[imageCount];
for (int i = 0; i < imageCount; i++) {
mImageView = new ImageView(mContext);
int imageParams_w = (int) (mScale * 10 + 0.5f);// XP与DP转换,适应应不同分辨率
int imageParams_h = (int) (mScale * 10 + 0.5f);// XP与DP转换,适应应不同分辨率
int imagePadding = (int) (mScale * 13 + 0.5f);
LayoutParams params = new LayoutParams(imageParams_w, imageParams_h);
params.leftMargin = 25;
mImageView.setScaleType(ScaleType.FIT_XY);
mImageView.setLayoutParams(params);
mImageView.setPadding(imagePadding, imagePadding, imagePadding, imagePadding);
mImageViews[i] = mImageView;
if (i == 0) {
mImageViews[i].setBackgroundResource(R.drawable.dot_select);
} else {
mImageViews[i].setBackgroundResource(R.drawable.dot_normal);
}
mGroup.addView(mImageViews[i]);
}
// imageName.setText(imageNameList.get(0));
mAdvAdapter = new ImageCycleAdapter(mContext, imageUrlList, imageNameList, imageCycleViewListener);
mAdvPager.setAdapter(mAdvAdapter);
mAdvPager.setCurrentItem(60);
startImageTimerTask();
}
/**
* 图片轮播(手动控制自动轮播与否,便于资源控件)
*/
public void startImageCycle() {
startImageTimerTask();
}
/**
* 暂停轮播—用于节省资源
*/
public void pushImageCycle() {
stopImageTimerTask();
}
/**
* 图片滚动任务
*/
private void startImageTimerTask() {
stopImageTimerTask();
// 图片滚动
mHandler.postDelayed(mImageTimerTask, 3000);
}
/**
* 停止图片滚动任务
*/
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) {
int index = mAdvPager.getCurrentItem() + 1;
mAdvPager.setCurrentItem(index);
if (!isStop) { // if isStop=true //当你退出后 要把这个给停下来 不然 这个一直存在
// 就一直在后台循环
mHandler.postDelayed(mImageTimerTask, 3000);
}
}
}
};
/**
* 轮播图片监听
*/
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;
// 设置当前显示的图片
mImageIndex = index;
// 设置图片滚动指示器背
mImageViews[index].setBackgroundResource(R.drawable.dot_select);
// imageName.setText(imageNameList.get(index));
for (int i = 0; i < mImageViews.length; i++) {
if (index != i) {
mImageViews[i].setBackgroundResource(R.drawable.dot_normal);
}
}
}
}
private class ImageCycleAdapter extends PagerAdapter {
/**
* 图片视图缓存列表
*/
private ArrayList mImageViewCacheList;
/**
* 图片资源列表
*/
private ArrayList mAdList = new ArrayList();
private ArrayList nameList = new ArrayList();
/**
* 广告图片点击监听
*/
private ImageCycleViewListener mImageCycleViewListener;
private Context mContext;
public ImageCycleAdapter(Context context, ArrayList adList, ArrayList nameList,
ImageCycleViewListener imageCycleViewListener) {
this.mContext = context;
this.mAdList = adList;
this.nameList = nameList;
mImageCycleViewListener = imageCycleViewListener;
mImageViewCacheList = new ArrayList();
}
@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) {
String imageUrl = mAdList.get(position % mAdList.size());
int po = position % mAdList.size();
ImageView imageView = null;
if (mImageViewCacheList.isEmpty()) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// 设置图片点击监听
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mImageCycleViewListener.onImageClick(position % mAdList.size(), v);
}
});
} else {
imageView = mImageViewCacheList.remove(0);
}
imageView.setTag(imageUrl);
container.addView(imageView);
mImageCycleViewListener.displayImage(po, imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ImageView view = (ImageView) object;
mAdvPager.removeView(view);
mImageViewCacheList.add(view);
}
}
/**
* 轮播控件的监听事件
*
* @author chen xiangxiang
*/
public static interface ImageCycleViewListener {
/**
* 加载图片资源
*
* @param imageURL
* @param imageView
*/
public void displayImage(int position, ImageView imageView);
/**
* 单击图片事件
*
* @param position
* @param imageView
*/
public void onImageClick(int position, View imageView);
}
}
最后看看怎么用的吧
mAdView = (ImageCycleView) mView.findViewById(R.id.ad_view);
mAdView.setImageResources(mImageUrl, mAdCycleViewListener);
}
private ImageCycleViewListener mAdCycleViewListener = new ImageCycleViewListener() {
@Override
public void onImageClick(int position, View imageView) {
// TODO 单击图片处理事件
}
@Override
public void displayImage(int po, ImageView imageView) {
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisc().cacheInMemory()
.showImageOnFail(R.drawable.banner1).build();
ImageLoader.getInstance().displayImage(mImageUrl.get(po), imageView, options);// 此处本人使用了ImageLoader对图片进行加装!
}
};