Android 实现时间轴效果,TimeLine(支持进度停在两点之间)

时间轴效果在项目中经常看到

横向的时间轴控件,自定义View实现,继承自View.不适合物流等Item特别多的,适合售后状态展示等场景.先看下效果吧:


EDCE06FD48261B43EB5824E85C342A53.jpg

  1. 支持文字大小设置
  2. 支持当前step前后文字,线条,小圆点的颜色设置
  3. 支持进度停在两点之间(项目需求,不需要请自行忽略);

布局文件就不看了,灰常滴简单就是五个TimeLineView在一个垂直的LinearLayout里面,看一个TimeLineView吧

 <com.bubu.timelinedemo.TimeLineView
        android:id="@+id/timeLineView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="20dp"
        <!--当前step左边的圆圈的颜色-->
        app:underwayCircleColor="@color/colorPrimaryDark"
        <!--当前step左边的文字的颜色-->
        app:underwayStringColor="@color/colorPrimaryDark"
        <!--当前step左边的直线的颜色-->
        app:startedLineColor="@color/colorAccent"   
        <!--当前step右边圆圈的颜色-->
        app:preCircleColor="@color/colorAccent"
        <!--当前step右边直线的颜色-->
        app:preLineColor="@color/colorAccent"
        <!--当前step右边文字的颜色-->
        app:preStringColor="@color/colorAccent"
        
        app:textSize="12sp"
        />

很简单没什么好说的,看下代码吧:

package com.bubu.timelinedemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.support.annotation.FloatRange;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * TimeLineView
 *
 * @author WrBug
 * @since 2017/2/21
 *
 */
public class TimeLineView extends View {
    private Paint mPaint;
    /**
     * 状态文本
     */
    private List<String> mPointTxt;
    /**
     * 步数
     */
    private float mStep = 1;
    /**
     * 圆形x坐标组
     */
    private int[] mXpoints;
    private float[] fXpoints;
    private int mPreLineColor;
    private int mStartedLineColor;

    private int mStartedCircleColor;
    private int mUnderwayCircleColor;
    private int mPreCircleColor;

    private int mStartedStringColor;
    private int mUnderwayStringColor;
    private int mPreStringColor;

    private int mRadius = 10;
    private float mTextSize = 20;
    private float mLineWidth = 5;
    private OnStepChangedListener mOnStepChangedListener;
    private Builder mBuilder;

    public TimeLineView(Context paramContext) {
        this(paramContext, null);
    }

    public TimeLineView(Context paramContext, AttributeSet paramAttributeSet) {
        this(paramContext, paramAttributeSet, 0);
    }

    public TimeLineView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        init();
        initAttrs(paramAttributeSet);
    }

    private void initAttrs(AttributeSet paramAttributeSet) {
        if (paramAttributeSet != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(paramAttributeSet, R.styleable.TimeLineView);
            mStartedLineColor = typedArray.getColor(R.styleable.TimeLineView_startedLineColor, Color.GREEN);
            mPreLineColor = typedArray.getColor(R.styleable.TimeLineView_preLineColor, Color.GRAY);

            mStartedCircleColor = typedArray.getColor(R.styleable.TimeLineView_startedCircleColor, Color.GREEN);
            mUnderwayCircleColor = typedArray.getColor(R.styleable.TimeLineView_underwayCircleColor, Color.GREEN);
            mPreCircleColor = typedArray.getColor(R.styleable.TimeLineView_preCircleColor, Color.GRAY);

            mStartedStringColor = typedArray.getColor(R.styleable.TimeLineView_startedStringColor, Color.GREEN);
            mUnderwayStringColor = typedArray.getColor(R.styleable.TimeLineView_underwayStringColor, Color.GREEN);
            mPreStringColor = typedArray.getColor(R.styleable.TimeLineView_preStringColor, Color.GRAY);
            mTextSize = typedArray.getDimension(R.styleable.TimeLineView_textSize, 20);
            mRadius = (int) typedArray.getDimension(R.styleable.TimeLineView_tlradius, 10);
            mLineWidth = typedArray.getDimension(R.styleable.TimeLineView_lineWidth, 5);
            typedArray.recycle();
        }
    }


    private void init() {
        this.mPaint = new Paint();
        this.mPaint.setAntiAlias(true);
        this.mStartedLineColor = Color.BLUE;
        this.mPreLineColor = Color.GRAY;
        this.mStartedCircleColor = mStartedLineColor;
        this.mUnderwayCircleColor = mStartedCircleColor;
        this.mPreCircleColor = mPreLineColor;
        this.mStartedStringColor = mStartedLineColor;
        this.mUnderwayStringColor = mStartedStringColor;
        this.mPreStringColor = mPreLineColor;
        this.mPointTxt = new ArrayList();
        this.mPointTxt.add("step 1");
        this.mPointTxt.add("step 2");
        this.mPointTxt.add("step 3");
        this.mBuilder = new Builder();
    }

    public Builder builder() {
        return mBuilder;
    }

    public void setPointStrings(@NonNull List<String> pointStringList, @FloatRange(from = 1.0) float step) {
        if (pointStringList == null || pointStringList.isEmpty()) {
            mPointTxt.clear();
            mStep = 0;
        } else {
            mPointTxt = new ArrayList(pointStringList);
            mStep = Math.min(step, mPointTxt.size());
        }
        invalidate();
    }

    public void setPointStrings(@NonNull String[] pointStringList, @FloatRange(from = 1.0) float step) {
        if (pointStringList == null) {
            mPointTxt.clear();
            mStep = 0;
            invalidate();
        } else {
            setPointStrings(new ArrayList<>(Arrays.asList(pointStringList)), step);
        }
    }

    public void setStep(@IntRange(from = 1) int step) {
        this.mStep = Math.min(step, this.mPointTxt.size());
        invalidate();
    }

    public boolean nextStep() {
        if (mStep + 1 > mPointTxt.size()) {
            return false;
        } else {
            mStep++;
            invalidate();
            return true;
        }
    }

    public float getStep() {
        return mStep;
    }

    public void setOnStepChangedListener(OnStepChangedListener listener) {
        this.mOnStepChangedListener = listener;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        initCalc();
        drawCircle(canvas, 1, this.mPointTxt.get(0), this.mXpoints[0]);
        if (mOnStepChangedListener != null) {
            mOnStepChangedListener.onchanged(this, (int) (this.mStep-0.5), this.mPointTxt.get((int)(mStep - 1.5)));
        }
        boolean needContinue=false;
        for (int i = 1; i < mPointTxt.size(); i++) {
            if(this.fXpoints[i]!=0 && this.mStep!=1){
                drawLine(canvas, true, this.mXpoints[(i - 1)], this.fXpoints[i]);
                if(i==mPointTxt.size()-1){
                    drawLine(canvas, false, (int) this.fXpoints[i]-mRadius*2, this.mXpoints[i]);
                }
                needContinue=true;
            }else{
                if(needContinue){
                    needContinue=false;
                    drawLine(canvas, this.mStep > i, (int) this.fXpoints[i-1]-mRadius*3, this.mXpoints[i]);
                }else{
                    drawLine(canvas, this.mStep > i, this.mXpoints[(i - 1)], this.mXpoints[i]);
                }

            }
            drawCircle(canvas, i + 1, this.mPointTxt.get(i), this.mXpoints[i]);
        }
    }

    private void initCalc() {
        int len = this.mPointTxt.size();
        this.mXpoints = new int[len];
        this.fXpoints=new float[len];
        if (len > 1) {
            int strlen = (int) (getWordCount(this.mPointTxt.get(0)) * this.mTextSize);
            this.mXpoints[0] = Math.max(strlen, this.mRadius);
            this.fXpoints[0] = Math.max(strlen, this.mRadius);
            strlen = (int) (getWordCount(this.mPointTxt.get(len - 1)) * this.mTextSize);
            this.mXpoints[len - 1] = getWidth() - Math.max(strlen, this.mRadius);
            int dx = (this.mXpoints[len - 1] - this.mXpoints[0]) / (len - 1);
            float offset = this.mStep % 1;
            if(this.mStep-offset==len - 1 && this.mStep!=len - 1){
                this.fXpoints[len - 1] = (int) (mXpoints[0] + dx * (len - 2)+offset*dx);
            }
            for (int i = 1; i < this.mXpoints.length - 1; i++) {
                if(i==(int)(this.mStep-offset) && mStep > i){
                    this.fXpoints[i] = (int) (mXpoints[0] + dx * (i-1)+offset*dx);
                }else{
                    this.fXpoints[i]=0;
                }
                this.mXpoints[i] =  mXpoints[0] + dx * i;
            }
        }
    }

    private float getWordCount(String s) {
        if (TextUtils.isEmpty(s)) {
            return 0;
        }
        s = s.replaceAll("[^\\x00-\\xff]", "**");
        int length = s.length();
        return length / 4.0f;
    }

    private void drawCircle(Canvas canvas, int drawStep, String text, int dx) {
        this.mPaint.setColor(this.mStep == drawStep ? mUnderwayCircleColor : this.mStep > drawStep ? mStartedCircleColor : mPreCircleColor);
        this.mPaint.setStyle(Paint.Style.FILL);
        this.mPaint.setStrokeWidth(2.0F);
        canvas.drawCircle(dx, getHeight() - this.mRadius - 1, this.mRadius, this.mPaint);
        this.mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(dx, getHeight() - this.mRadius - 1, this.mRadius - 5, this.mPaint);
        this.mPaint.setColor(this.mStep == drawStep ? mUnderwayStringColor : this.mStep > drawStep ? mStartedStringColor : mPreStringColor);
        this.mPaint.setTextSize(this.mTextSize);
        canvas.drawText(text, dx - getWordCount(text) * this.mTextSize, getHeight() - this.mRadius * 2 - 20, this.mPaint);

    }

    private void drawLine(Canvas paramCanvas, boolean isStart, int startX, float endX) {
        this.mPaint.setColor(isStart ? mStartedLineColor : mPreLineColor);
        this.mPaint.setStyle(Paint.Style.FILL);
        this.mPaint.setStrokeWidth(mLineWidth);
        paramCanvas.drawLine(this.mRadius * 1.2F + startX, getHeight() - this.mRadius - 1, endX - this.mRadius * 1.2F, getHeight() - this.mRadius - 1, this.mPaint);
    }

    public interface OnStepChangedListener {
        void onchanged(TimeLineView view, int step, String stepStr);
    }

    public class Builder {

        private Builder() {
        }

        /**
         * 状态文本
         *
         * @param pointStringList
         * @param step
         * @return
         */
        public Builder pointStrings(@NonNull List<String> pointStringList, @IntRange(from = 1) int step) {
            if (pointStringList == null || pointStringList.isEmpty()) {
                mPointTxt.clear();
                mStep = 0;
            } else {
                mPointTxt = new ArrayList(pointStringList);
                mStep = Math.min(step, mPointTxt.size());
            }
            return this;
        }

        /**
         * 状态文本
         *
         * @param pointStringList
         * @param step
         * @return
         */
        public Builder pointStrings(@NonNull String[] pointStringList, @IntRange(from = 1) int step) {
            if (pointStringList == null) {
                mPointTxt.clear();
                mStep = 0;
                return this;
            } else {
                return pointStrings(new ArrayList<>(Arrays.asList(pointStringList)), step);
            }
        }

        /**
         * 文本大小
         *
         * @param px
         * @return
         */
        public Builder textSize(float px) {
            mTextSize = px;
            return this;
        }

        /**
         * 未开始状态线条颜色
         *
         * @param preLineColor
         * @return
         */
        public Builder preLineColor(@ColorInt int preLineColor) {
            mPreLineColor = preLineColor;
            return this;
        }

        /**
         * 已进行状态线条颜色
         *
         * @param startedLineColor
         * @return
         */
        public Builder startedLineColor(@ColorInt int startedLineColor) {
            mStartedLineColor = startedLineColor;
            return this;
        }


        /**
         * 未开始状态圆颜色
         *
         * @param preCircleColor
         * @return
         */
        public Builder preCircleColor(@ColorInt int preCircleColor) {
            mPreCircleColor = preCircleColor;
            return this;
        }


        /**
         * 进行中状态圆颜色
         *
         * @param underwayCircleColor
         * @return
         */
        public Builder underwayCircleColor(@ColorInt int underwayCircleColor) {
            mUnderwayCircleColor = underwayCircleColor;
            return this;
        }

        /**
         * 已进行状态圆颜色
         *
         * @param startedCircleColor
         * @return
         */
        public Builder startedCircleColor(@ColorInt int startedCircleColor) {
            mStartedCircleColor = startedCircleColor;
            return this;
        }


        /**
         * 未开始状态文本颜色
         *
         * @param preStringColor
         * @return
         */
        public Builder preStringColor(@ColorInt int preStringColor) {
            mPreStringColor = preStringColor;
            return this;
        }


        /**
         * 进行中状态文本颜色
         *
         * @param underwayStringColor
         * @return
         */
        public Builder underwayStringColor(@ColorInt int underwayStringColor) {
            mUnderwayStringColor = underwayStringColor;
            return this;
        }

        /**
         * 已进行状态文本颜色
         *
         * @param startedStringColor
         * @return
         */
        public Builder startedStringColor(@ColorInt int startedStringColor) {
            mStartedStringColor = startedStringColor;
            return this;
        }

        /**
         * 圆半径
         *
         * @param px
         * @return
         */
        public Builder radius(int px) {
            mRadius = px;
            return this;
        }

        /**
         * 线条宽度
         *
         * @param lineWidth
         * @return
         */
        public Builder lineWidth(float lineWidth) {
            mLineWidth = lineWidth;
            return this;
        }

        /**
         * 重新绘制
         */
        public void load() {
            invalidate();
        }
    }
}

使用的时候只需要调用timeLineView.setPointStrings(steps,1);方法就可以了,一个参数是steps集合,一个是当前的step,停在两点之间请传float.
完整代码请移步github

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容