文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android仿淘宝物流信息TimeLineView

2022-06-06 04:26

关注

淘宝物流信息TimeLine的制作方法:

仿照的TimeLine效果图:

代码实现:


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
    * timelineHeadRadius + timelineRadiusDistance
    + marginTop + 5)), mPaint);
  continue;
  }
  
  canvas.drawCircle(viewWidth,
   (2 * timelineRadius + timelineRadiusDistance) * (j - 1) + 2
    * timelineHeadRadius - timelineRadius + marginTop,
   timelineRadius, mPaint);
  
  canvas.drawRect(
   new Rect(
    viewWidth - timelineWidth / 2,
    (int) (j
     * (2 * timelineRadius + timelineRadiusDistance)
     - timelineRadiusDistance + 2
     * (timelineHeadRadius - timelineRadius) + marginTop),
    viewWidth + timelineWidth / 2,
    (int) (j
     * (2 * timelineRadius + timelineRadiusDistance)
     + 2 * (timelineHeadRadius - timelineRadius) + marginTop)),
   mPaint);
 }
 }
 public float getTimelineHeadRadius() {
 return timelineHeadRadius;
 }
 public void setTimelineHeadRadius(float timelineHeadRadius) {
 this.timelineHeadRadius = timelineHeadRadius;
 invalidate();
 }
 public int getTimelineHeadColor() {
 return timelineHeadColor;
 }
 public void setTimelineHeadColor(int timelineHeadColor) {
 this.timelineHeadColor = timelineHeadColor;
 invalidate();
 }
 public int getTimelineOtherColor() {
 return timelineOtherColor;
 }
 public void setTimelineOtherColor(int timelineOtherColor) {
 this.timelineOtherColor = timelineOtherColor;
 invalidate();
 }
 public int getTimelineCount() {
 return timelineCount;
 }
 public void setTimelineCount(int timelineCount) {
 this.timelineCount = timelineCount;
 invalidate();
 }
 public int getMarginTop() {
 return marginTop;
 }
 public void setMarginTop(int marginTop) {
 this.marginTop = marginTop;
 invalidate();
 }
 public int getTimelineRadius() {
 return timelineRadius;
 }
 public void setTimelineRadius(int timelineRadius) {
 this.timelineRadius = timelineRadius;
 invalidate();
 }
 public int getTimelineRadiusDistance() {
 return timelineRadiusDistance;
 }
 public void setTimelineRadiusDistance(int timelineRadiusDistance) {
 this.timelineRadiusDistance = timelineRadiusDistance;
 invalidate();
 }
 public int getTimelineWidth() {
 return timelineWidth;
 }
 public void setTimelineWidth(int timelineWidth) {
 this.timelineWidth = timelineWidth;
 }
 public float getTimeLineViewHeight() {
 this.timeLineViewHeight = getMarginTop() + getTimelineCount()
  * (2 * getTimelineRadius() + getTimelineRadiusDistance());
 return timeLineViewHeight;
 }
 public void setTimeLineViewHeight(float timeLineViewHeight) {
 this.timeLineViewHeight = timeLineViewHeight;
 invalidate();
 }
 public int getViewWidth() {
 return viewWidth;
 }
 public void setViewWidth(int viewWidth) {
 this.viewWidth = viewWidth;
 invalidate();
 }
 
 public static int convertDIP2PX(Context context, int dip) {
 float scale = context.getResources().getDisplayMetrics().density;
 return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
 }
}
您可能感兴趣的文章:Android仿淘宝物流追踪的实例代码


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯