android自定义控件实现简易时间轴(2)

这篇做了一个简单的时间轴控件。右侧的数据就是一个简单的字符串。问题还是有的,当右侧的文字长度不一样的时候就会有问题了。现在可以修改一下适配右侧的文字。

效果如下:

代码:

private Paint bgPaint, linePaint, borderPaint,textPaint;
private Rect bgRect, textRect;

//基本属性
private int mTextSize;
private int mTextColor;
private String mTextTitle="默认文本内容";

private int lineColr = Color.parseColor("#AAAAAA");
private int borderColor = Color.parseColor("#AAAAAA");
private int bgColor = Color.parseColor("#138DDD");
private int mBorderColor=0xFFDDDDDD;
private int mBorderWidth = 10;
private int mLineColor=Color.parseColor("#ff000000");
private int mLineWidth = 2;
private int mLineHeight;
private int mBgColor;

private  int mWidth =0;
private  int mHeight =300;//整个控件的宽和高

//line绘制
private int lineLocation = -1;//0  上方 1  下方 2  上下两个
private int mRadius = 90;//直径,最终会被宽高限制

//设置line的位置 0  上方 1  下方 2  上下两个

public void setLineLocation(int lineLocation) {
        this.lineLocation = lineLocation;
    }

//设置纯色的整圆形,包括背景
public void setBgAndBorderColor(int color) {
        this.mBgColor = color;
    }

    public void setmHeight(int mHeight) {

        this.mHeight = mHeight;
    }

    public void setmBorderColor(int mBorderColor) {
        this.mBorderColor = mBorderColor;
    }

    public void setmTextTitle(String mTextTitle) {
        this.mTextTitle = mTextTitle;
    }

    public void setmRadius(int mRadius) {
        this.mRadius = mRadius;
    }

    public void setmLineHeight(int mLineHeight) {
        this.mLineHeight = mLineHeight;
    }

    public TimeLineSingleView(Context context) {
        this(context,null);
    }

    public TimeLineSingleView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TimeLineSingleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomCicleView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CustomCicleView_textSize:
                    // 默认设置为16sp,TypeValue也可以把sp转化为px
                    mTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, 14, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomCicleView_textColor:
                    mTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.CustomCicleView_textTitle:
                    mTextTitle = a.getString(attr);
                    break;
                case R.styleable.CustomCicleView_lineWidth:
                    mLineWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomCicleView_lineColor:
                    mLineColor = a.getColor(attr, lineColr);
                    break;

                case R.styleable.CustomCicleView_mRadius:
                    mRadius=a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomCicleView_borderWidth:
                    mBorderWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CustomCicleView_borderColor:
                    mBorderColor = a.getColor(attr, borderColor);
                    break;
                case R.styleable.CustomCicleView_bgColor:
                    mBgColor = a.getColor(attr, bgColor);
                    break;
            }
        }
        a.recycle();
        bgPaint = new Paint();
        borderPaint = new Paint();
        linePaint = new Paint();
        textPaint = new Paint();
        textRect = new Rect();
        textPaint.setTextSize(mTextSize);

    }

    //EXACTLY :在准确的数值和Match_parent的状态是这个值 列表中,wrap_content的状态下必须计算一个合适的值
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int w = 0;
        int h =0;
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if(widthMode==MeasureSpec.EXACTLY){

            w=widthSize;
        }else{
            w=Math.max(mHeight,mRadius+getPaddingRight()+getPaddingLeft());
        }

        if(heightMode==MeasureSpec.EXACTLY){

            h=heightSize;
        }else{
            h=Math.max(mHeight,mRadius+getPaddingTop()+getPaddingBottom());
        }

        setMeasuredDimension(w,h);
        }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        int centreX = getWidth()/ 2; // 获取圆心的x坐标
        int centreY=getHeight()/2;

        //半径比较
        mBorderWidth =(mBorderWidth>=mRadius/10)?(mRadius/10):mBorderWidth;//半径的1/5
        int radius = mRadius/2 - mBorderWidth / 2;// 半径

        if(mLineHeight<=0){

            mLineHeight=Math.abs(getHeight()/2-radius);//这个地方要判断设置正负
        }
        //绘制圆

        bgPaint.setAntiAlias(true); // 消除锯齿
        bgPaint.setColor(mBgColor);
        bgPaint.setStyle(Paint.Style.FILL); // 设置实心
        canvas.drawCircle(centreX, centreY, radius, bgPaint);
        //绘制圆环
        borderPaint.setStrokeWidth(mBorderWidth); // 设置圆环的宽度
        borderPaint.setAntiAlias(true); // 消除锯齿
        if (mBorderColor != 0) {
            borderPaint.setColor(mBorderColor);
        } else {
            borderPaint.setColor(borderColor);
        }
        borderPaint.setStyle(Paint.Style.STROKE); // 设置实心
        canvas.drawCircle(centreX,centreY, radius - mBorderWidth / 2+mLineWidth/2, borderPaint);

        //绘制文本
        textPaint.setTextSize(mTextSize);
        textPaint.setColor(mTextColor);
        textPaint.getTextBounds(mTextTitle, 0, mTextTitle.length(), textRect);

        canvas.drawText(mTextTitle, centreX -textRect.width()/2-mBorderWidth/2, centreY  + textRect.height() / 2, textPaint);

        //绘制线条
        drawLineAll(canvas,centreX,centreY,radius);

    }
    //上下都绘制不用
    //1  上方 0  下方 2  上下两个

    private void drawLineAll(Canvas canvas,  float centreX, float centreY,int radius) {

        if(lineLocation==-1){
            linePaint.setColor(borderColor);
            linePaint.setStrokeWidth(mLineWidth);
            canvas.drawLine(centreX, 0, centreX, centreY-radius, linePaint);//上方的
            canvas.drawLine(centreX, centreY+radius, centreX, getHeight(), linePaint);//下方的
        }else{
            //这个可以绘制不同的line
            linePaint.setColor(lineColr);
            linePaint.setStrokeWidth(mLineWidth);
            if (lineLocation == 0) {
                canvas.drawLine(centreX, centreY+radius, centreX, getHeight(), linePaint);
            } else if (lineLocation == 1) {
                canvas.drawLine(centreX, 0, centreX, centreY -radius , linePaint);

            } else if (lineLocation == 2) {
                canvas.drawLine(centreX, centreY+radius, centreX, getHeight(), linePaint);
                canvas.drawLine(centreX, 0, centreX, centreY -radius , linePaint);
            }
        }

    }

其他代码和之前文章一样就不贴了,但是还有一个问题就是,这个控件是放在一个列表里面的,你在适配器中使用的时候布局要是wrap的状态下要计算一个合适的高度,比如listView 的Item的高度。这里我没有实现,还是在Match和固定高度中,后期更改。

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

(0)

相关推荐

  • Android自定义控件实现时间轴

    本文实例为大家分享了Android自定义控件实现时间轴的具体代码,供大家参考,具体内容如下 由于项目中有需求,就简单的封装一个,先记录一下,有时间上传到github. 1.先增加自定义属性: <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="global_TimelineLayout"> <!--时间轴左

  • Android自定义view仿淘宝快递物流信息时间轴

    学了Android有一段时间了,一直没有时间写博客,趁着周末有点空,就把自己做的一些东西写下来. 一方面锻炼一下自己的写文档的能力,另一方面分享代码的同时也希望能与大家交流一下技术,共同学习,共同进步. 废话不多少说,我们先来看看我们自定义view要实现的效果: 效果图 自定义属性 <resources> <declare-styleable name="TimeLineView"> <attr name="timelineRadius"

  • Android使用自定义View实现横行时间轴效果

    前言 本篇文章会说下如何使用并且要用麻烦的自定义 view 去实现时间轴效果,以及如何分析.实现自定义 view. 需要具备的知识:Paint.Canvas.自定义 view 的绘制流程. 欢迎留言交流. 一.已经有很多 RecycleView 实现时间轴的例子,为何还要费劲的使用自定义 view 去实现时间轴? 首先看下最终想要的效果: 根据上图可以总结出以下几点: 每个阶段要显示时间.阶段名.状态图标.中间有虚线: 文字上下交错显示: 相邻阶段的文字在垂直方向上是可以相交的: 时间轴的个数不

  • Android实现列表时间轴

    本文实例为大家分享了Android列表时间轴展示的具体代码,供大家参考,具体内容如下 实现的效果图如下: 实现的方式是利用recycleview的ItemDecoration这个抽象类,就是我们经常用来画分割线的的这个类, 具体如下 public class DividerItemDecoration extends RecyclerView.ItemDecoration{ // 写右边字的画笔(具体信息) private Paint mPaint; // 写左边日期字的画笔( 时间 + 日期)

  • Android控件之使用ListView实现时间轴效果

     实现思路: 该View是通过ListView实现的,通过实体两个字段内容content和时间time来展示每个ListItem 时间轴是使用上面一条线(20dp)和中间一个圆(15dp)和下面一条线(40dp)组装成的 在ListView中,设置其分割线为空,并且没有点击效果 效果图: 步骤一:使用xml画出一个灰色的圆点(time_cycle.xml) <?xml version="1.0" encoding="utf-8"?> <shape

  • Android实现快递物流时间轴效果

    本文实例为大家分享了Android实现快递物流时间轴效果展示的具体代码,供大家参考,具体内容如下 首先,这篇参考了别人的代码.根据自己的项目需求简单改造了一下,效果图如下 xml:代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:lay

  • Android自定义时间轴的实现过程

    本文讲述Android自定义时间轴的实现过程,供大家参考,具体内容如下 相关视频链接: Android自定义控件系列 http://edu.csdn.net/course/detail/3719/65396 Android视频全系列 http://edu.csdn.net/course/detail/2741/43163 时间轴效果,实际上非常简单,就是listView中一个又一个的条目而已-.大家可以只关注一个条目. 首先展示一个条目的布局效果 <?xml version="1.0&qu

  • 教你3分钟了解Android 简易时间轴的实现方法

    一.有段时间没更了,因为一直在思索,应该写点什么,真的是无比纠结.这一回,就给大家分享一款简便好用的,小编自制的土晾时间轴. 附上XML预览图: 效果图 注:小黄鸭不是效果哈,是为了保护个人隐私P上去的: 1.新建一个自定义控件: public class WorkExcView extends LinearLayout { private TextView dataLeft; private TextView dataRight; private TextView company; priva

  • Android自定义指示器时间轴效果实例代码详解

    指示器时间轴在外卖.购物类的APP里会经常用到,效果大概就像下面这样,看了网上很多文章,大都是自己绘制,太麻烦,其实通过ListView就可以实现. 在Activity关联的布局文件activity_main.xml中放置一个ListView,代码如下.由于这个列表只是用于展示信息,并不需要用户去点击,所以将其clickable属性置为false:为了消除ListView点击产生的波纹效果,我们设置其listSelector属性的值为透明:我们不需要列表项之间的分割线,所以设置其divider属

  • Android自定义View实现垂直时间轴布局

    时间轴,顾名思义就是将发生的事件按照时间顺序罗列起来,给用户带来一种更加直观的体验.京东和淘宝的物流顺序就是一个时间轴,想必大家都不陌生,如下图: 分析 实现这个最常用的一个方法就是用ListView,我这里用继承LinearLayout的方式来实现.首先定义了一些自定义属性: attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable

随机推荐