自定义绘制柱形图

设计思路:

1.画柱状图  2.画竖线  3.画顶部横线  4.画文字

1.画柱状图

画柱状图的方法很简单,就是使用canvas.drawRect(float left, float top, float right, float bottom, Paint paint),其实这里我遇到了一个问题,一开始我想只画一条柱状图,然后需要几个柱状图就在xml文件中声明几个,后来我发现,这样实现起来的动画非常之卡顿(上面gif录出来看上去很卡,其实很流畅)。后来我就换了一种思路,就是声明一个数组,在Activity传入我们需要画的柱状图的总个数和每个柱状图的目标值大小,然后在onDraw方法里分别计算每个柱状图的当前进度,然后分别画出来,这样动画效果就非常流畅了。

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
         * 画柱状图
         */</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> ; i<totalBarNum ; i++){
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (currentBarProgress[i]<(respTarget.get(i)/max)*stopX) {
                currentBarProgress[i]+=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>;
                postInvalidateDelayed(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
            }
            canvas.drawText(respName.get(i),startX,startY+deltaY+i*(deltaY+barWidth)+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>*barWidth/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, mTextPaint);
            canvas.drawRect(startX+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>*barWidth/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>, startY+deltaY+i*(deltaY+barWidth), currentBarProgress[i], startY+deltaY+i*(deltaY+barWidth)+barWidth, mBarPaint);
        }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

2.画竖线和文字

有了上面画柱状图的思路,画竖线就非常容易想了,和画柱状图是一个思路,也是Activity中传入需要画几条竖线,然后在onDraw方法里分别去计算他们的当前进度值,然后再分别去画

文字大小应该随着柱形图宽度来自动适应,所以我进行了一些计算,看上去很复杂,其实就是为了自适应文字的大小

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
         * 画竖线
         */</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> ; i<verticalLineNum ; i++){
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (currentVerticalLineProgress< measuredHeight) {
                currentVerticalLineProgress+=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>;
                postInvalidateDelayed(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
            }
            canvas.drawLine((startX+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>*barWidth/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)+(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)*deltaX, startY, (startX+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>*barWidth/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)+(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)*deltaX, currentVerticalLineProgress, mLinePaint);
            <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//画文字</span>
            canvas.drawText(numPerUnit*(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)+unit, (startX+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>*barWidth/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)+(i+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)*deltaX-barWidth, startY-barWidth/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>, mTextPaint);
        }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>

3.画顶部横线

顶部横线我是从右向左画的,正好与柱状图形成一个对比

<code class="language-java hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
         * 画最上面的横线
         */</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (currentHorizentalLineProgress>startX+<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>*barWidth/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>) {
            currentHorizentalLineProgress-=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>;
            postInvalidateDelayed(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
        }
        canvas.drawLine(stopX, startY, currentHorizentalLineProgress, startY, mLinePaint);
    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

使用方法

1.设置柱状图的最大值

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">mBarGraph<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setMax</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

2.设置柱状图单位

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">mBarGraph<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setUnit</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"亿元"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

3.设置柱状图宽度

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">mBarGraph<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setBarWidth</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

4.设置竖线条数

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">mBarGraph<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setVerticalLineNum</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

5.设置柱状图总个数

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> mBarGraph<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setTotalBarNum</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

6.设置每个柱状图的目标值

<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> private ArrayList<Float> respectTarget;
 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>
 respectTarget = new ArrayList<Float>();
 respectTarget.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">35.</span>0f);
        respectTarget.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20.</span>0f);
        respectTarget.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18.</span>0f);
        respectTarget.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15.</span>0f);
        respectTarget.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.</span>0f);
        respectTarget.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8.</span>0f);
        respectTarget.add(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.</span>0f);
  mBarGraph.setRespectTargetNum(respectTarget);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

7.设置每个柱状图的名字

<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">private ArrayList<String> respName;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span>
respName = new ArrayList<String>();
 respName.add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"滴滴"</span>);
        respName.add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"小米"</span>);
        respName.add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"京东"</span>);
        respName.add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"美团"</span>);
        respName.add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"魅族"</span>);
        respName.add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"酷派"</span>);
        respName.add(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"携程"</span>);
mBarGraph.setRespectName(respName);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

完整代码

大家可以上我的GitHub上下载完整代码

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏iOSer成长记录

OpenGL ES(三) 纹理

16050
来自专栏柠檬先生

Sass 基础(七)

Sass Maps 的函数-map-remove($map,$key),keywords($args)     map-remove($map,$key) ...

22850
来自专栏落影的专栏

Metal图像处理——颜色查找表(Color Lookup Table)

一张1024x1024的普通图片,是由1024 * 1024=1048576个像素点组成,每个像素点包括RGBA共32bit,常见的图像处理是对相邻像素点颜色、...

46760
来自专栏糊一笑

CSS Grid 新手入门

另外,下面一段话摘自A Complete Guide to Grid,对于CSS Grid会有更加清楚地释义

21060
来自专栏黒之染开发日记

【easeljs】矢量形状 Shape类

一个Shape(形状)允许你在显示列表中显示矢量图。它包含一个带有所有绘制矢量图形的方法的Graphics(图形)实例。Graphics实例可以在多个Shape...

13430
来自专栏Coco的专栏

谈谈一些有趣的CSS题目(三)-- 层叠顺序与堆栈上下文知多少

16850
来自专栏重庆的技术分享区

HTML5-Canvas初探(1)

20620
来自专栏编程之旅

iOS开发——Core Graphics绘图

我们在搭建UI界面时,有很多时候,我们会用到iOS自带的绘图功能来完成一些界面的效果,很常用也很方便。今天我们在这里就一起讨论一下iOS的绘图功能。

28320
来自专栏Android机动车

用数学思维实现雷达分析图

前段时间回看里约奥运会的国球比赛,岛国媒体给我龙队一个响亮的称号—— 六边形战士 !

12320
来自专栏Android知识点总结

Android关于Path你所知道的和不知道的一切

42960

扫码关注云+社区

领取腾讯云代金券