Android BannerView通用封装详解

  

之前封装过一个,但总觉得不够优雅,就有了这个通用封装,很简洁,不知道够不够优雅,不过原来那个有跟随指示器和丝滑滑动效果,感兴趣可以看一下。

  

<强>封装过程

  

1,自定义属性
  

  

selectPoint:选中指示器图标
  normalPoint:未选中指示器图标
  pointWidth:图标宽度
  switchTime:轮播间隔事件
  地点:指示器位置,下中或下右

        & lt; declare-styleable name=" NewBannerView比;   & lt; attr name==安慰肌?皊electPoint”格式比;   & lt; attr name==安慰肌?皀ormalPoint”格式比;   & lt; attr=name=" pointWidth "格式"维度"/比;   & lt; attr name==罢?皊witchTime”格式比;   & lt; attr name=拔恢谩北?   & lt;枚举名称="中心" value=" https://www.yisu.com/zixun/0 "/比;   & lt;枚举名称==" https://www.yisu.com/zixun/1 "/罢贰钡募壑当?   & lt;/attr>   & lt;/declare-styleable>   之前      

<强> 2,初始化视图
  初始化:viewpage和指示器组合视图
  

  

<强> 3,绑定数据源
  通过setImageData设置轮播图数据源
  

  

<强> 4,绑定点击事件
  通过OnPageClickListener绑定点击事件
  

  

<强> 5,开启关闭轮播
  启动和停止方法开启和关闭轮播

  

<强>用法

  xml中

        & lt; com.goldou.lovesee.view.NewBannerView   android: id=癅 + id/bannerView”   android: layout_width=" match_parent "   应用:selectPoint=" @drawable/red_point”   应用:normalPoint=" @drawable/gray_point”   应用:switchTime=" 4000 "   应用:位置=罢贰?   android: layout_height=" 200 dp/比;   之前      

活动中         int [] imageList={R.drawable。me_top R.drawable。me_top R.drawable。me_top R.drawable.me_top};   NewBannerView bannerView=view.findViewById (R.id.bannerView);   bannerView.setImageData (imageList);   bannerView.start ();   bannerView。setOnPageClickListener(新NewBannerView.OnPageClickListener () {   @Override   公共空间>   公共类NewBannerView扩展RelativeLayout实现视图。OnClickListener {   私人上下文语境;   私人int selectPoint normalPoint;   私人浮动pointWidth=7;   私人int位置;   私人int中心=0=1;   私人int lastPosition=0;   私人:viewpage: viewpage;   私人int switchTime=5000;   私人int[]图像;   私人OnPageClickListener OnPageClickListener;      私人处理程序处理程序=new处理程序(新Handler.Callback () {   @Override   公共布尔handleMessage(消息味精){   如果(味精。什么==101){   viewPager.setCurrentItem (viewPager.getCurrentItem () + 1);   开始();   }   返回错误;   }   });      公共NewBannerView(上下文语境){   这(上下文,null);   }      公共NewBannerView(上下文语境,AttributeSet attrs) {   这(上下文、attrs 0);   }      公共NewBannerView(上下文语境、AttributeSet attrs int defStyleAttr) {   超级(上下文、attrs defStyleAttr);   这一点。上下文=上下文;   initAttr (attrs);   }      私人空间initAttr (AttributeSet attrs) {   TypedArray数组=上下文。obtainStyledAttributes (attrs R.styleable.NewBannerView);   selectPoint=array.getResourceId (R.styleable。NewBannerView_selectPoint R.mipmap.ic_launcher_round);   normalPoint=array.getResourceId (R.styleable。NewBannerView_normalPoint R.mipmap.ic_launcher_round);   pointWidth=array.getDimension (R.styleable。NewBannerView_pointWidth pointWidth);   位置=array.getInteger (R.styleable。NewBannerView_location吧);   switchTime=array.getInteger (R.styleable。NewBannerView_switchTime switchTime);   array.recycle ();   }      公共空间setImageData(最后一个int[]图像){   这一点。图像=图像;   最后LinearLayout ll_point=new LinearLayout(上下文);   LayoutParams LayoutParams=new LayoutParams (LayoutParams。WRAP_CONTENT LayoutParams.WRAP_CONTENT);   layoutParams。addRule (ALIGN_PARENT_BOTTOM RelativeLayout.TRUE);   layoutParams。页下空白=20;   如果(位置==中心){   layoutParams。addRule (CENTER_HORIZONTAL RelativeLayout.TRUE);   其他}{   layoutParams。addRule (ALIGN_PARENT_RIGHT RelativeLayout.TRUE);   layoutParams。rightMargin=20;   }   LinearLayout。LayoutParams params1=new LinearLayout.LayoutParams (UIUtil.dip2px (pointWidth) UIUtil.dip2px (pointWidth));   LinearLayout。LayoutParams params2=new LinearLayout.LayoutParams (UIUtil.dip2px (pointWidth) UIUtil.dip2px (pointWidth));   params1。leftMargin=0;   params2。leftMargin=UIUtil.dip2px (pointWidth);   for (int i=0;我& lt;images.length;我+ +){   ImageView点=new ImageView(上下文);   如果(i==0) {   point.setBackgroundResource (selectPoint);   point.setLayoutParams (params1);   其他}{   point.setBackgroundResource (normalPoint);   point.setLayoutParams (params2);   }   ll_point.addView(点);   }      :viewpage=new: viewpage(上下文);   :viewpage。setAdapter(新BannerAdapter ());   :viewpage。addOnPageChangeListener(新ViewPager.OnPageChangeListener () {   @Override   公共空间onPageScrolled (int位置、浮动positionOffset int positionOffsetPixels) {      }      @Override   公共空间onPageSelected (int位置){   位置=% images.length位置;   如果(lastPosition==位置){   返回;   }   ll_point.getChildAt(位置).setBackgroundResource (selectPoint);   ll_point.getChildAt (lastPosition) .setBackgroundResource (normalPoint);   lastPosition=位置;   }      @Override   公共空间onPageScrollStateChanged (int状态){      }   });   LayoutParams layoutParams1=new LayoutParams (LayoutParams。MATCH_PARENT LayoutParams.MATCH_PARENT);   addView (: viewpage layoutParams1);   addView (ll_point layoutParams);   }      null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android BannerView通用封装详解