Android开发实现广告无限循环功能示例
本文实例讲述了Android开发实现广告无限循环功能。分享给大家供大家参考,具体如下:
一、效果图:
二、代码实现:
/** * 新闻首页 * * @Project App_Card * @Package com.android.koomama.fragment.home * @author chenlin * @version 1.0 * @Date 2014年6月22日 * @Note TODO */ public class NewsHomeFragment extends BaseFragment implements OnTouchListener { private ImageHandler mHandler = new ImageHandler(new WeakReference<NewsHomeFragment>(this)); private ViewPager mViewPager; // 自定义轮播图的资源 private int[] mImageResIds = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5 }; // 放轮播图片的ImageView 的list private List<ImageView> mImageList = new ArrayList<ImageView>(); // 放圆点的View的list private List<View> mDotList = new ArrayList<View>(); @Override public View initConvertView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_news_main, container, false); } @Override protected void onVisible(boolean isInit) { if (isInit) { initViews(); } } private void initViews() { // 初始化iewPager的内容 mViewPager = (ViewPager) mConvertView.findViewById(R.id.view_pager); LinearLayout dotLayout = (LinearLayout) mConvertView.findViewById(R.id.dotLayout); dotLayout.removeAllViews(); for (int i = 0; i < mImageResIds.length; i++) { ImageView imageView = new ImageView(getActivity()); imageView.setScaleType(ScaleType.FIT_XY); // view.setTag(mImageResId[i]); imageView.setImageResource(mImageResIds[i]); mImageList.add(imageView); View dotView = new View(getActivity()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.dot_width), getResources().getDimensionPixelSize(R.dimen.dot_width)); params.setMargins(4, 0, 4, 0); dotView.setLayoutParams(params); // if (i == 0) { // dotView.setBackgroundResource(R.drawable.dot_red); // } else { // dotView.setBackgroundResource(R.drawable.dot_gray); // } dotLayout.addView(dotView); mDotList.add(dotView); } mViewPager.setAdapter(new ImageAdapter(mImageList)); mViewPager.setOnPageChangeListener(new PageChangeListener()); mViewPager.setFocusable(true); mViewPager.setCurrentItem(Integer.MAX_VALUE / 2);// 默认在中间,使用户看不到边界 mViewPager.setOnTouchListener(this); // 开始轮播效果 mHandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mHandler.sendEmptyMessage(ImageHandler.MSG_KEEP_SILENT); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mHandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY); break; } return false; } private class PageChangeListener implements OnPageChangeListener { // 配合Adapter的currentItem字段进行设置。 @Override public void onPageSelected(int position) { // 这个是设置左滑切换底部view的边界,必须要设置 App.getInstance().setBorderViewPosition(position); mHandler.sendMessage(Message.obtain(mHandler, ImageHandler.MSG_PAGE_CHANGED, position, 0)); } @Override public void onPageScrolled(int position, float arg1, int arg2) { } // 覆写该方法实现轮播效果的暂停和恢复 @Override public void onPageScrollStateChanged(int state) { switch (state) { case ViewPager.SCROLL_STATE_DRAGGING: mHandler.sendEmptyMessage(ImageHandler.MSG_KEEP_SILENT); break; case ViewPager.SCROLL_STATE_IDLE: mHandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY); break; default: break; } } } private class ImageAdapter extends PagerAdapter { private List<ImageView> viewlist; public ImageAdapter(List<ImageView> viewlist) { this.viewlist = viewlist; } @Override public int getCount() { // 设置成最大,使用户看不到边界 return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // Warning:不要在这里调用removeView } @Override public Object instantiateItem(ViewGroup container, int position) { // 对ViewPager页号求模取出View列表中要显示的项 position %= viewlist.size(); if (position < 0) { position = viewlist.size() + position; } ImageView view = viewlist.get(position); // 如果View已经在之前添加到了一个父组件,则必须先remove,否则会抛出IllegalStateException。 ViewParent vp = view.getParent(); if (vp != null) { ViewGroup parent = (ViewGroup) vp; parent.removeView(view); } container.addView(view); // 此处可添加监听事件 return view; } } private static class ImageHandler extends Handler { /** * 请求更新显示的View。 */ protected static final int MSG_UPDATE_IMAGE = 1; /** * 请求暂停轮播。 */ protected static final int MSG_KEEP_SILENT = 2; /** * 请求恢复轮播。 */ protected static final int MSG_BREAK_SILENT = 3; /** * 记录最新的页号,当用户手动滑动时需要记录新页号,否则会使轮播的页面出错。 * 例如当前如果在第一页,本来准备播放的是第二页,而这时候用户滑动到了末页, * 则应该播放的是第一页,如果继续按照原来的第二页播放,则逻辑上有问题。 */ protected static final int MSG_PAGE_CHANGED = 4; // 轮播间隔时间 protected static final long MSG_DELAY = 3000; // 使用弱引用避免Handler泄露.这里的泛型参数可以不是Activity,也可以是Fragment等 private WeakReference<NewsHomeFragment> weakReference; private int currentItem = 0; // private boolean isOnce = true; protected ImageHandler(WeakReference<NewsHomeFragment> wk) { weakReference = wk; } @Override public void handleMessage(Message msg) { super.handleMessage(msg); final NewsHomeFragment fragment = weakReference.get(); if (fragment == null) { // Activity已经回收,无需再处理UI了 return; } // 第一次不删重复的消息 if (currentItem != 0) { // 检查消息队列并移除未发送的消息,这主要是避免在复杂环境下消息出现重复等问题。 if (fragment.mHandler.hasMessages(MSG_UPDATE_IMAGE)) { fragment.mHandler.removeMessages(MSG_UPDATE_IMAGE); } } switch (msg.what) { case MSG_UPDATE_IMAGE: currentItem++; fragment.mViewPager.setCurrentItem(currentItem); // 准备下次播放 fragment.mHandler.sendEmptyMessageDelayed(MSG_UPDATE_IMAGE, MSG_DELAY); break; case MSG_KEEP_SILENT: // 只要不发送消息就暂停了 break; case MSG_BREAK_SILENT: fragment.mHandler.sendEmptyMessageDelayed(MSG_UPDATE_IMAGE, MSG_DELAY); break; case MSG_PAGE_CHANGED: // 记录当前的页号,避免播放的时候页面显示不正确。 int position = msg.arg1; int lastIndex = 0; int index = position % fragment.mImageList.size(); for (int i = 0; i < fragment.mDotList.size(); i++) { fragment.mDotList.get(i).setBackgroundResource(R.drawable.dot_normal); } if (fragment.mDotList.get(index)!=null) { fragment.mDotList.get(index).setBackgroundResource(R.drawable.dot_red); } lastIndex = index; currentItem = position; break; default: break; } } } }
三、布局
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="@dimen/news_pic_height" > <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="@dimen/news_pic_height" /> <LinearLayout android:id="@+id/dotLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="right|bottom" android:orientation="horizontal" android:background="@color/transparent_white_half" android:padding="8dp" > <View android:id="@+id/v_dot1" android:layout_width="@dimen/dot_width" android:layout_height="@dimen/dot_width" android:background="@drawable/dot_normal" /> <View android:id="@+id/v_dot2" android:layout_width="@dimen/dot_width" android:layout_height="@dimen/dot_width" android:layout_marginLeft="5dp" android:background="@drawable/dot_red" /> </LinearLayout> </RelativeLayout> </LinearLayout>
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
赞 (0)