本文实例为大家分享了AndroID折线走势图的具体代码,供大家参考,具体内容如下
先来看看效果图
可以根据球的数量动态的改变自己的球半径,以及线宽
代码实现也是超级简单
//获取自定义属性private voID obtainStyledAttrs(AttributeSet attrs) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs,R.styleable.High_LowChartVIEw); mTextSize = (int)typedArray.getDimension(R.styleable.High_LowChartVIEw_hl_chart_textsize,mTextSize); mTextcolor = typedArray.getcolor(R.styleable.High_LowChartVIEw_hl_chart_textcolor,mTextcolor); if (typedArray.getString(R.styleable.High_LowChartVIEw_hl_hchart_text)!=null){ mHighText = typedArray.getString(R.styleable.High_LowChartVIEw_hl_hchart_text); } if(typedArray.getString(R.styleable.High_LowChartVIEw_hl_hchart_text)!=null){ mLowText = typedArray.getString(R.styleable.High_LowChartVIEw_hl_hchart_text); } mHighPointcolor = typedArray.getcolor(R.styleable.High_LowChartVIEw_hl_chart_high_pointcolor,mHighPointcolor); mLowPointcolor = typedArray.getcolor(R.styleable.High_LowChartVIEw_hl_chart_low_pointcolor,mLowPointcolor); mMainlinecolor = typedArray.getcolor(R.styleable.High_LowChartVIEw_hl_chart_mianlinecolor,mMainlinecolor); mChartlinecolor = typedArray.getcolor(R.styleable.High_LowChartVIEw_hl_chart_chartlinecolor,mChartlinecolor); mChartdistance = (int) typedArray.getDimension(R.styleable.High_LowChartVIEw_hl_chart_distance,mChartdistance); init(); typedArray.recycle(); }//重写onMeasure @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { super.onMeasure(wIDthMeasureSpec,heightmeasureSpec); int wIDth = MeasureSpec.getSize(wIDthMeasureSpec); int height = measureHeight(heightmeasureSpec); setMeasuredDimension(wIDth,height); }//计算vIEw需要的高度 private int measureHeight(int heightmeasureSpec) { int result = 0; int mode = MeasureSpec.getMode(heightmeasureSpec); int size = MeasureSpec.getSize(heightmeasureSpec); if(mode == MeasureSpec.EXACTLY){//如果给了具体值则直接用 result=size; }else { //否则高度等于字高与球直径的最大值 textHeight = (mPaint.descent()-mPaint.ascent()); float halfheight = Math.max(textHeight,mPointMaxHeight) / 2; result = (int) (halfheight+mChartdistance); //如果模式为AT_MOST即:测量高度不能超过父类给定的高度则取测量结果与size的最小值 if(mode==MeasureSpec.AT_MOST){ result = Math.min(result,size); } } return result; } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); if(!initMeasure()) return; canvas.save(); //1先画两条主线 mPaint.setcolor(mMainlinecolor); mPaint.setstrokeWIDth(mainlineHeight); //high line canvas.drawline(textWIDth+DEFAulT_OFFSETTING,mainlineposition,w,mPaint); //low line canvas.drawline(textWIDth+DEFAulT_OFFSETTING,mainlineposition+mChartdistance,mPaint); //2再画文字 mPaint.setcolor(mTextcolor); mPaint.setTextAlign(Paint.Align.left); Paint.FontMetricsInt FontMetrics = mPaint.getFontMetricsInt(); RectF rt1=new RectF(0,mainlineposition-textHeight/2,mainlineposition+textHeight/2); int baseline = (int) ((rt1.bottom + rt1.top - FontMetrics.bottom - FontMetrics.top) / 2); canvas.drawText(mHighText,baseline,mPaint); canvas.drawText(mLowText,baseline+mChartdistance,mPaint); //3初始化小球圆心 canvas.translate(textWIDth+DEFAulT_OFFSETTING,0); for (int i=0;i<mPointNum;i++){ //high point if (mList.get(i).isHighPoint){// mPaint.setcolor(mHighPointcolor);// canvas.drawCircle((pointHeight/2+offsetX)+i*(pointHeight+offsetX),pointHeight/2,mPaint); //存入小球的圆心坐标 mList.get(i).setXY((pointHeight/2+offsetX)+i*(pointHeight+offsetX),mainlineposition); }else { //low point// mPaint.setcolor(mLowPointcolor);// canvas.drawCircle((pointHeight/2+offsetX)+i*(pointHeight+offsetX),mPaint); mList.get(i).setXY((pointHeight/2+offsetX)+i*(pointHeight+offsetX),mainlineposition+mChartdistance); } } //4连接小球 if(mList.size()>=2){ mPaint.setcolor(mChartlinecolor); mPaint.setstrokeWIDth(chartlinewidth); for (int i=0;i<mPointNum-1;i++){ canvas.drawline(mList.get(i).cx,mList.get(i).cy,mList.get(i+1).cx,mList.get(i+1).cy,mPaint); } } //5绘制小球(为了不让线遮盖小球) for (int i=0;i<mPointNum;i++){ //high point if (mList.get(i).isHighPoint){ mPaint.setcolor(mHighPointcolor); //low point }else { mPaint.setcolor(mLowPointcolor); } canvas.drawCircle(mList.get(i).cx,mPaint); } canvas.restore(); } /** * 根据小球数量去动态测量一些属性 * @return */ private boolean initMeasure() { //根据传进来的point数量计算小点的大小,Mainline的线宽,折线的线宽 /* 如果小球数量在1-10个,主线高度为3dip * 如果小球数量在10-20个,主线高度设置为2dip * 如果小球数量超过20个,主线高度设置为1dp */ mPointNum=mList.size();// if(mPointNum==0) return false; if(10>mPointNum){ mainlineHeight=dp2px(3); }else if(10<=mPointNum&&20>mPointNum){ mainlineHeight = dp2px(2); }else { mainlineHeight = dp2px(1); } //主线长度等于总宽度-(文字宽度+30px) textWIDth=Math.max(mPaint.measureText(mHighText),mPaint.measureText(mLowText)); mainlinewidth = (int) (w-(textWIDth+DEFAulT_OFFSETTING)); /*小球直径应该由主线长度与小球数量决定*/ //理想直径 float IDeaDia = mainlinewidth / (mPointNum + (mPointNum + 1)); //小球直径不能大于最大直径 if(IDeaDia>mPointMaxHeight){// Log.i("TTT","IDeaDia>mPointMaxHeight"); pointHeight = mPointMaxHeight; offsetX=(mainlinewidth-mPointNum*pointHeight)/(mPointNum+1); //(极端情况)如果小球直径小于线高,为小球直径+2px }else if(IDeaDia<=mainlineHeight){// Log.i("TTT","IDeaDia<=mPointMaxHeight"); pointHeight = mainlineHeight+2; offsetX=(mainlinewidth-mPointNum*pointHeight)/(mPointNum+1); }else {// Log.i("TTT"," pointHeight=offsetX=IDeaDia"); pointHeight=offsetX=IDeaDia; } //主线位置 mainlineposition = (h-mChartdistance)/2; //折线宽度 chartlinewidth = mainlineHeight/2; return true; }//刷新小球集合 public voID setPointList(List<AiyingPoint> List){ if (List==null&&List.size()==0) return; mList.clear(); if (List.size()>150) { mList.addAll(List.subList(0,150)); } else { mList.addAll(List); } invalIDate(); }
使用示例
<com.androID.vIEw.High_LowChartVIEw androID:ID="@+ID/hl_chart" androID:layout_below="@+ID/tv_state" androID:layout_marginleft="15dp" androID:layout_marginRight="15dp" androID:layout_wIDth="match_parent" androID:layout_height="70dp" app:hl_chart_mianlinecolor="#CDCDCD" app:hl_chart_distance="55dp" app:hl_chart_low_pointcolor="#999999" app:hl_chart_high_pointcolor="#F70919" app:hl_chart_textsize="13sp" app:hl_chart_textcolor="#2f2f2f" app:hl_chart_chartlinecolor="#999999" />
好了这样折线图就绘制完成了,是不是很简单呢?
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的Android实现折线走势图全部内容,希望文章能够帮你解决Android实现折线走势图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)