广告牌 循环 轮播 图片
http://download.csdn.net/detail/stevenhu_223/8697903
先编写一个ViewPager:
package com.example.m_evolution; import android.content.Context; import android.database.DataSetObserver; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; public class CycleViewPager extends ViewPager { private InnerPagerAdapter mAdapter; public CycleViewPager(Context context) { super( context); setOnPageChangeListener( null); } public CycleViewPager(Context context, AttributeSet attrs) { super( context, attrs); setOnPageChangeListener( null); } @Override public void setAdapter(PagerAdapter arg0) { mAdapter = new InnerPagerAdapter( arg0); super.setAdapter( mAdapter); setCurrentItem(1); } @Override public void setOnPageChangeListener(OnPageChangeListener listener) { super.setOnPageChangeListener( new InnerOnPageChangeListener( listener)); } private class InnerOnPageChangeListener implements OnPageChangeListener { private OnPageChangeListener listener; private int position; public InnerOnPageChangeListener(OnPageChangeListener listener) { this.listener = listener; } @Override public void onPageScrollStateChanged(int arg0) { if(null != listener) { listener.onPageScrollStateChanged( arg0); } if(arg0 == ViewPager.SCROLL_STATE_IDLE) { if(position == mAdapter.getCount() - 1) { setCurrentItem( 1, false); } else if(position == 0) { setCurrentItem(mAdapter.getCount() - 2, false); } } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { if(null != listener) { listener.onPageScrolled( arg0, arg1, arg2); } } @Override public void onPageSelected(int arg0) { position = arg0; if(null != listener) { listener.onPageSelected( arg0); } } } private class InnerPagerAdapter extends PagerAdapter { private PagerAdapter adapter; public InnerPagerAdapter(PagerAdapter adapter) { this.adapter = adapter; adapter.registerDataSetObserver( new DataSetObserver() { @Override public void onChanged() { notifyDataSetChanged(); } @Override public void onInvalidated() { notifyDataSetChanged(); } }); } @Override public int getCount() { return adapter.getCount() + 2; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return adapter.isViewFromObject( arg0, arg1); } @Override public Object instantiateItem(ViewGroup container, int position) { if(position == 0) { position = adapter.getCount() - 1; } else if(position == adapter.getCount() + 1) { position = 0; } else { position -= 1; } return adapter.instantiateItem( container, position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { adapter.destroyItem( container, position, object); } } }
编写其控件View(继承与LinearLayout):
package com.example.m_evolution; import android.content.Context; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; 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.LinearLayout; import com.example.m_evolution.Bean.ADInfo; import java.util.ArrayList; /** * 广告图片自动轮播控件</br> * * <pre> * 集合ViewPager和指示器的一个轮播控件,主要用于一般常见的广告图片轮播,具有自动轮播和手动轮播功能 * 使用:只需在xml文件中使用{@code <com.minking.imagecycleview.ImageCycleView/>} , * 然后在页面中调用 {@link #setImageResources(ArrayList, ImageCycleViewListener) }即可! * * 另外提供{@link #startImageCycle() } \ {@link #pushImageCycle() }两种方法,用于在Activity不可见之时节省资源; * 因为自动轮播需要进行控制,有利于内存管理 * </pre> * */ public class ImageCycleView extends LinearLayout { /** * 上下文 */ private Context mContext; /** * 图片轮播视图 */ private CycleViewPager mBannerPager = null; /** * 滚动图片视图适配器 */ private ImageCycleAdapter mAdvAdapter; /** * 图片轮播指示器控件 */ private ViewGroup mGroup; /** * 图片轮播指示器-个图 */ private ImageView mImageView = null; /** * 滚动图片指示器-视图列表 */ private ImageView[] mImageViews = null; /** * 图片滚动当前图片下标 */ private int mImageIndex = 1; /** * 手机密度 */ private float mScale; /** * @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.view_banner_content, this); mBannerPager = (CycleViewPager) findViewById(R.id.pager_banner); mBannerPager.setOnPageChangeListener(new GuidePageChangeListener()); mBannerPager.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 imageCycleViewListener */ public void setImageResources(ArrayList<ADInfo> infoList, ImageCycleViewListener imageCycleViewListener) { // 清除所有子视图 mGroup.removeAllViews(); // 图片广告数量 final int imageCount = infoList.size(); mImageViews = new ImageView[imageCount]; for (int i = 0; i < imageCount; i++) { mImageView = new ImageView(mContext); int imageParams = (int) (mScale * 20 + 0.5f);// XP与DP转换,适应不同分辨率 int imagePadding = (int) (mScale * 5 + 0.5f); LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); layout.setMargins(3, 0, 3, 0); mImageView.setLayoutParams(layout); //mImageView.setPadding(imagePadding, imagePadding, imagePadding, imagePadding); mImageViews[i] = mImageView; if (i == 0) { mImageViews[i].setBackgroundResource(R.drawable.icon_point_pre); } else { mImageViews[i].setBackgroundResource(R.drawable.icon_point); } mGroup.addView(mImageViews[i]); } mAdvAdapter = new ImageCycleAdapter(mContext, infoList, imageCycleViewListener); mBannerPager.setAdapter(mAdvAdapter); startImageTimerTask(); } /** * 开始轮播(手动控制自动轮播与否,便于资源控制) */ public void startImageCycle() { startImageTimerTask(); } /** * 暂停轮播——用于节省资源 */ public void pushImageCycle() { stopImageTimerTask(); } /** * 开始图片滚动任务 */ private void startImageTimerTask() { stopImageTimerTask(); // 图片每3秒滚动一次 mHandler.postDelayed(mImageTimerTask, 3000); } /** * 停止图片滚动任务 */ private void stopImageTimerTask() { mHandler.removeCallbacks(mImageTimerTask); } private Handler mHandler = new Handler(); /** * 图片自动轮播Task */ private Runnable mImageTimerTask = new Runnable() { @Override public void run() { if (mImageViews != null) { // 下标等于图片列表长度说明已滚动到最后一张图片,重置下标 if ((++mImageIndex) == mImageViews.length + 1) { mImageIndex = 1; } mBannerPager.setCurrentItem(mImageIndex); } } }; /** * 轮播图片状态监听器 * * @author minking */ private final class GuidePageChangeListener implements ViewPager.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) { if (index == 0 || index == mImageViews.length + 1) { return; } // 设置图片滚动指示器背景 mImageIndex = index; index -= 1; mImageViews[index].setBackgroundResource(R.drawable.icon_point_pre); for (int i = 0; i < mImageViews.length; i++) { if (index != i) { mImageViews[i].setBackgroundResource(R.drawable.icon_point); } } } } private class ImageCycleAdapter extends PagerAdapter { /** * 图片视图缓存列表 */ private ArrayList<ImageView> mImageViewCacheList; /** * 图片资源列表 */ private ArrayList<ADInfo> mAdList = new ArrayList<ADInfo>(); /** * 广告图片点击监听器 */ private ImageCycleViewListener mImageCycleViewListener; private Context mContext; public ImageCycleAdapter(Context context, ArrayList<ADInfo> adList, ImageCycleViewListener imageCycleViewListener) { mContext = context; mAdList = adList; mImageCycleViewListener = imageCycleViewListener; mImageViewCacheList = new ArrayList<>(); } @Override public int getCount() { return mAdList.size(); } @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).getUrl(); ImageView imageView = null; if (mImageViewCacheList.isEmpty()) { imageView = new ImageView(mContext); imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); imageView.setScaleType(ImageView.ScaleType.FIT_XY); } else { imageView = mImageViewCacheList.remove(0); } // 设置图片点击监听 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mImageCycleViewListener.onImageClick(mAdList.get(position),position, v); } }); imageView.setTag(imageUrl); container.addView(imageView); mImageCycleViewListener.displayImage(imageUrl, imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ImageView view = (ImageView) object; container.removeView(view); mImageViewCacheList.add(view); } } /** * 轮播控件的监听事件 * * @author minking */ public static interface ImageCycleViewListener { /** * 加载图片资源 * * @param imageURL * @param imageView */ public void displayImage(String imageURL, ImageView imageView); /** * 单击图片事件 * * @param imageView */ public void onImageClick(ADInfo info, int postion, View imageView); } }
使用时,布局文件中添加:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.m_evolution.ImageCycleView android:id="@+id/ad_view" android:layout_width="fill_parent" android:layout_height="150dip" /> </RelativeLayout>
java中:
package com.example.m_evolution; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import com.example.m_evolution.Bean.ADInfo; import com.nostra13.universalimageloader.core.ImageLoader; public class ImageCycleActivity extends Activity { private ImageCycleView mAdView; private ArrayList<ADInfo> infos = new ArrayList<ADInfo>(); private String[] imageUrls = {"http://img.taodiantong.cn/v55183/infoimg/2013-07/130720115322ky.jpg", "http://pic30.nipic.com/20130626/8174275_085522448172_2.jpg", "http://pic18.nipic.com/20111215/577405_080531548148_2.jpg", "http://pic15.nipic.com/20110722/2912365_092519919000_2.jpg", "http://pic.58pic.com/58pic/12/64/27/55U58PICrdX.jpg"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ad_cycle); /*mImageUrl = new ArrayList<String>(); mImageUrl2 = new ArrayList<String>();*/ for(int i=0;i < imageUrls.length; i ++){ ADInfo info = new ADInfo(); info.setUrl(imageUrls[i]); info.setContent("top-->" + i); infos.add(info); } mAdView = (ImageCycleView) findViewById(R.id.ad_view); mAdView.setImageResources(infos, mAdCycleViewListener); } private ImageCycleView.ImageCycleViewListener mAdCycleViewListener = new ImageCycleView.ImageCycleViewListener() { @Override public void onImageClick(ADInfo info, int position, View imageView) { Toast.makeText(ImageCycleActivity.this, "content->"+info.getContent(), Toast.LENGTH_SHORT).show(); } @Override public void displayImage(String imageURL, ImageView imageView) { ImageLoader.getInstance().displayImage(imageURL, imageView);// 使用ImageLoader对图片进行加装! } }; @Override protected void onResume() { super.onResume(); mAdView.startImageCycle(); }; @Override protected void onPause() { super.onPause(); mAdView.pushImageCycle(); } @Override protected void onDestroy() { super.onDestroy(); mAdView.pushImageCycle(); } }