Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android仿IOS ViewPager滑动进度条大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

最近做项目,碰到如下的需求:ViewPager分页,如果是6页(包括6页)就用圆点,如果是6页以上就用进度条来切换。前面一种交互方法最常见,用小圆点来表示当前选中的页面,这些小圆点称为导航点,很多App都是这种实现方式。当用户第一次安装或升级应用时,都会利用导航页面告诉用户当前版本的主要亮点,一般情况下当行页面有三部分组成,背景图片,导航文字和滑动的原点,即下面的效果

Android仿IOS ViewPager滑动进度条

这里就不作详细的讲解,大家可以参我以前写过的博客
ViewPager实现图片轮翻效果
今天来实现ViewPager进度条切换,主要逻辑如下:
MainActivity.java

package com.jackie.slidebarviewdemo.activity; 
 
import android.os.bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.TextView; 
 
import com.jackie.slidebarviewdemo.R; 
import com.jackie.slidebarviewdemo.widget.SlideBarView; 
 
public class MainActivity extends AppCompatActivity { 
  private SlideBarView mSlideBarView; 
  private TextView mTextView; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceStatE) { 
    super.onCreate(savedInstanceStatE); 
    setContentView(R.layout.activity_main); 
 
    mSlideBarView = (SlideBarView) findViewById(R.id.slide_bar); 
    mTextView = (TextView) findViewById(R.id.text_view); 
 
    mSlideBarView.set@R_204_10586@lPage(80); 
    mSlideBarView.setOnSlidechangelistener(new SlideBarView.onSlidechangelistener() { 
      @Override 
      public void onSlideChange(int pagE) { 
        mTextView.setText("当前是第" + page + "页"); 
      } 
    }); 
  } 
} 

SlideBarView.java

package com.jackie.slidebarviewdemo.widget; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.PopupWindow; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
 
import com.jackie.slidebarviewdemo.R; 
import com.jackie.slidebarviewdemo.utils.ConvertUtils; 
 
/** 
 * Created by Jackie on 2017/1/17. 
 */ 
 
public class SlideBarView extends RelativeLayout { 
  private LayoutInflater mInflater; 
 
  private RelativeLayout mSlideBarView; 
  private View mSlideBarBlock; 
 
  private PopupWindow mPopupWindow; 
  private TextView mPopupText; 
 
  privatE int mDp40; 
 
  private String mBound = "no"; // no表示没到边界,left为到左边界了,right表示到右边界了 
 
  public interface OnSlidechangelistener { 
    void onSlideChange(int pagE); 
  } 
 
  private OnSlidechangelistener mOnSlidechangelistener; 
  public void setOnSlidechangelistener(OnSlidechangelistener onSlidechangelistener) { 
    this.mOnSlidechangelistener = onSlidechangelistener; 
  } 
 
  public SlideBarView(Context context) { 
    this(context,null); 
  } 
 
  public SlideBarView(Context context,AttributeSet attrs) { 
    this(context,attrs,0); 
  } 
 
  public SlideBarView(Context context,AttributeSet attrs,int defStyleAttr) { 
    super(context,defStyleAttr); 
 
    init(context); 
    initEvent(); 
  } 
 
  private void init(Context context) { 
    mInflater = LayoutInflater.from(context); 
    View slideBar = mInflater.inflate(R.layout.slide_bar,null); 
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); 
    addView(slideBar,params); 
 
    mSlideBarView = (RelativeLayout) slideBar.findViewById(R.id.slide_bar_view); 
    mSlideBarBlock = slideBar.findViewById(R.id.slide_bar_block); 
 
    mDp40 = ConvertUtils.dip2px(context,40); 
  } 
 
  private void initEvent() { 
    mSlideBarView.setOnTouchListener(new OnTouchListener() { 
      int currentX = 0; 
      int startX = 0; 
 
      @Override 
      public Boolean onTouch(View v,MotionEvent event) { 
        switch (event.getAction()) { 
          case MotionEvent.ACTION_DOWN: 
            currentX = (int) event.getX(); 
            startX = (int) event.getX(); 
 
            // 设置滑块的滑动,手指第一次点下去把滑块放到手指上 
            int downLeft = currentX - mSlideBarBlock.getMeasuredWidth() / 2; 
            int downTop = mSlideBarBlock.getTop(); 
            int downRight = downLeft + mSlideBarBlock.getWidth(); 
            int downBottom = mSlideBarBlock.getBottom(); 
 
            //边界检测 
            if (downLeft < 0) { 
              downLeft = 0; 
              downRight = mSlideBarBlock.getMeasuredWidth(); 
            } else if (downRight > mSlideBarView.getMeasuredWidth()) { 
              downLeft = mSlideBarView.getMeasuredWidth() - mSlideBarBlock.getMeasuredWidth(); 
              downRight = mSlideBarView.getMeasuredWidth(); 
            } 
 
            mSlideBarBlock.layout(downLeft,downTop,downRight,downBottom); 
            break; 
          case MotionEvent.ACTION_MOVE: 
            currentX = (int) event.getX(); 
            int currentPage = currentX * m@R_204_10586@lPage / mSlideBarView.getMeasuredWidth(); 
            if (currentPage < 0) { 
              currentPage = 0; 
            } else if (currentPage > m@R_204_10586@lPagE) { 
              currentPage = m@R_204_10586@lPage; 
            } 
 
            // 设置滑块的滑动 
            int moveLeft = currentX - mSlideBarBlock.getMeasuredWidth() / 2; 
            int moveTop = mSlideBarBlock.getTop(); 
            int moveRight = moveLeft + mSlideBarBlock.getMeasuredWidth(); 
            int moveBottom = mSlideBarBlock.getBottom(); 
 
            //边界处理 
            if (moveLeft < 0) { 
              mBound = "left"; 
 
              moveLeft = 0; 
              moveRight = mSlideBarBlock.getMeasuredWidth(); 
            } else if (moveRight >= mSlideBarView.getMeasuredWidth()) { 
              mBound = "right"; 
 
              moveLeft = mSlideBarView.getMeasuredWidth() - mSlideBarBlock.getMeasuredWidth(); 
              moveRight = mSlideBarView.getMeasuredWidth(); 
            } else { 
              mBound = "no"; 
            } 
 
            mSlideBarBlock.layout(moveLeft,moveTop,moveRight,moveBottom); 
            startX = currentX; 
 
            //设置popupWindow的弹出位置 
            if (mOnSlidechangelistener != null) { 
              if (currentPage == m@R_204_10586@lPagE) { 
                //防止ViewPager越界 
                currentPage = m@R_204_10586@lPage - 1; 
              } 
 
              mOnSlidechangelistener.onSlideChange(currentPagE); 
 
              if (mPopupWindow != null) { 
                mPopupText.setText(currentPage + ""); 
 
                //设置PopupWindow的滑动 
                if (!mPopupWindow.isShowing()) { 
                  int[] LOCATIOn = new int[2]; 
                  mSlideBarView.getLOCATIOnInWindow(LOCATIOn); 
                  mPopupWindow.showAsDropDown(mSlideBarView,currentX,LOCATIOn[1] - mDp40); 
                } else { 
                  if ("no".equals(mBound)) { 
                    int[] LOCATIOn = new int[2] ; 
                    mSlideBarView.getLOCATIOnInWindow(LOCATIOn); 
                    mPopupWindow.update(currentX,LOCATIOn[1] - mDp40,mPopupWindow.getWidth(),mPopupWindow.getHeight(),truE); 
                  } 
                } 
              } 
            } 
            break; 
          case MotionEvent.ACTION_UP: 
            currentX = 0; 
            startX = 0; 
            mPopupWindow.dismiss(); 
            break; 
        } 
 
        return true; 
      } 
    }); 
 
    // 初始化PopupWindow 
    View contentView = mInflater.inflate(R.layout.popup_window,null); 
    mPopupText = (TextView) contentView.findViewById(R.id.popup_text); 
    mPopupWindow = new PopupWindow(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
    mPopupWindow.setContentView(contentView); 
    mPopupWindow.setOutsideTouchable(true); 
    mPopupWindow.setBACkgroundDrawable(getresources().getDrawable(R.mipmap.popup_window_bg)); 
    mPopupWindow.setAnimationStyle(0); 
  } 
 
  int m@R_204_10586@lPage = 0; 
  public void set@R_204_10586@lPage(int @R_204_10586@lPagE) { 
    this.m@R_204_10586@lPage = @R_204_10586@lPage; 
  } 
} 

相关的单位转化工具,大家可以拷贝到自己的项目中直接使用。
ConvertUtils.java

package com.jackie.slidebarviewdemo.utils; 
 
import android.content.Context; 
 
public class ConvertUtils { 
  public static int dip2px(Context context,float dp@R_262_7548@ { 
    final float scale = context.getresources().getDisplaymetrics().density; 
    return (int) (dpValue * scale + 0.5f); 
  } 
 
  public static int px2dip(Context context,float px@R_262_7548@ { 
    final float scale = context.getresources().getDisplaymetrics().density; 
    return (int) (pxValue / scale + 0.5f); 
  } 
   
  public static int px2sp(Context context,float px@R_262_7548@ { 
    final float fontScale = context.getresources().getDisplaymetrics().scaledDensity;  
    return (int) (pxValue / fontScale + 0.5f);  
  }  
   
  public static int sp2px(Context context,float sp@R_262_7548@ { 
    final float fontScale = context.getresources().getDisplaymetrics().scaledDensity;  
    return (int) (spValue * fontScale + 0.5f);  
  } 
} 

自定义组合控件,然后实现相关的手势,思路很清晰,代码很详细,这里就直接贴代码了。
activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
  xmlns:android="http://scheR_54_11845@as.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:orientation="vertical"> 
 
    <com.jackie.slidebarviewdemo.widget.SlideBarView 
      android:id="@+id/slide_bar" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp"/> 
 
    <TextView 
      android:id="@+id/text_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="20dp" 
      android:textColor="#000" 
      android:textSize="20dp" 
      android:text="当前是第0页"/> 
  </LinearLayout> 
</RelativeLayout> 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://scheR_54_11845@as.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
  <RelativeLayout 
    android:id="@+id/slide_bar_view" 
    android:layout_width="match_parent" 
    android:layout_height="50dp"> 
 
    <View 
      android:layout_width="match_parent" 
      android:layout_height="5dp" 
      android:layout_centerInParent="true" 
      android:BACkground="@drawable/shape_slide_bar_bg"/> 
 
    <View 
      android:id="@+id/slide_bar_block" 
      android:layout_width="20dp" 
      android:layout_height="14dp" 
      android:BACkground="#b9b9b9" 
      android:layout_centerVertical="true" /> 
  </RelativeLayout> 
</RelativeLayout> 

popup_window.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://scheR_54_11845@as.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
  <RelativeLayout 
    android:layout_width="30dp" 
    android:layout_height="30dp"> 
    <TextView 
      android:id="@+id/popup_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#fff" 
      android:textSize="16dp" 
      android:gravity="center" 
      android:layout_centerInParent="true" /> 
  </RelativeLayout> 
</RelativeLayout> 

附上相关的资源文件
shape_slide_bar_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://scheR_54_11845@as.android.com/apk/res/android" 
  android:shape="rectangle"> 
  <solid android:color="#dcdcdc" /> 
  <corners android:radius="1dp"/> 
</shape> 

popup_window_bg.9.png

Android仿IOS ViewPager滑动进度条


效果如下:

Android仿IOS ViewPager滑动进度条

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的Android仿IOS ViewPager滑动进度条全部内容,希望文章能够帮你解决Android仿IOS ViewPager滑动进度条所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。