程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java制造告白圖片主動輪播控件

java制造告白圖片主動輪播控件

編輯:關於JAVA

java制造告白圖片主動輪播控件。本站提示廣大學習愛好者:(java制造告白圖片主動輪播控件)文章只能為提供參考,不一定能成為您想要的結果。以下是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;
  }
   
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved