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();
}
}
android物流时间轴
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 背景 公司新项目中有一个功能要实现时间轴效果,网上也找了很多教程和Demo,但都不是我想要的,大部分用的是Recy...