Android自定义StepView仿外卖配送进度

本文实例为大家分享了Android自定义StepView配送进度展示的具体代码,供大家参考,具体内容如下

效果图


使用

可在layout文件下设置以下属性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="StepView">
 <attr name="step_size" format="dimension"/><!--step的size,也就是image的大小-->
 <attr name="line_size" format="dimension"/><!--线宽-->
 <attr name="text_size" format="dimension"/><!--文字大小-->
 <attr name="text_line_margin" format="dimension"/><!--文字和线之间的间距-->
 <attr name="normal_line_color" format="color"/><!--一般线的颜色-->
 <attr name="normal_text_color" format="color"/><!--一般文字的颜色-->
 <attr name="target_text_color" format="color"/><!--一般文字的颜色-->
 <attr name="passed_line_color" format="color"/><!--已经过线的颜色-->
 <attr name="step_count" format="integer"/><!--总step数-->
 <attr name="current_step" format="integer"/><!--当前step位置-->
 <attr name="normal_step_iv" format="reference"/><!--一般图片-->
 <attr name="passed_step_iv" format="reference"/><!--已经过的图片-->
 <attr name="target_step_iv" format="reference"/><!--当前step图片-->
 <attr name="step_is_touch" format="boolean"/><!--step是否可点-->
 <attr name="text_up_line" format="boolean"/><!--文字是否在线上-->
 </declare-styleable>
</resources> 
CheckBox cbTouch = findViewById(R.id.cb_touch);
CheckBox cbIsDown = findViewById(R.id.cb_is_down);
final StepView stepView = findViewById(R.id.step_view);
String[] stepTexts = new String[]{"订单已提交", "商家已接单", "配送中", "已送达"};
stepView.setStepTexts(stepTexts);//传入每一进度的文字描述
stepView.setCurrentStep(2);//设置当前进度所在位置
stepView.setOnItemStepTouchListener(new StepView.OnItemStepTouchListener() {
 @Override
 public void onItemStepTouch(int postion) {
 Log.d(TAG, "当前点击位置: "+postion);
 }
});
cbTouch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 stepView.setStepIsTouch(isChecked);
 }
});
cbIsDown.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 stepView.setTextUpLine(!isChecked);
 }
}); 

步骤

1、在构造函数中初始化文字、线、step图片的属性。

public StepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 init(context, attrs);
 } 

 private void init(Context context, AttributeSet attrs) {
 mLinePaint = new Paint();
 mLinePaint.setAntiAlias(true);
 mTextPaint = new Paint();
 mTextPaint.setAntiAlias(true);
 mPreLineLength = 0;
 //默认的step图片
 mNormalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_normal);
 mPassedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_passed);
 mTargetBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_target); 

 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StepView);
 //获取xml文件中的线的颜色值、size
 mNormalLineColor = typedArray.getColor(R.styleable.StepView_normal_line_color, Color.BLUE);
 mPassedLineColor = typedArray.getColor(R.styleable.StepView_passed_line_color, Color.WHITE);
 int lineSize = (int) typedArray.getDimension(R.styleable.StepView_line_size, 2);
 //获取xml文件中的文本的颜色值、size
 mNormalTextColor = typedArray.getColor(R.styleable.StepView_normal_text_color, Color.BLACK);
 mTargetTextColor = typedArray.getColor(R.styleable.StepView_target_text_color, Color.BLACK);
 int textSize = (int) typedArray.getDimension(R.styleable.StepView_text_size, 10);
 //获取xml文件中的step的size,设置给step图片的高度
 int stepSize = (int) typedArray.getDimension(R.styleable.StepView_step_size, 0);
 //获取xml文件中的文本和线之间的间距
 mTextLineMargin = (int) typedArray.getDimension(R.styleable.StepView_text_line_margin, 3);
 //获取xml文件中的step总数
 mStepCount = typedArray.getInt(R.styleable.StepView_step_count, 2); 

 //获取xml文件中的当前step位置
 mCurrentStep = typedArray.getInt(R.styleable.StepView_current_step, 0);
 //获取xml文件中step图片
 BitmapDrawable normalDrawable = (BitmapDrawable) typedArray.getDrawable(R.styleable.StepView_normal_step_iv);
 BitmapDrawable passedDrawable = (BitmapDrawable) typedArray.getDrawable(R.styleable.StepView_passed_step_iv);
 BitmapDrawable targetDrawable = (BitmapDrawable) typedArray.getDrawable(R.styleable.StepView_target_step_iv);
 //获取xml文件中step是否可点击TRUE可以,FALSE不可以,默认为FALSE
 mStepIsTouch = typedArray.getBoolean(R.styleable.StepView_step_is_touch, false);
 //获取xml文件中text是否在线上,TRUE在线上,FALSE不在线上,默认为FALSE
 mTextUpLine = typedArray.getBoolean(R.styleable.StepView_text_up_line, true);
 mTextPaint.setTextSize(textSize);
 mLinePaint.setStrokeWidth(lineSize);
 mNormalBitmap = normalDrawable.getBitmap();//将xml文件中指定的图片赋给对应的bitmap
 mPassedBitmap = passedDrawable.getBitmap();
 mTargetBitmap = targetDrawable.getBitmap();
 mNormalBitmapWH = getBitmapWH(stepSize, mNormalBitmap);
 mPassedBitmapWH = getBitmapWH(stepSize, mPassedBitmap);
 mTargetBitmapWH = getBitmapWH(stepSize, mTargetBitmap);
 if (stepSize != 0) {//如果stepSize不为0,要对其进行压缩处理,使其高度等于stepSize
  mNormalBitmap = zoomImg(mNormalBitmap, mNormalBitmapWH);
  mPassedBitmap = zoomImg(mPassedBitmap, mPassedBitmapWH);
  mTargetBitmap = zoomImg(mTargetBitmap, mPassedBitmapWH);
 }
 mStepRectFs = new RectF[mStepCount];//初始化step所对应的矩阵数组,点击step时会用到,用于确定点击的是哪个step
 typedArray.recycle();
} 

2、在onMeasure中对StepView的宽高进行设置,并根据StepView的宽高计算每条直线的长度。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
 int width = widthSize - getPaddingLeft() - getPaddingRight();//任何模式下with都是父容器给定的with-padding值
 int height = 0;
 if (heightMode == MeasureSpec.EXACTLY) {
 height = heightSize - getPaddingTop() - getPaddingBottom();
 } else {
 height = dp2px(getContext(), 80);
 }
 setMeasuredDimension(width, height);
 mPreLineLength = width / (mStepCount + 1);//计算每条线的长度,由于线比step多一个所以加1
} 

3、开始绘制,先画线,再画step和文字。

@Override
protected void onDraw(Canvas canvas) {
 if (mStepCount != 0) {
 drawLine(canvas);//drawLine和drawStep分两次循环是为了防止部分线覆盖step
 drawStep(canvas);
 }
}

4、画线,前一条线的stopX坐标是下一条线的startX坐标,并根据当前step所在的位置对lineColor进行设置。

private void drawLine(Canvas canvas) {
 float lineStartX = getPaddingLeft();
 float lineStartY = getLineStartY();
 float lineStopX = 0;
 float lineStopY = lineStartY;
 for (int i = 0; i < mStepCount + 1; i++) {
 if (i < mCurrentStep - 1) {
  mLinePaint.setColor(mPassedLineColor);
 } else if (i == mCurrentStep - 1) {
  mLinePaint.setColor(mPassedLineColor);
 } else {
  mLinePaint.setColor(mNormalLineColor);
 }
 lineStopX = lineStartX + mPreLineLength;
 canvas.drawLine(lineStartX, lineStartY, lineStopX, lineStopY, mLinePaint);
 lineStartX = lineStopX;
 }
}

5、画step和文字。

private void drawStep(Canvas canvas) {
 float lineStartX = getPaddingLeft();
 float lineStartY = getLineStartY();
 Bitmap currentBitmap;
 int[] currentBitmapWH;
 float lineStopX;
 float bitmapLeft;
 float bitmapTop;
 for (int i = 0; i < mStepCount; i++) {
 if (i < mCurrentStep - 1) {
  currentBitmap = mPassedBitmap;
  currentBitmapWH = mPassedBitmapWH;
  mTextPaint.setColor(mNormalTextColor);
 } else if (i == mCurrentStep - 1) {
  currentBitmap = mTargetBitmap;
  currentBitmapWH = mTargetBitmapWH;
  mTextPaint.setColor(mTargetTextColor);
 } else {
  currentBitmap = mNormalBitmap;
  currentBitmapWH = mNormalBitmapWH;
  mTextPaint.setColor(mNormalTextColor);
 }
 lineStopX = lineStartX + mPreLineLength;
 bitmapLeft = lineStopX - currentBitmapWH[0] / 2;
 bitmapTop = lineStartY - currentBitmapWH[1] / 2;
 canvas.drawBitmap(currentBitmap, bitmapLeft, bitmapTop, null);
 mStepRectFs[i] = new RectF(bitmapLeft, bitmapTop, bitmapLeft + currentBitmapWH[0], bitmapTop + currentBitmapWH[1]);
 if (mStepTexts != null) {//当没有传入对应的texts时不需要划线
  drawText(canvas, i, bitmapLeft + currentBitmapWH[1] / 2, bitmapTop, currentBitmapWH[1]);//传入step中点坐标
 }
 lineStartX = lineStopX;
 }
} 

private void drawText(Canvas canvas, int i, float x, float y, float bitmapH) {
 String text = mStepTexts[i];
 int[] textWH = getTextWH(text);
 int textWidth = textWH[0];
 int textHeight = textWH[1];
 float bottom = 0;
 if (mTextUpLine) {//画文本时的基准点是left.bottom,使其中心点与step的中心点对其
 bottom = y - mTextLineMargin;
 } else {
 bottom = y + bitmapH + mTextLineMargin + textHeight;
 }
 canvas.drawText(text, x - textWidth / 2, bottom, mTextPaint);
}

6、对触摸事件进行处理。

@Override
public boolean onTouchEvent(MotionEvent event) {
 if (!mStepIsTouch) {//不能点击返回FALSE不处理
 return false;
 }
 switch (event.getAction()) {
 case MotionEvent.ACTION_DOWN:
  float x = event.getX();
  float y = event.getY();
  int touchStep = getTouchStep(new PointF(x, y));//获取被点击的点的位置
  if (touchStep != -1) {
  mCurrentStep = touchStep + 1;
  invalidate();
  }
  break;
 }
 return true;
}

7、step的触摸监听。

private OnItemStepTouchListener mOnItemStepTouchListener; 

public void setOnItemStepTouchListener(OnItemStepTouchListener onItemStepTouchListener) {
 mOnItemStepTouchListener = onItemStepTouchListener;
} 

//每一个step的触摸监听
public interface OnItemStepTouchListener {
 void onItemStepTouch(int postion);
}

8、设置当前进度所在位置,也可在layout文件中通过current_step属性进行设置。

//设置当前step
public void setCurrentStep(int currentStep) {
 mCurrentStep = currentStep;
 invalidate();
}

9、设置step对应的文字,不传入不会显示文字。

//设置step对应的texts
public void setStepTexts(String[] stepTexts) {
 mStepTexts = stepTexts;
 mStepCount = mStepTexts.length;
 mStepRectFs = new RectF[mStepCount];//初始化step所对应的矩阵数组,点击step时会用到,用于确定点击的是哪个step
} 

10、设置step是否可点击,不出入默认为false不可点击,也可在layout文件中通过step_is_touch属性进行设置。

public void setStepIsTouch(boolean stepIsTouch) {
 mStepIsTouch = stepIsTouch;
} 

11、设置文字是否在线上,不传入默认为true在线上,也可在layout文件中通过text_up_line属性进行设置。

public void setTextUpLine(boolean textUpLine) {
 mTextUpLine = textUpLine;
 invalidate();
}

源码地址:StepViewDemo

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

(0)

相关推荐

  • Android仿美团淘宝实现多级下拉列表菜单功能

    我们在常用的电商或者旅游APP中,例如美团,手机淘宝等等,都能够看的到有那种下拉式的二级列表菜单.具体如图所示: 上面两张图就是美团的一个二级列表菜单的一个展示.我相信很多人都想开发一个跟它一样的功能放到自己的APP中.好,接下来我们就开始动手,解决它. 1.结构分析 首先,我们给出这个下拉菜单需要的组建.我们用线框图来分析. 1)如上图所示,最外围的是一个Activity,顶部包含了一个View的容器,这个容器主要是装载ToggleButton来实现诸如美团里面的"美食,全城,理我最近,刷选&

  • 模仿美团点评的Android应用中价格和购买栏悬浮固定的效果

    随着移动互联网的快速发展,它已经和我们的生活息息相关了,在公交地铁里面都能看到很多人的人低头看着自己的手机屏幕,从此"低头族"一词就产生了,作为一名移动行业的开发人员,我自己也是一名"低头族",上下班时间在公交地铁上看看新闻来打发下时间,有时候也会看看那些受欢迎的App的一些界面效果,为什么人家的app那么受欢迎?跟用户体验跟UI设计也有直接的关系,最近在美团和大众点评的App看到如下效果,我感觉用户好,很人性化,所以自己也尝试着实现了下,接下来就讲解下实现思路!

  • Android仿美团外卖菜单界面

    美团外卖菜单界面的Android实现代码,供大家参考,具体内容如下 布局文件 总布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" andr

  • Android仿百度外卖自定义下拉刷新效果

    现如今的APP各式各样,同样也带来了各种需求,一个下拉刷新都能玩出花样了,前两天订饭的时候不经意间看到了"百度外卖"的下拉刷新,今天的主题就是它–自定义下拉刷新动画. 看一下实现效果吧: 动画 我们先来看看Android中的动画吧: Android中的动画分为三种: Tween动画,这一类的动画提供了旋转.平移.缩放等效果. Alpha – 淡入淡出 Scale – 缩放效果 Roate – 旋转效果 Translate – 平移效果 Frame动画(帧动画),这一类动画可以创建一个D

  • Android编程实现仿美团或淘宝的多级分类菜单效果示例【附demo源码下载】

    本文实例讲述了Android编程实现仿美团或淘宝的多级分类菜单效果.分享给大家供大家参考,具体如下: 这里要实现的是诸如美团/淘宝/百度糯米 多级分类菜单效果.当分类数量非常多时可以考虑采用两级分类,而诸如美团这种表现方式是一个不错的选择. 首先上效果图:   主要代码: 1. PopupWindow初始化过程: popupWindow = new PopupWindow(this); View view = LayoutInflater.from(this).inflate(R.layout.

  • Android仿外卖购物车功能

    先看看效果图: 知识点分析 效果图来看不复杂内容并没多少,值得介绍一下的知识点也就下面几个吧 - 列表标题悬停 - 左右列表滑动时联动 - 添加商品时的抛物线动画 - 底部弹出购物车清单 - 数据的同步 另外就是实现效果的时候可能会遇到的几个坑... 布局很简单直接进入代码 1:列表标题悬停 现在做项目列表什么的基本抛弃了ListView改用RecyclerView,上篇博客中的标题悬停也是使用了一个RecyclerView的开源项目sticky-headers-recyclerview,不过写

  • Android模仿美团顶部的滑动菜单实例代码

    前言 本文主要给大家介绍了关于Android模仿美团顶部滑动菜单的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 先来看下效果图: 实现方法 这是通过 ViewPager 和 GridView 相结合做出来的效果,每一个 ViewPager 页面都是一个 GridView,底部的每个滑动指示圆点都是从布局文件中 inflate 出来的 首先需要一个代表每个活动主题的 JavaBean /** * Created by CZY on 2017/6/23. */ publ

  • Android仿美团分类下拉菜单实例代码

    本文实例为大家分享了Android仿美团下拉菜单的实现代码,分类进行选择,供大家参考,具体内容如下 效果图 操作平台 AS2.0 第三方框架:butterknife build.gradle dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile

  • Android仿美团下拉菜单(商品选购)实例代码

    美团电商应用平台大家使用非常频繁,下面小编通过本文给大家介绍电商应用平台中常用的选择类别下拉列表的实现.先给大家展示下效果图: 一.下拉列表的实现 其实实现方法有很多,这时实现的也没有什么技术含量,只是总结下自己在项目中的做法,也提供一个思路. 首先是列表的数据,一般数据都是从后台读过来,这里因为没有后台,所以写死在客户端: private void initMenuData() { menuData = new ArrayList<map<string, string=""

  • Android使用RecyclerView仿美团分类界面

    RecyclerView目前来说对大家可能不陌生了.由于在公司的项目中,我们一直用的listview和gridview.某天产品设计仿照美团的分类界面设计了一个界面,我发现用gridview不能实现这样的效果,所以就想到了RecyclerView,确实是一个很好的控件.和大家分享一下. 效果图 简介 RecyclerView与ListView原理是类似的:都是仅仅维护少量的View并且可以展示大量的数据集.RecyclerView用以下两种方式简化了数据的展示和处理: * 使用LayoutMan

随机推荐