本文实例为大家分享了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,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(); } /** * 初始化获取需要对齐的VIEw高度 */ 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; } /** * 累加底部的margin高度 */ 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; } /** * 累加头部margin高度 */ 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方法中分别绘制这三个部分;
/** * 初始化Draw able */ private voID initDrawable() { initMarkerBounds(); initlineBounds(); postInvalIDate(); } /** * 初始化时光线Bounds */ private voID initlineBounds() { int height = getHeight(); Rect bounds = mMarker.getBounds(); int lineleft = bounds.centerX() - (mlinesize >> 1); if (mBeginline != null) mBeginline.setBounds(lineleft,lineleft + mlinesize,bounds.top); if (mEndline != null) mEndline.setBounds(lineleft,bounds.bottom,height); } /** * 初始化标记Bounds */ 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(); } /** * 设置标记的Draw able */ public voID setMarker(Drawable marker) { this.mMarker = marker; postInvalIDate(); } /** * 设置标记资源 * * @param resouceID * 资源ID */ 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时光轴实现淘宝物流信息浏览效果全部内容,希望文章能够帮你解决Android时光轴实现淘宝物流信息浏览效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)