ANDROID广告轮播DEMO

编程入门 行业动态 更新时间:2024-10-10 23:22:01

ANDROID<a href=https://www.elefans.com/category/jswz/34/1770787.html style=广告轮播DEMO"/>

ANDROID广告轮播DEMO

ANDROID广告轮播DEMO_仿淘宝广告轮播_滑动图片广告例子
2014-08-27 11:50:10  By: dwtedx
  • Android 
  • dwtedx 
  • 7749

今天在项目中遇到了这样的需求、轮播图片来展示商品、刚刚已经分享了一种方法

但是我觉得这种还是达不到我的要求、所以我后台又换了一种方式、比较好

跟淘宝京东的商品展示非常像、很符合的我胃口、现在分享出来、供大家使用

代码简单、设置简单、自定义圆点和滚动时间、通过 ViewPager 实现的

滚动图片的类


// 图片滚动类
public class MyImgScroll extends ViewPager {Activity mActivity; //上下文List<View> mListViews; //图片组int mScrollTime = 0;Timer timer;int oldIndex = 0;int curIndex = 0;public MyImgScroll(Context context, AttributeSet attrs) {super(context, attrs);}// 开始广告滚动public void start(Activity mainActivity, List<View> imgList,int scrollTime, LinearLayout ovalLayout, int ovalLayoutId,int ovalLayoutItemId, int focusedId, int normalId) {mActivity = mainActivity;mListViews = imgList;mScrollTime = scrollTime;// 设置圆点setOvalLayout(ovalLayout, ovalLayoutId, ovalLayoutItemId, focusedId, normalId);this.setAdapter(new MyPagerAdapter());// 设置适配器if (scrollTime != 0 && imgList.size() > 1) {// 设置滑动动画时间  ,如果用默认动画时间可不用 ,反射技术实现new FixedSpeedScroller(mActivity).setDuration(this, 700);startTimer();// 触摸时停止滚动this.setOnTouchListener(new OnTouchListener() {public boolean onTouch(View v, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_UP) {startTimer();} else {stopTimer();}return false;}});} if (mListViews.size() > 1) {// 设置选中为中间/图片为和第0张一样this.setCurrentItem((Integer.MAX_VALUE / 2)- (Integer.MAX_VALUE / 2) % mListViews.size());}}// 设置圆点private void setOvalLayout(final LinearLayout ovalLayout, int ovalLayoutId,final int ovalLayoutItemId, final int focusedId, final int normalId) {if (ovalLayout != null) {LayoutInflater inflater=LayoutInflater.from(mActivity);for (int i = 0; i < mListViews.size(); i++) {ovalLayout.addView(inflater.inflate(ovalLayoutId, null));}//选中第一个ovalLayout.getChildAt(0).findViewById(ovalLayoutItemId).setBackgroundResource(focusedId);this.setOnPageChangeListener(new OnPageChangeListener() {public void onPageSelected(int i) {curIndex = i % mListViews.size();//取消圆点选中ovalLayout.getChildAt(oldIndex).findViewById(ovalLayoutItemId).setBackgroundResource(normalId);//圆点选中ovalLayout.getChildAt(curIndex).findViewById(ovalLayoutItemId).setBackgroundResource(focusedId);oldIndex = curIndex;}public void onPageScrolled(int arg0, float arg1, int arg2) {}public void onPageScrollStateChanged(int arg0) {}});}}// 取得当明选中下标public int getCurIndex() {return curIndex;}//停止滚动public void stopTimer() {if (timer != null) {timer.cancel();timer = null;}}//开始滚动public void startTimer() {timer = new Timer();timer.schedule(new TimerTask() {public void run() {mActivity.runOnUiThread(new Runnable() {public void run() {MyImgScroll.this.setCurrentItem(MyImgScroll.this.getCurrentItem() + 1);}});}}, mScrollTime, mScrollTime);}// 适配器 //循环设置private class MyPagerAdapter extends PagerAdapter {public void finishUpdate(View arg0) {}public void notifyDataSetChanged() {super.notifyDataSetChanged();}public int getCount() {// 一张图片时不用流动if (mListViews.size() == 1) {return mListViews.size();}return Integer.MAX_VALUE;}public Object instantiateItem(View v, int i) {if (((ViewPager) v).getChildCount() == mListViews.size()) {((ViewPager) v).removeView(mListViews.get(i % mListViews.size()));}((ViewPager) v).addView(mListViews.get(i % mListViews.size()), 0);return mListViews.get(i % mListViews.size());}public boolean isViewFromObject(View arg0, Object arg1) {return arg0 == (arg1);}public void restoreState(Parcelable arg0, ClassLoader arg1) {}public Parcelable saveState() {return null;}public void startUpdate(View arg0) {}public void destroyItem(View arg0, int arg1, Object arg2) {}}
}
以下是对 start 方法中的参数介绍
mainActivity 显示广告的主界面
imgList 图片列表, 不能为null ,最少一张
scrollTime 滚动间隔 ,0为不滚动
ovalLayout 圆点容器,可为空,LinearLayout类型
ovalLayoutId ovalLayout为空时 写0, 圆点layout XMl
ovalLayoutItemId ovalLayout为空时 写0,圆点layout XMl 圆点XMl下View ID
focusedId ovalLayout为空时 写0, 圆点layout XMl 选中时的动画
normalId ovalLayout为空时 写0, 圆点layout XMl 正常时背景

更多推荐

ANDROID广告轮播DEMO

本文发布于:2024-03-10 16:39:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1728487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:广告   ANDROID   DEMO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!