十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
今天就跟大家聊聊有关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
使用示例
看完上述内容,你们对Android中怎么实现一个折线走势图有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。