最近做项目,碰到如下的需求: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, attrs, 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的弹出位置

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

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:orientation="vertical">

android:id="@+id/slide_bar"

android:layout_width="match_parent"

android:layout_height="50dp"

android:layout_marginLeft="20dp"

android:layout_marginRight="20dp"/>

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页"/>

activity_main.xml

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/slide_bar_view"

android:layout_width="match_parent"

android:layout_height="50dp">

android:layout_width="match_parent"

android:layout_height="5dp"

android:layout_centerInParent="true"

android:background="@drawable/shape_slide_bar_bg"/>

android:id="@+id/slide_bar_block"

android:layout_width="20dp"

android:layout_height="14dp"

android:background="#b9b9b9"

android:layout_centerVertical="true" />

popup_window.xml

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="30dp"

android:layout_height="30dp">

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" />

附上相关的资源文件:

shape_slide_bar_bg.xml

android:shape="rectangle">

popup_window_bg.9.png

效果如下:

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

android触摸进度条,Android仿IOS ViewPager滑动进度条相关推荐

  1. Android设置透明状态栏,仿ios状态栏

    为什么80%的码农都做不了架构师?>>>    Android设置透明状态栏,仿ios状态栏 设置透明状态栏后,效果如下: 我的实现思路是: 在根布局上添加一块布局 添加了一块线性布 ...

  2. android仿苹果滑动,Android开发仿IOS的滑动按钮

    释放双眼,带上耳机,听听看~! 项目结构 MainActivity public class MainActivity extends AppCompatActivity { @Override pr ...

  3. Android 文件夹放大缩小仿IOS融合动画效果

    效果如下,打开文件夹时从小到大与最终位置大小吻合,关闭文件夹时缩小到图标原有位置做到无缝融合效果. 过程分析 (一)文件展开后的布局 如上图文件夹打开后布局层次 1.最下面时背景层(背景不移动但有透明 ...

  4. android触摸事件分发,Android 事件分发机制

    Android 事件分发机制一直让人头痛,之前也是面向 GitHub 编程得过且过.今天下定决心了解一下,以便后面自己定制 View 效果.Android 触摸事件有三个基本类型:ACTION_DOW ...

  5. Android 应用开发---ViewPager---5.使用Fragment实现ViewPager滑动

    ViewPager 详解(五)-----使用Fragment实现ViewPager滑动 在第一个页面加一个Btn                                          第一 ...

  6. android 触摸事件 控制,Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    其他相关博文: Android中的事件类型分为按键事件和屏幕触摸事件.TouchEvent是屏幕触摸事件的基础事件,要深入了解屏幕触摸事件的处理机制,就必须掌握TouchEvent在整个触摸事件中的转 ...

  7. android 触摸 卡顿,Android CoordinatorLayout(五) 严重的卡顿BUG

    这章来讲一个重大的问题,解决卡顿,我不敢保证我的方法是最优而且对所以都管用,但是至少会比之前的滑动顺畅. 如果你用我Android CoordinatorLayout(三)中写的demo,你会发现一个 ...

  8. android 触摸 唤醒屏幕,Android中屏幕保持唤醒

    1.锁的类型 PowerManager中各种锁的类型对CPU .屏幕.键盘的影响: PARTIAL_WAKE_LOCK : 保持CPU 运转,屏幕和键盘灯有可能是关闭的. SCREEN_DIM_WAK ...

  9. android 触摸时震动,android触屏震动以及震动强度批改

    android触屏震动以及震动强度修改 PhoneWindowManager.java: 在interceptKeyBeforeQueueing函数中 performHapticFeedbackLw( ...

最新文章

  1. pandas读取多个excel文件并拼接(append)起来形成最终的dataframe、与标签文件连接(join)形成最终学习数据集
  2. 背景和文字分离的matlab实现
  3. 中标麒麟桌面版yum安装mysql_中标麒麟Neokylin6桌面版如何本地yum安装软件
  4. c语言两字符串转数字后相加,一个觉得很难的C语言问题。对两个数字字符串相加。 C语言 如何把一个字符串中相连的两个数字转化为一......
  5. 一个人会python能做什么_利用Python来预测一个人有没有女朋友!无所不能!
  6. KDE/QT vs GNOME/GTK
  7. php抽奖概率算法(刮刮卡,大转盘)
  8. 线性代数与电路学、信号与系统的关系、微分方程--学习笔记
  9. TensorFlow-RNN循环神经网络 Example 2:文本情感分析
  10. 怎样设置mysql软件用户_mysql数据库用户的权限如何设置?
  11. 谷歌设置支持webgl
  12. 路由器mac地址克隆
  13. 关于js的match用法
  14. 输入国家名按字典顺序进行排序
  15. jar a java exception has occured_Java Virtual Machine报错:A Java Exception has occured
  16. ellipse函数用法
  17. Xiuno BBS 4.0 修改时间显示
  18. 要只看每日减少的新增数量,别忘了疫情最开始的时候可能也只有几个人患病
  19. C# TreeView基本操作及其节点增,删,改(3级节点)
  20. usb声卡驱动(六):usb声卡中的pcm打开和关闭

热门文章

  1. hdu1596-find the safest road
  2. Linux环境下FTP工具的使用方法
  3. mysql 聚集函数 count 使用详解(转载)
  4. mongodb由于目标计算机积极拒绝无法连接失败
  5. android 照片旋转并保存
  6. C#对Windows服务组的启动与停止
  7. GDAL源码剖析(五)之Python命令行程序
  8. 为U盘装备Ubuntu工作学习两不误
  9. UVA 11235 - Frequent values
  10. error C2065: 'IDD_***' : undeclared identifier