成都创新互联网站制作重庆分公司

Android中怎么实现一个折线走势图

今天就跟大家聊聊有关Android中怎么实现一个折线走势图,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

10年积累的网站建设、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有汪清免费网站建设让你可以放心的选择与我们合作。

//获取自定义属性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,mainLinePosition,mPaint);  //low Line  canvas.drawLine(textWidth+DEFAULT_OFFSETTING,mainLinePosition+mChartDistance,w,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,w,mainLinePosition+textHeight/2);  int baseline = (int) ((rt1.bottom + rt1.top - fontMetrics.bottom - fontMetrics.top) / 2);  canvas.drawText(mHighText,0,baseline,mPaint);  canvas.drawText(mLowText,0,baseline+mChartDistance,mPaint);  //3初始化小球圆心  canvas.translate(textWidth+DEFAULT_OFFSETTING,0);  for (int i=0;i=2){   mPaint.setColor(mChartLineColor);   mPaint.setStrokeWidth(chartLineWidth);   for (int i=0;imPointNum){   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 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(); }

使用示例

看完上述内容,你们对Android中怎么实现一个折线走势图有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


文章标题:Android中怎么实现一个折线走势图
文章网址:http://cxhlcq.com/article/gegjoc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部