java制作广告图片自动轮播控件

首页图片的轮播

/**
 * 广告图片自动轮播控件</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的图片轮播控件

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;
  }
   
}