iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android时光轴实现淘宝物流信息浏览效果
  • 941
分享到

Android时光轴实现淘宝物流信息浏览效果

宝物淘宝Android 2022-06-06 07:06:45 941人浏览 独家记忆
摘要

本文实例为大家分享了Android时光轴的制作方法,供大家参考,具体内容如下 1. 效果 2.分析和实现 2.1效果实现:   之前想了一下这种效果,因为只需要用到自己的项目

本文实例为大家分享了Android时光轴的制作方法,供大家参考,具体内容如下

1. 效果

2.分析和实现

2.1效果实现:

  之前想了一下这种效果,因为只需要用到自己的项目中所以采用图片直接布局的形式去实现效果,虽然效果实现了,但是后来发现了出了很多问题:第一Android的分辨率太多了直接设置xxxdp难免有部分机型出现不适配的情况,第二我们与右边这部分需要对齐的问题这个就比较头大。   

所以目前的实现效果方式是这样子的:   

1.自定义TimerLineMarker,根据自定义属性获取点和线的背景资源或是颜色以及宽度等等,在onMeasure中计算布局的宽度和高度;
2.在Item布居中我们给需要对齐那个View设置一个id为need_align_view,我们在onSizeChanged中去找到并计算对齐View距头部的高度;
3.当我们得到对齐View的高度后,我们计算上面Line,中间Marker以及下面Line需要绘制的矩形区域,调用invalidate()然后在onDraw方法中分别绘制这三个部分;
4.很显然我们需要显示的方式是有些不同的,比如第一个没有上面的线其中心标记颜色也不一样,最后一个没有下面的线,所以我们需要提供两个方法:setStyle()设置显示风格;setMarker(int resouceId)设置中间标记的资源   

2.2分步实现: 

1.自定义TimerLineMarker,根据自定义属性获取点和线的背景资源或是颜色以及宽度等等,在onMeasure中计算布局的宽度和高度


public class TimerLineMarker extends View {
 // 3个部分的drawable
 private Drawable mBeginLine, mEndLine, mMarker;
 // 显示大小
 private int mMarkerSize = 26, mLineSize = 4;
 // 距离头部的微调
 private int mMarkerMarginTop = 0;
 public TimerLineMarker(Context context) {
  this(context, null);
 }
 public TimerLineMarker(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }
 public TimerLineMarker(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  initAttribute(attrs);
 }
 
 private void initAttribute(AttributeSet attrs) {
  final TypedArray typedArray = getContext().obtainStyledAttributes(
    attrs, R.styleable.TimerLineMarker);
  // 获取size
  mMarkerSize = typedArray.getDimensionPixelSize(
    R.styleable.TimerLineMarker_markerSize, mMarkerSize);
  mLineSize = typedArray.getDimensionPixelSize(
    R.styleable.TimerLineMarker_lineSize, mLineSize);
  // 获取drawable
  mBeginLine = typedArray
    .getDrawable(R.styleable.TimerLineMarker_beginLine);
  mEndLine = typedArray.getDrawable(R.styleable.TimerLineMarker_endLine);
  mMarker = typedArray.getDrawable(R.styleable.TimerLineMarker_marker);
  mMarkerMarginTop = typedArray.getDimensionPixelSize(
    R.styleable.TimerLineMarker_markerMarginTop, mMarkerMarginTop);
  typedArray.recycle();
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  // 测量本View的宽高里面子控件的宽度
  int with = mMarkerSize + getPaddingLeft() + getPaddingRight();
  int height = mMarkerSize + getPaddingTop() + getPaddingBottom();
  // 通过系统的一个方法做决策最终决定宽高
  int withSize = resolveSizeAndState(with, widthMeasureSpec, 0);
  int heightSize = resolveSizeAndState(height, heightMeasureSpec, 0);
  // 设置宽高
  setMeasuredDimension(withSize, heightSize);
 }
}

2.在Item布居中我们给需要对齐那个View设置一个id为need_align_view,我们在onSizeChanged中去找到并计算对齐View距头部的高度;


 // 标记距离头部的位置
 private int mMarkerTopDistance;
 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  initAlignViewHeight();
  // 当View显示的时候回调
  // 定位到当前几个draw able的坐标,然后绘制
  initDrawable();
 }
 
 private void initAlignViewHeight() {
  // 获取需要对齐的View
  ViewGroup parent = (ViewGroup) this.getParent();
  mNeedAlignView = findNeedAlignView(parent);
  // 获取需要对齐的View距离顶部的高度
  if (mNeedAlignView != null) {
   mMarkerTopDistance = 0;
   // 与需要对齐View的中心点对齐
   mMarkerTopDistance += calcViewTop(mNeedAlignView)
     + mNeedAlignView.getMeasuredHeight() / 2;
  }
 }
 
 private int calcViewTop(View view) {
  final ViewGroup parentView = (ViewGroup) view.getParent();
  final int childCount = parentView.getChildCount();
  // 先加上paddingTop
  int topDistance = parentView.getPaddingTop();
  for (int i = 0; i < childCount; i++) {
   final View childView = parentView.getChildAt(i);
   final ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) childView
     .getLayoutParams();
   topDistance = addTopMargin(topDistance, params);
   if (childView == view) {
    return topDistance;
   }
   topDistance = addBottomMargin(topDistance, params);
   topDistance += childView.getMeasuredHeight();
  }
  return topDistance;
 }
 
 private int addBottomMargin(int topDistance, ViewGroup.LayoutParams params) {
  if (params instanceof RelativeLayout.LayoutParams) {
   RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) params;
   topDistance += param.bottomMargin;
  }
  if (params instanceof LinearLayout.LayoutParams) {
   LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) params;
   topDistance += param.bottomMargin;
  }
  if (params instanceof FrameLayout.LayoutParams) {
   FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) params;
   topDistance += param.bottomMargin;
  }
  if (params instanceof TableLayout.LayoutParams) {
   TableLayout.LayoutParams param = (TableLayout.LayoutParams) params;
   topDistance += param.bottomMargin;
  }
  return topDistance;
 }
 
 private int addTopMargin(int topDistance, ViewGroup.LayoutParams params) {
  if (params instanceof RelativeLayout.LayoutParams) {
   RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) params;
   topDistance += param.topMargin;
  }
  if (params instanceof LinearLayout.LayoutParams) {
   LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) params;
   topDistance += param.topMargin;
  }
  if (params instanceof FrameLayout.LayoutParams) {
   FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) params;
   topDistance += param.topMargin;
  }
  if (params instanceof TableLayout.LayoutParams) {
   TableLayout.LayoutParams param = (TableLayout.LayoutParams) params;
   topDistance += param.topMargin;
  }
  return topDistance;
 }

3.当我们得到对齐View的高度后,我们计算上面Line,中间Marker以及下面Line需要绘制的矩形区域,调用invalidate()然后在onDraw方法中分别绘制这三个部分;


 
 private void initDrawable() {
  initMarkerBounds();
  initLineBounds();
  postInvalidate();
 }
 
 private void initLineBounds() {
  int height = getHeight();
  Rect bounds = mMarker.getBounds();
  int lineLeft = bounds.centerX() - (mLineSize >> 1);
  if (mBeginLine != null)
   mBeginLine.setBounds(lineLeft, 0, lineLeft + mLineSize, bounds.top);
  if (mEndLine != null)
   mEndLine.setBounds(lineLeft, bounds.bottom, lineLeft + mLineSize,
     height);
 }
 
 private void initMarkerBounds() {
  int pLeft = getPaddingLeft();
  int pRight = getPaddingRight();
  int pBottom = getPaddingBottom();
  int pTop = getPaddingTop();
  int width = getWidth();
  int height = getHeight();
  int cWidth = width - pLeft - pRight;
  int cHeight = height - pTop - pBottom;
  mMarkerSize = Math.min(mMarkerSize, Math.min(cWidth, cHeight));
  mMarkerTopDistance = mMarkerTopDistance - mMarkerSize / 2;
  if (mMarkerMarginTop < 0) {
   mMarkerMarginTop = 0;
  }
  mMarker.setBounds(pLeft, mMarkerTopDistance + mMarkerMarginTop, pLeft
    + mMarkerSize, mMarkerTopDistance + mMarkerMarginTop
    + mMarkerSize);
 }
 @Override
 protected void onDraw(canvas canvas) {
  if (mMarker.getBounds().right <= 0) {
   // 如果bounds被弄丢了
   assignValue();
  }
  if (mMarkerStyle != MarkerStyle.START_STYLE) {
   if (mBeginLine != null)
    mBeginLine.draw(canvas);
  }
  mMarker.draw(canvas);
  if (mMarkerStyle != MarkerStyle.END_STYLE) {
   if (mEndLine != null)
    mEndLine.draw(canvas);
  }
 }
 
 private void assignValue() {
  initAlignViewHeight();
  initMarkerBounds();
  initLineBounds();
 }

4.很显然我们需要显示的方式是有些不同的,比如第一个没有上面的线其中心标记颜色也不一样,最后一个没有下面的线,所以我们需要提供两个方法:setStyle()设置显示风格;setMarker(int resouceId)设置中间标记的资源。


 
 public void setStyle(MarkerStyle markerStyle) {
  this.mMarkerStyle = markerStyle;
  invalidate();
 }
 
 public void setMarker(Drawable marker) {
  this.mMarker = marker;
  postInvalidate();
 }
 
 public void setMarker(int resouceId) {
  this.mMarker = getResources().getDrawable(resouceId);
  postInvalidate();
 }
 
 public enum MarkerStyle {
  // 开始第一个
  START_STYLE,
  // 中间位置
  CENTER_STYLE,
  // 最后一个
  END_STYLE
 }

以后希望自己有点空,就把自己做的一些东西写下来. 一方面锻炼一下自己的写文档的能力,另一方面分享代码的同时也希望能与大家交流一下技术,共同学习,共同进步。因为开发过程中遇到一些问题我总会先在网上找一些例子参考一下,类似的代码,可能有些达不到效果或是用不上,没办法也只能自己造轮子。

源码下载地址:Http://xiazai.jb51.net/201611/yuanma/AndroidTimeLine(jb51.net).rar

您可能感兴趣的文章:Android ListView物流获取追踪功能实现Android仿淘宝物流信息TimeLineView


--结束END--

本文标题: Android时光轴实现淘宝物流信息浏览效果

本文链接: https://www.lsjlt.com/news/23481.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • vue实现物流时间轴效果
    本文实例为大家分享了vue实现物流时间轴效果的具体代码,供大家参考,具体内容如下 son组件(物流时间轴组件) <template> <div class...
    99+
    2024-04-02
  • Android自定义recyclerView实现时光轴效果
    时光轴效果在很多app上都有出现,例如淘宝中快递的跟踪,本文将使用recyclerView实现时光轴效果,我们会到自定义控件,首先先看一下效果图: 接下来是步骤分析 1自定义属性 ...
    99+
    2024-04-02
  • Android自定义recyclerView怎么实现时光轴效果
    这篇文章主要为大家分析了Android自定义recyclerView怎么实现时光轴效果的相关知识点,内容详细易懂,操作细节合理,具有一定参考价值。如果感兴趣的话,不妨跟着跟随小编一起来看看,下面跟着小编一起深入学习“Android自定义re...
    99+
    2023-06-28
  • Android开发中使用RecyclerView怎么实现一个时光轴效果
    Android开发中使用RecyclerView怎么实现一个时光轴效果?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。如图:    &nb...
    99+
    2023-05-31
    android recyclerview recycle
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作