欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自動(dòng)播放Banner圖片輪播效果

 更新時(shí)間:2020年07月23日 09:24:05   作者:a940659387  
這篇文章主要介紹了Android自動(dòng)播放Banner圖片輪播效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android自動(dòng)播放Banner圖片輪播的具體代碼,供大家參考,具體內(nèi)容如下

先看一下效果圖

支持本地圖片以及網(wǎng)絡(luò)圖片or本地網(wǎng)絡(luò)混合。

使用方式:

<com.jalen.autobanner.BannerView
 android:id="@+id/banner"
 android:layout_width="match_parent"
 android:layout_height="230dip">
</com.jalen.autobanner.BannerView>

核心代碼:

 int length = mList.size();
 View view = LayoutInflater.from(mContext).inflate(R.layout.banner_view,this,true);
 LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_points);
 vp= (ViewPager) view.findViewById(R.id.vp);
 ll.removeAllViews();
 LinearLayout.LayoutParams ll_parmas = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
 ll_parmas.leftMargin=5;
 ll_parmas.rightMargin=5;
 for(int i=0;i<length;i++){
 ImageView img = new ImageView(mContext);
 img.setLayoutParams(ll_parmas);
 if(i==0){
 img.setImageResource(R.mipmap.dot_focus);
 }else{
 img.setImageResource(R.mipmap.dot_blur);
 }
 ll.addView(img);
 mImgs.add(img);

 final ImageView imgforview = new ImageView(mContext);
 imgforview.setOnClickListener(this);
 imgforview.setScaleType(ImageView.ScaleType.FIT_XY);
 if(mList.get(i).getType()==0){//本地圖片
 imgforview.setImageResource(mList.get(i).getDrawableforint());

 }else{//網(wǎng)絡(luò)
 Glide.with(mContext).load(mList.get(i).getDrawableforurl()).diskCacheStrategy(DiskCacheStrategy.ALL).into(imgforview);
// Glide.with(mContext).load(mList.get(i).getDrawableforurl()).listener(new RequestListener<String, GlideDrawable>() {
//  @Override
//  public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
//  Log.d("yu","Faile:"+e.toString());
//  return false;
//  }
//
//  @Override
//  public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
//  imgforview.setImageDrawable(resource);
//  return false;
//  }
// }).into(imgforview);
// Log.d("yu","url: "+mList.get(i).getDrawableforurl());
 }
 mViews.add(imgforview);
 }

 vp.setAdapter(new MyAdapter());
 vp.addOnPageChangeListener(onPageChange);

自動(dòng)輪播利用的是handler的postdelay方法。

private Runnable task = new Runnable() {
 @Override
 public void run() {
 if(isAuto){
 currentItem = currentItem%(mViews.size());
// Log.d("yu","runalbe "+currentItem);
 if(currentItem==0){
  vp.setCurrentItem(currentItem,false);
 }else{
  vp.setCurrentItem(currentItem);
 }
 currentItem++;
 mHandle.postDelayed(task,delaytime);
 }else{
 mHandle.postDelayed(task,delaytime);
 }
 }
 };

利用isAuto判斷是否正在自動(dòng)輪播 如果為false 不自動(dòng)切換item。isAuto賦值操作位于OnPageChangeListener的onPageScrollStateChanged方法中:

 public void onPageScrollStateChanged(int state) {

 switch (state){
 case ViewPager.SCROLL_STATE_IDLE://用戶什么都沒(méi)有操作
  isAuto=true;
  currentItem = vp.getCurrentItem();
//  Log.d("yu","IDLE"+currentItem);
//  if(vp.getCurrentItem()==mViews.size()){
//  vp.setCurrentItem(0,false);
//  }
  break;
 case ViewPager.SCROLL_STATE_DRAGGING://正在滑動(dòng)
  isAuto =false;
  break;
 case ViewPager.SCROLL_STATE_SETTLING://滑動(dòng)結(jié)束
  isAuto=true;
  break;

 }
 }

當(dāng)狀態(tài)為SCROLL_STATE_DRAGGING時(shí) 說(shuō)明用戶正在操作 ,看下源碼中的解釋:

 /**
 * Indicates that the pager is in an idle, settled state. The current page
 * is fully in view and no animation is in progress.
 */
 public static final int SCROLL_STATE_IDLE = 0;

 /**
 * Indicates that the pager is currently being dragged by the user.
 */
 public static final int SCROLL_STATE_DRAGGING = 1;

 /**
 * Indicates that the pager is in the process of settling to a final position.
 */
 public static final int SCROLL_STATE_SETTLING = 2;

大致意思呢就是 0代碼沒(méi)有任何操作。1頁(yè)面正在被用戶拖動(dòng)。2代表成功切換至下一頁(yè)面。

源碼地址:https://github.com/yudehai0204/autoBanner

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論