public class TimeLineView extends View {

public TimeLineView(Context context) {

this(context,null);

}

public TimeLineView(Context context, @Nullable AttributeSet attrs) {

this(context, attrs,0);

}

public TimeLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init(context,attrs,defStyleAttr);

}

private Paint mPaint;

private float mHeadRadius;//第一个节点的外半径

private int mHeadColor;//第一个节点颜色

private int mOtherColor;//其它节点颜色

private int mCount;//节点数

private int mViewWidth;//时间轴位置

private float mMarginTop;//时间轴距离顶部距离

private float mOtherRadius;//时间轴节点半径

private float mDistance;//节点长度

private int mHeight;//总长

private float mWidth;//时间轴宽度

private Bitmap mHeadBitmap;//第一个节点显示图片

private Rect mLineRect;

private void init(Context context, AttributeSet attrs, int defStyleAttr) {

TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.TimeLineView,defStyleAttr,0);

mDistance=array.getDimension(R.styleable.TimeLineView_distance,20);

mOtherRadius=array.getDimension(R.styleable.TimeLineView_radius,5);

mOtherColor=array.getColor(R.styleable.TimeLineView_line_color, Color.parseColor("#A8A8A8"));

mCount=array.getInteger(R.styleable.TimeLineView_count,0);

mWidth=array.getDimension(R.styleable.TimeLineView_width,2);

mMarginTop=array.getDimension(R.styleable.TimeLineView_marginTop,100);

array.recycle();

mPaint=new Paint();

mPaint.setAntiAlias(true);

mLineRect=new Rect();

mHeadBitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.location);

}

// @Override

// protected void onSizeChanged(int w, int h, int oldw, int oldh) {

// super.onSizeChanged(w, h, oldw, oldh);

// mViewWidth=getMeasuredWidth()/2;

//

// }

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int specMode = MeasureSpec.getMode(heightMeasureSpec);

int specSize = MeasureSpec.getSize(heightMeasureSpec);

setMeasuredDimension(mHeadBitmap.getWidth(),specSize);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

for(int j=1;j<=mCount;j++){

//头结点

if(j == 1){

drawBitmap(canvas,null,null,mHeadBitmap);

mPaint.setColor(mOtherColor);

mPaint.setStyle(Paint.Style.FILL);

//画头结点下的时间轴

canvas.drawRect(new RectF((mHeadBitmap.getWidth()/2-mWidth/2),(int)(mHeadBitmap.getHeight() +mMarginTop),

(mHeadBitmap.getWidth()/2+mWidth/2),(int)(mHeadBitmap.getHeight()+mMarginTop+mDistance)),mPaint);

continue;

}

float y=(2*mOtherRadius+mDistance)*(j-1)+mHeadBitmap.getHeight()+mMarginTop-mOtherRadius;

canvas.drawCircle(mHeadBitmap.getWidth()/2,y,mOtherRadius,mPaint);

if(j < mCount){

int startX=(int)(mHeadBitmap.getWidth()/2-mWidth/2);

int startY=(int)(y+mOtherRadius);

int endX=(int)(mHeadBitmap.getWidth()/2+mWidth/2);

int endY=(int)(startY+mDistance);

mLineRect.set(startX,startY,endX,endY);

canvas.drawRect(mLineRect,mPaint);

}

}

}

private void drawBitmap(Canvas canvas, Rect src, Rect dst, Bitmap mHeadBitmap) {

if(dst == null){

dst=new Rect(0,(int)mMarginTop,mHeadBitmap.getWidth(),mHeadBitmap.getHeight()+(int)mMarginTop);

}

Paint paint=new Paint();

canvas.drawBitmap(mHeadBitmap,src,dst,paint);

}

public void setHeadRadius(int radius){

mHeadRadius=radius;

invalidate();

}

}

Logo

电商企业物流数字化转型必备!快递鸟 API 接口,72 小时快速完成物流系统集成。全流程实战1V1指导,营造开放的API技术生态圈。

更多推荐