Android通过Canvas手绘一个折线图

简述

Android绘制图表,有强大的 MPAndroidChart,适合图表功能需求大的应用。至于如下图一个简单的展示作用的折线图还是可以自己封装手绘一个的

折线图.jpg

这个就是要实现的最终效果

定义的Style

  <declare-styleable name="BrokenLineView">
        <attr name="isShowXBorderLine" format="boolean"/>
        <attr name="isShowYBorderLine" format="boolean"/>
        <attr name="XBorderLineColor" format="color"/>
        <attr name="YBorderLineColor" format="color"/>
        <attr name="XBorderLineWidth" format="dimension"/>
        <attr name="YBorderLineWidth" format="dimension"/>
        <attr name="XTextColor" format="color"/>
        <attr name="XTextSize" format="dimension"/>
        <attr name="XTextPadding" format="dimension"/>
        <attr name="YTextColor" format="color"/>
        <attr name="YTextSize" format="dimension"/>
        <attr name="YTextPadding" format="dimension"/>
        <attr name="isShowSolid" format="boolean"/>
        <attr name="solidColor" format="color"/>
        <attr name="circleRadius" format="dimension"/>
        <attr name="circleColor" format="color"/>
        <attr name="isCircleSolid" format="boolean"/>
        <attr name="isShowCircle" format="boolean"/>
        <attr name="BrokenLineWidth" format="dimension"/>
        <attr name="BrokenLineColor" format="color"/>
        <attr name="xUnitValue" format="integer"/>
        <attr name="yUnitValue" format="integer"/>
    </declare-styleable>
name类型描述
isShowXBorderLineboolean是否显示X轴方向的分割线
isShowYBorderLineboolean是否显示Y轴方向的分割线
XBorderLineColorcolorX轴方向分割线显示颜色
YBorderLineColorcolorY轴方向分割线显示颜色
XBorderLineWidthdimensionX轴方向分割线宽
XBorderLineWidthdimensionY轴方向分割线宽
XTextColorcolorX轴方向标识符字体颜色
XTextSizedimensionX轴方向标识符字体大小
XTextPaddingdimensionX轴方向标识符字体与X轴间距
YTextColorcolorY轴方向标识符字体颜色
YTextSizedimensionY轴方向标识符字体大小
YTextPaddingdimensionY轴方向标识符字体与Y轴间距
isShowSolidboolean是否绘制折线和X轴之间的透明背景
solidColorcolor折线和X轴之间的透明背景颜色
circleRadiusdimension折线上绘制圆点的半径
circleColorcolor折线上绘制圆点的颜色
isCircleSolidboolean折线上绘制圆点是实心还是空心
isShowCircleboolean是否绘制折线上的圆点
circleColorcolor折线上绘制圆点的颜色
BrokenLineWidthdimension折线线宽度
BrokenLineColorcolor折线线颜色
xUnitValueintegerX轴一个标识的单位量
yUnitValueintegerY轴一个标识的单位量

对应的方法

  • setXUnitValue(value:Int)

设置X轴一个标识符单位量

  • setYUnitValue(value:Int)

设置Y轴一个标识符单位量

  • setXTextUnits(textUnits:List)

设置X轴方向的标识符,包括原点

  • setYTextUnits(textUnits:List)

设置Y轴方向的标识符,不包括原点

  • setDateList(dataList:List)

设置折线描点

  • startDraw()

开始绘制

使用

  • 依赖
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
 dependencies {
    compile 'com.github.Goach:ChartSimple:1.0.0'
}
  • 布局
<com.goach.widget.BrokenLineView
        android:id="@+id/brokenView"
        android:layout_width="300dp"
        android:layout_height="300dp"/>

同时也可以结合上面Style使用

  • 使用默认配置
brokenView.setDateList(mutableListOf(Point(1,2999), Point(2,1555), Point(3,10000)
                        , Point(4,6000), Point(5,5098), Point(6,1890)))
                .startDraw()
  • 使用自定义的配置
brokenView.setXUnitValue(1)
                .setYUnitValue(2000)
                .setXTextUnits(mutableListOf("0","1","2","3","4","5","6"))
                .setYTextUnits(mutableListOf("2000元","4000元","6000元","8000元","10000元"))
                .setDateList(mutableListOf(Point(1,2999), Point(2,1555), Point(3,10000)
                        , Point(4,6000), Point(5,5098), Point(6,1890)))
                .startDraw()

详细路径ChartSimple-GithubChartSimple-CSDN

package com.example.linchartdemo.view; import java.util.List; import android.R.color; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.util.AttributeSet; import android.util.Log; import android.view.View; public class ZXView extends View { private List<Integer> xlist;// X坐标标签 private List<Integer> ylist;// Y坐标标签 private List<Integer> params;// 参数集合 private Paint paint; private Paint paintLines; private Paint paintArc; private Paint paintText; private int textsize = 20; private float Xoffset = 0;// X轴偏移或叫间隔 private float Yoffset = 0;// Y轴偏移或叫间隔 public float XSpac = 50; public float Xspac = 50; public float YSpac = 50; public float rightXspac = 50; public float rightYspac = 50; public float circleRadius = 10; private float lastX = -1; private float lastY = -1; public ZXView(Context context, List<Integer> xlists, List<Integer> ylists, List<Integer> paramss) { super(context); xlist = xlists; ylist = ylists; params = paramss; initWidget(); } public ZXView(Context context, AttributeSet attrs) { super(context, attrs); initWidget(); } public ZXView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initWidget(); } public void initViewData(List<Integer> xlists, List<Integer> ylists, List<Integer> paramss) { xlist = xlists; ylist = ylists; params = paramss; } @Override protected void onDraw(Canvas canvas) { drawXY(canvas); drawLine(canvas); super.onDraw(canvas); } public void updataUI() { invalidate(); } /** * 画折线 * * @param canvas */ private void drawLine(Canvas canvas) { Xoffset = ((getWidth()) - 20) / (xlist.size()); Yoffset = (getHeight()) / (ylist.size()); Log.i("texts", "X=" + Xoffset + "Y=" + Yoffset); float rulerOffset = 0; if (xlist.size() < 2) throw new IllegalArgumentException("the params argument is <2"); else rulerOffset = (xlist.get(1) - xlist.get(0)); if (params == null && params.size() <= 0) throw new IllegalArgumentException("the params is null or 0"); // Paint p = new Paint(); // p.setAntiAlias(true); // p.setTextSize(25); // p.setColor(Color.WHITE); // canvas.drawText("KW", 10 + Xoffset, 20, p); for (int i = 0; i < params.size(); i++) { int param = params.get(i); float histigramHight = param * (Yoffset / rulerOffset); float currentX = (Xoffset * i + Xspac + 5); float currentY = (getHeight() - YSpac - histigramHight); canvas.drawCircle(currentX, currentY, circleRadius, paintArc); if (lastX != -1) { canvas.drawLine(lastX, lastY, currentX, currentY, paintLines); } lastX = currentX; lastY = currentY; } // 重置lastX跟Y lastX = -1; lastY = -1; } /** * 画坐标轴 * * @param canvas */ private void drawXY(Canvas canvas) { canvas.drawLine(XSpac, 0, XSpac, getHeight() - 5 - XSpac, paint);// x canvas.drawLine(XSpac, getHeight() - XSpac, getWidth(), getHeight() - XSpac, paint);// y canvas.drawLine(getWidth(), 0, getWidth() - 1, getWidth() - YSpac, paint);// 右边Y float yoffset = getHeight() / ylist.size(); float xoffset = (getWidth() - XSpac) / xlist.size(); // 画字 for (int i = 0; i < ylist.size(); i++) { canvas.drawText(ylist.get(i) + "", 0, getHeight() - yoffset * i - YSpac, paintText); } for (int i = 0; i < xlist.size(); i++) { canvas.drawText(xlist.get(i) + "", XSpac + xoffset * i, getHeight() - YSpac + textsize, paintText); } // for (int i = 0; i < ylist.size(); i++) { // for (int j = 0; j < 50; j++) { // canvas.drawLine(XSpac + j * 303, yoffset * i - YSpac, XSpac + j // * 30 + 20, yoffset * i - YSpac, paint); // } // } } /** * 初始化画笔 */ private void initWidget() { paint = new Paint(); paint.setColor(Color.parseColor("#999999")); paint.setTypeface(Typeface.DEFAULT); paint.setAntiAlias(true); paint.setStrokeWidth(2); paintLines = new Paint(); paintLines.setColor(Color.parseColor("#00B4EA")); paintLines.setTypeface(Typeface.DEFAULT); paintLines.setAntiAlias(true); paintLines.setStrokeWidth(3); paintArc = new Paint(); paintArc.setColor(Color.parseColor("#EBEEEF")); paintArc.setTypeface(Typeface.DEFAULT); paintArc.setAntiAlias(true); paintArc.setStrokeWidth(2); paintText = new Paint(); // paintText.setColor(Color.parseColor("#ffffff")); paintText.setTypeface(Typeface.DEFAULT); paintText.setAntiAlias(true); paintText.setStrokeWidth(2); paintText.setTextSize(textsize); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值