编程语言
首页 > 编程语言> > Android 仿淘宝物流信息TimeLineView,腾讯面试算法题

Android 仿淘宝物流信息TimeLineView,腾讯面试算法题

作者:互联网

代码实现:

package com.zms.timelineview;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.util.AttributeSet;

import android.view.View;

public class TimeLineView extends View {

private Paint mPaint;

/**

*/

private float timelineHeadRadius;

/**

*/

private int timelineHeadColor;

/**

*/

private int timelineHeadColorEdge;

/**

*/

private int timelineOtherColor;

/**

*/

private int timelineCount;

/**

*/

private int viewWidth;

/**

*/

private int marginTop;

/**

*/

private int timelineRadius;

/**

*/

private int timelineRadiusDistance;

/**

*/

private int timelineWidth;

/**

*/

private float timeLineViewHeight;

public TimeLineView(Context context) {

this(context, null);

}

public TimeLineView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public TimeLineView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(context, attrs, defStyle);

}

/**

*/

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

final TypedArray a = getContext().obtainStyledAttributes(attrs,

R.styleable.TimeLineView, defStyle, 0);

timelineRadiusDistance = (int) a.getDimension(

R.styleable.TimeLineView_timelineRadiusDistance,

convertDIP2PX(context, 20));

timelineHeadRadius = a.getDimension(

R.styleable.TimeLineView_timelineHeadRadius,

convertDIP2PX(context, 6));

timelineRadius = (int) a.getDimension(

R.styleable.TimeLineView_timelineRadius,

convertDIP2PX(context, 5));

timelineHeadColor = a.getColor(

R.styleable.TimeLineView_timelineHeadColor,

Color.parseColor("#25ae5f")); // 中心深色

timelineHeadColorEdge = a.getColor(

R.styleable.TimeLineView_timelineHeadColorEdge,

Color.parseColor("#b9e5cc")); // 边缘浅色

timelineOtherColor = a.getColor(

R.styleable.TimeLineView_timelineOtherColor,

Color.parseColor("#cccccc"));

timelineCount = a.getInteger(R.styleable.TimeLineView_timelineCount, 0);

timelineWidth = (int) a.getDimension(

R.styleable.TimeLineView_timelineWidth,

convertDIP2PX(context, 1));

marginTop = (int) a.getDimension(

R.styleable.TimeLineView_timelineMarginTop,

convertDIP2PX(context, 50));

a.recycle();

mPaint = new Paint();

mPaint.setAntiAlias(true);

}

@Override

protected void onDraw(Canvas canvas) {

// 默认设置时间轴的位置位于view的中间

viewWidth = getMeasuredWidth() / 2;

// 设置第一个节点的颜色

mPaint.setColor(timelineHeadColor);

/**

*/

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

/**

*/

if (j == 1) {

// 画笔设置为空心

canvas.drawCircle(viewWidth, timelineHeadRadius + marginTop,

timelineRadius, mPaint);

mPaint.setStyle(Paint.Style.STROKE);

mPaint.setStrokeWidth(5.0f);

// 画第一个节点外围的圆环

mPaint.setColor(timelineHeadColorEdge);

canvas.drawCircle(viewWidth, timelineHeadRadius + marginTop,

timelineHeadRadius, mPaint);

// 设置画笔颜色,画其他时间节点的颜色

mPaint.setColor(timelineOtherColor);

// 画笔设置为实心

mPaint.setStyle(Paint.Style.FILL);

/**

*/

canvas.drawRect(new Rect(viewWidth - timelineWidth / 2,

(int) (2 * timelineHeadRadius + marginTop) + 5,

viewWidth + timelineWidth / 2, (int) (2

continue;

}

/**

*/

canvas.drawCircle(viewWidth,

(2 * timelineRadius + timelineRadiusDistance) * (j - 1) + 2

timelineRadius, mPaint);

/**

*/

canvas.drawRect(

new Rect(

*/

canvas.drawRect(

new Rect(

标签:timelineRadius,int,TimeLineView,时间轴,腾讯,Android,节点,timelineHeadRadius
来源: https://blog.csdn.net/m0_66145114/article/details/122635946