热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android仿摩拜单车共享单车进度条实现StepView效果

这篇文章主要介绍了android仿摩拜单车共享单车进度条实现StepView效果的实例,通过定义五个状态,分别为:为完成、正在进行、已完成、终点完成、终点未完成。具体实现代码,大家参考下

先看效果图:

Step1:定义StepBean

定义五个状态,分别为:为完成、正在进行、已完成、终点完成、终点未完成。

public class StepBean{
  public static final int STEP_UNDO = -1;//未完成
  public static final int STEP_CURRENT = 0;//正在进行
  public static final int STEP_COMPLETED = 1;//已完成
  public static final int STEP_LAST_COMPLETED = 2;//终点完成
  public static final int STEP_LAST_UNCOMPLETED = 3;//终点未完成
  private String name;
  private int state;
  public String getName(){
    return name;
  }
  public void setName(String name){
    this.name = name;
  }
  public int getState(){
    return state;
  }
  public void setState(int state){
    this.state = state;
  }
  public StepBean(){
  }
  public StepBean(String name, int state){
    this.name = name;
    this.state = state;
  }
}

Step2:自定义HorizontalStepsViewIndicator

public class HorizontalStepsViewIndicator extends View {
  private int defaultStepIndicatorNum = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());//定义默认的高度
  private float mCompletedLineHeight;//完成线的高度
  private float mCircleRadius;//圆的半径
  private Drawable mCompleteIcon;//完成的默认图片
  private Drawable mAttentionIcon;//正在进行的默认图片
  private Drawable mDefaultIcon;//默认的背景图
  private Drawable mLastCompleteIcon;//终点未完成图片
  private Drawable mLastUnCompleteIcon;//终点完成图片
  private float mCenterY;//该view的Y轴中间位置
  private float mLeftY;//左上方的Y位置
  private float mRightY;//右下方的位置
  private List mStepBeanList ;//当前有几步流程
  private int mStepNum = 0;
  private float mLinePadding;//两条连线之间的间距
  private List mCircleCenterPointPositionList;//定义所有圆的圆心点位置的集合
  private Paint mUnCompletedPaint;//未完成Paint
  private Paint mCompletedPaint;//完成paint
  private int mUnCompletedLineColor = ContextCompat.getColor(getContext(), R.color.uncompleted_color);//定义默认未完成线的颜色
  private int mCompletedLineColor = ContextCompat.getColor(getContext(), R.color.completed_color);//定义默认完成线的颜色
  private PathEffect mEffects;
  private int mComplectingPosition;//正在进行position
  private Path mPath;
  private OnDrawIndicatorListener mOnDrawListener;
  private int screenWidth;
  /**
   * 设置监听
   * @param onDrawListener
   */
  public void setOnDrawListener(OnDrawIndicatorListener onDrawListener){
    mOnDrawListener= onDrawListener;
  }
  /**
   * get圆的半径 get circle radius
   * @return
   */
  public float getCircleRadius(){
    return mCircleRadius;
  }
  public HorizontalStepsViewIndicator(Context context){
    this(context, null);
  }
  public HorizontalStepsViewIndicator(Context context, AttributeSet attrs){
    this(context, attrs, 0);
  }
  public HorizontalStepsViewIndicator(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
    init();
  }
  private void init(){
    mStepBeanList = new ArrayList<>();
    mPath = new Path();
    mEffects = new DashPathEffect(new float[]{8, 8, 8, 8}, 1);
    mCircleCenterPointPositiOnList= new ArrayList<>();//初始化
    mUnCompletedPaint = new Paint();
    mCompletedPaint = new Paint();
    mUnCompletedPaint.setAntiAlias(true);
    mUnCompletedPaint.setColor(mUnCompletedLineColor);
    mUnCompletedPaint.setStyle(Paint.Style.STROKE);
    mUnCompletedPaint.setStrokeWidth(2);
    mCompletedPaint.setAntiAlias(true);
    mCompletedPaint.setColor(mCompletedLineColor);
    mCompletedPaint.setStyle(Paint.Style.STROKE);
    mCompletedPaint.setStrokeWidth(2);
    mUnCompletedPaint.setPathEffect(mEffects);
    mCompletedPaint.setStyle(Paint.Style.FILL);
    mCompletedLineHeight = 0.03f * defaultStepIndicatorNum;//已经完成线的宽高
    mCircleRadius = 0.28f * defaultStepIndicatorNum;//圆的半径
    mLinePadding = 1.0f * defaultStepIndicatorNum;//线与线之间的间距
    mCompleteIcon = ContextCompat.getDrawable(getContext(), R.drawable.complted);//已经完成的icon
    mAttentiOnIcon= ContextCompat.getDrawable(getContext(), R.drawable.attention);//正在进行的icon
    mDefaultIcon = ContextCompat.getDrawable(getContext(), R.drawable.default_icon);//未完成的icon
    mLastCompleteIcon= ContextCompat.getDrawable(getContext(), R.drawable.last_complted);//终点已完成的icon
    mLastUnCompleteIcon= ContextCompat.getDrawable(getContext(), R.drawable.last_uncomplted);//终点未完成的icon
  }
  @Override
  protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    int width = defaultStepIndicatorNum * 2;
    if(MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(widthMeasureSpec)){
      screenWidth = MeasureSpec.getSize(widthMeasureSpec);
    }
    int height = defaultStepIndicatorNum;
    if(MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(heightMeasureSpec)){
      height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
    }
    width = (int) (mStepNum * mCircleRadius * 2 - (mStepNum - 1) * mLinePadding);
    setMeasuredDimension(width, height);
  }
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh){
    super.onSizeChanged(w, h, oldw, oldh);
    //获取中间的高度,目的是为了让该view绘制的线和圆在该view垂直居中
    mCenterY = 0.5f * getHeight();
    //获取左上方Y的位置,获取该点的意义是为了方便画矩形左上的Y位置
    mLeftY = mCenterY - (mCompletedLineHeight / 2);
    //获取右下方Y的位置,获取该点的意义是为了方便画矩形右下的Y位置
    mRightY = mCenterY + mCompletedLineHeight / 2;
    mCircleCenterPointPositionList.clear();
    for(int i = 0; i  getCircleCenterPointPositionList()
  {
    return mCircleCenterPointPositionList;
  }
  /**
   * 设置流程步数
   * @param stepsBeanList 流程步数
   */
  public void setStepNum(List stepsBeanList) {
    this.mStepBeanList = stepsBeanList;
    mStepNum = mStepBeanList.size();
    if(mStepBeanList!=null&&mStepBeanList.size()>0){
      for(int i = 0;i

Step3:自定义HorizontalStepView

public class HorizontalStepView extends LinearLayout implements HorizontalStepsViewIndicator.OnDrawIndicatorListener{
  private RelativeLayout mTextContainer;
  private HorizontalStepsViewIndicator mStepsViewIndicator;
  private List mStepBeanList;
  private int mComplectingPosition;
  private int mUnComplectedTextColor = ContextCompat.getColor(getContext(), R.color.uncompleted_text_color);//定义默认未完成文字的颜色;
  private int mComplectedTextColor = ContextCompat.getColor(getContext(), R.color.completed_color);//定义默认完成文字的颜色;
  private int mTextSize = 14;//default textSize
  private TextView mTextView;
  public HorizontalStepView(Context context){
    this(context, null);
  }
  public HorizontalStepView(Context context, AttributeSet attrs){
    this(context, attrs, 0);
  }
  public HorizontalStepView(Context context, AttributeSet attrs, int defStyleAttr){
    super(context, attrs, defStyleAttr);
    init();
  }
  private void init(){
    View rootView = LayoutInflater.from(getContext()).inflate(R.layout.widget_horizontal_stepsview, this);
    mStepsViewIndicator = (HorizontalStepsViewIndicator) rootView.findViewById(R.id.steps_indicator);
    mStepsViewIndicator.setOnDrawListener(this);
    mTextCOntainer= (RelativeLayout) rootView.findViewById(R.id.rl_text_container);
  }
  /**
   * 设置显示的文字
   * @param stepsBeanList
   * @return
   */
  public HorizontalStepView setStepViewTexts(List stepsBeanList) {
    mStepBeanList = stepsBeanList;
    mStepsViewIndicator.setStepNum(mStepBeanList);
    return this;
  }
  /**
   * 设置未完成文字的颜色
   * @param unComplectedTextColor
   * @return
   */
  public HorizontalStepView setStepViewUnComplectedTextColor(int unComplectedTextColor) {
    mUnComplectedTextColor = unComplectedTextColor;
    return this;
  }
  /**
   * 设置完成文字的颜色
   * @param complectedTextColor
   * @return
   */
  public HorizontalStepView setStepViewComplectedTextColor(int complectedTextColor) {
    this.mComplectedTextColor = complectedTextColor;
    return this;
  }
  /**
   * 设置StepsViewIndicator未完成线的颜色
   * @param unCompletedLineColor
   * @return
   */
  public HorizontalStepView setStepsViewIndicatorUnCompletedLineColor(int unCompletedLineColor) {
    mStepsViewIndicator.setUnCompletedLineColor(unCompletedLineColor);
    return this;
  }
  /**
   * 设置StepsViewIndicator完成线的颜色
   * @param completedLineColor
   * @return
   */
  public HorizontalStepView setStepsViewIndicatorCompletedLineColor(int completedLineColor) {
    mStepsViewIndicator.setCompletedLineColor(completedLineColor);
    return this;
  }
  /**
   * 设置StepsViewIndicator默认图片
   * @param defaultIcon
   */
  public HorizontalStepView setStepsViewIndicatorDefaultIcon(Drawable defaultIcon) {
    mStepsViewIndicator.setDefaultIcon(defaultIcon);
    return this;
  }
  /**
   * 设置StepsViewIndicator已完成图片
   * @param completeIcon
   */
  public HorizontalStepView setStepsViewIndicatorCompleteIcon(Drawable completeIcon) {
    mStepsViewIndicator.setCompleteIcon(completeIcon);
    return this;
  }
  /**
   * 设置StepsViewIndicator正在进行中的图片
   * @param attentionIcon
   */
  public HorizontalStepView setStepsViewIndicatorAttentionIcon(Drawable attentionIcon) {
    mStepsViewIndicator.setAttentionIcon(attentionIcon);
    return this;
  }
  public HorizontalStepView setStepsViewIndicatorLastCompleteIcon(Drawable lastCompleteIcon) {
    mStepsViewIndicator.setLastCompleteIcon(lastCompleteIcon);
    return this;
  }
  public HorizontalStepView setStepsViewIndicatorLastUnCompleteIcon(Drawable lastUnCompleteIcon) {
    mStepsViewIndicator.setLastUnCompleteIcon(lastUnCompleteIcon);
    return this;
  }
  /**
   * set textSize
   * @param textSize
   * @return
   */
  public HorizontalStepView setTextSize(int textSize) {
    if(textSize > 0) {
      mTextSize = textSize;
    }
    return this;
  }
  @Override
  public void ondrawIndicator() {
    if(mTextContainer != null) {
      mTextContainer.removeAllViews();
      List complectedXPosition = mStepsViewIndicator.getCircleCenterPointPositionList();
      if(mStepBeanList != null && complectedXPosition != null && complectedXPosition.size() > 0) {
        for(int i = 0; i 

Step4:如何使用&#63;

在布局文件xml中:

在Activity中的使用,部分代码截取:

private List stepsBeanList;
         private HorizontalStepView mHorizontalStepView;
         mHorizOntalStepView=(HorizontalStepView)findViewById(R.id.hsv_step_view);
         stepsBeanList = new ArrayList<>();
            StepBean stepBean0=null;
            StepBean stepBean1=null;
            StepBean stepBean2=null;
            StepBean stepBean3=null;
            switch (stepIndex){
              case 1:
                stepBean0 = new StepBean("手机绑定",1);
                stepBean1 = new StepBean("实名认证",0);
                stepBean2 = new StepBean("学时充值",-1);
                stepBean3 = new StepBean("开始用车",3);
                break;
              case 2:
                stepBean0 = new StepBean("手机绑定",1);
                stepBean1 = new StepBean("实名认证",1);
                stepBean2 = new StepBean("学时充值",0);
                stepBean3 = new StepBean("开始用车",3);
                break;
              case 3:
                stepBean0 = new StepBean("手机绑定",1);
                stepBean1 = new StepBean("实名认证",1);
                stepBean2 = new StepBean("学时充值",1);
                stepBean3 = new StepBean("开始用车",2);
                break;
            }
            stepsBeanList.add(stepBean0);
            stepsBeanList.add(stepBean1);
            stepsBeanList.add(stepBean2);
            stepsBeanList.add(stepBean3);
          mHorizontalStepView.setStepViewTexts(stepsBeanList);

以上所述是小编给大家介绍的Android 仿摩拜单车共享单车进度条实现StepView效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


推荐阅读
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 本文探讨了 RESTful API 和传统接口之间的关键差异,解释了为什么 RESTful API 在设计和实现上具有独特的优势。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
author-avatar
再见要死不活的_454
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有