最近做项目,碰到如下的需求:VIEwPager分页,如果是6页(包括6页)就用圆点,如果是6页以上就用进度条来切换。前面一种交互方法最常见,用小圆点来表示当前选中的页面,这些小圆点称为导航点,很多App都是这种实现方式。当用户第一次安装或升级应用时,都会利用导航页面告诉用户当前版本的主要亮点,一般情况下当行页面有三部分组成,背景图片,导航文字和滑动的原点,即下面的效果:
这里就不作详细的讲解,大家可以参考我以前写过的博客:
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.setTotalPage(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 * mTotalPage / mSlIDebarVIEw.getMeasureDWIDth(); if (currentPage < 0) { currentPage = 0; } else if (currentPage > mTotalPage) { currentPage = mTotalPage; } // 设置滑块的滑动 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的d出位置 if (mOnSlIDechangelistener != null) { if (currentPage == mTotalPage) { //防止VIEwPager越界 currentPage = mTotalPage - 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 mTotalPage = 0; public voID setTotalPage(int totalPage) { this.mTotalPage = totalPage; } }
相关的单位转化工具,大家可以拷贝到自己的项目中直接使用。
ConvertUtils.java
package com.jackIE.slIDebarvIEwdemo.utils; import androID.content.Context; public class ConvertUtils { public static int dip2px(Context context,float dpValue) { final float scale = context.getResources().getdisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } public static int px2dip(Context context,float pxValue) { final float scale = context.getResources().getdisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } public static int px2sp(Context context,float pxValue) { final float FontScale = context.getResources().getdisplayMetrics().scaledDensity; return (int) (pxValue / FontScale + 0.5f); } public static int sp2px(Context context,float spValue) { 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://schemas.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://schemas.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://schemas.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://schemas.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滑动进度条所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)