首頁圖片的輪播
/** * 廣告圖片自動輪播控制</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_ager); (new GuidePageChangeListener()); // 滾動圖片右下指示器視mGroup = (ViewGroup) findViewById(R.id.viewGroup); } /** * 觸摸停止計時器,抬起啟動計時器*/ @Override public Eventlean dispatchTouchouchTouchTouch (MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_UP){ //開始圖片滾動startImageTimerTask(); }else{ // 停止圖片滾動stopImageTimerTask(); } return super.dispatchTouchEvent(event); } /** * 裝填圖片資料* * @param imageUrlList *set @param imageCycleViewListener (ArrayList<Integer> imageUrlList ,ImageCycleViewListener imageCycleViewListener,int stype){ this.stype=stype; // 清除mGroup.removeAllViews(); // 圖片廣告數量final int imageCount = imageUrlList.size(); mImageViews = new Images [ = 0; i < imageCount; i++) { mImageView = new ImageView(mContext); LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.leftMargin=30; mImageView.WRAP_CONTENT); params.leftMargin=30; mImageView.set.ScagedIale.Fyset.Fy); mImageViews[i] = mImageView; if (i == 0) { if(this.stype==1)// mImageViews[i].setBackgroundResource(R.drawable.banner_dian_focus); mImageViews[i].setBackgroundResource(R.dabledian_focus); mImageViews[i].setBackgroundResource(R.dable .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.ViewView(mImaged imageUrlList ,imageCycleViewListener); mAdvPager.setAdapter(mAdvAdapter); mAdvPager.setCurrentItem(Integer.MAX_VALUE/2); startImageTimerTask(); } public void setImageResources2(ArListyListIs2(); imageCycleViewListener,int stype){ this.stype=stype; // 清除mGroup.removeAllViews(); // 圖片廣告數量final int imageCount = imageUrlList.size(); mImageViews = new ImageView[imageCount]; for (int i =Views = new ImageView[imageCount]; ; i < imageCount; i++) { mImageView = new ImageView(mContext); LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.leftMargin=30; mImageView.setScaleType(ScaleType.CENTERIm.L. = mImageView; if (i == 0) { if(this.stype==1)// mImageViews[i].setBackgroundResource(R.drawable.banner_dian_focus); mImageViews[i].setBackgroundResource(R.drawable.cicle_banner_dian); //換點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.ViewView(mImaged imageUrlList ,imageCycleViewListener); mAdvPager.setAdapter(mAdvAdapter); mAdvPager.setCurrentItem(Integer.MAX_VALUE/2); startImageTimerTask(); } /** * 圖片控制輪播(手動控制輪播與否,方便自動播資源) * public void startImageCycle() { startImageTimerTask(); } /** * 暫停輪播用於節省資源*/ public void pushImageCycle() { stopImageTimerTask(); } /** * 圖片滾動任務*/ private void startImageTimerTask() { stopImage/erTage; /圖片捲動mHandler.postDelayed(mImageTimerTask, 5000); } /** * 停止圖片滾動任務*/ private void stopImageTimerTask() { isStop=true; mHandler.removeCallbacks(mImageTimerTask); ** * 圖片自動輪播Task */ private Runnable mImageTimerTask = new Runnable() { @Override public void run() { if (mImageViews != null) { mAdvPager.setCurrentItem(mAdvPager.getCurrentItem()+1); if(!isStop){if isStop(!isStop //當你退出後要把這個給停下來不然這個一直存在就一直在後台循環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) {Pinride arg2) {P. 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[Resource].setBgroundBground .drawable.cicle_banner_dian_blur); } } } } private class ImageCycleAdapter extends PagerAdapter { /** * 圖片視圖快取清單*/ private ArrayList<SmartImageView> mImageViewCacheList; /** * 圖片資源清單*/ //private ArrayList<AAdList private ArrayList<Integer> mAdList = new ArrayList<Integer>(); /** * 廣告圖片點擊監聽*/ private ImageCycleViewListener mImageCycleViewListener; private Context mContext;/// public ImageCycleAdapter(Context context, ArrayList<String> adList View, ImageCycleAdapter(Context context, ArrayList<String> adList View, ImageCyListm 看到;// this.mAdList = adList;// mImageCycleViewListener = imageCycleViewListener;// mImageViewCacheList = new ArrayList<SmartImageView>();// } public ImageCycleAdapter(Context context; this.mAdList = adList; mImageCycleViewListener = imageCycleViewListener; mImageViewCacheList = new ArrayList<SmartImageView>(); } @Override public int getCount() { return Integer.MAX_VALUE] 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()); 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_CONTENTA); else { imageView = mImageViewCacheList.remove(0); } imageView.setTag(imageUrl); container.addView(imageView); //imageView.setImageUrl(imageUrl); imageView.setBackgroundResource(imageUrl); 點選設定圖片. setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mImageCycleViewListener.onImageClick(position%mAdList.size(), v); } }); return imageView; } @Override public void destroyItem(ViewGroup container, imageView; } @Override public void destroyItem(ViewGroup container, imageView; } @Override public void destroy近view = (SmartImageView) object; mAdvPager.removeView(view); mImageViewCacheList.add(view); } } /** * 輪播控制項的監聽事件* * @author minking */ public static interface ImageCycleViewListener { /** * 點選圖片事件* * @paramic 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 = PageFlName.class.getSimple( ); 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 is0FromView(ViewF. , 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) ; 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 public void onPageScrolled(int position, float percent. (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 SCRO_STATE. //閒置if(!handler.hasMessages(START_FLIPPING)) handler.sendEmptyMessageDelayed(START_FLIPPING, 3000); // 延時捲動break; case SCROLL_STATE_DRAGGING: // 拖曳中移除中盤.sendbreakEmphandler.sendbreakEmp-Pessage(Wemage); SCROLL_STATE_SETTLING: // 拖曳結束break; } } }, listener2; private final int START_FLIPPING = 0; private final int STOP_FLIPPING = 1; private Handler handler = new Handler(){ public void leMessage(coid). 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(intoid] setViews{ 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 setOnPageChanListList listener2 = listener; } }