IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android纯手工实现几种数据趋势图的自定义View(超简单,有源码) -> 正文阅读

[移动开发]Android纯手工实现几种数据趋势图的自定义View(超简单,有源码)

不需要依赖第三方框架,直接使用Canvas

drawLine,drawPath,drawCircle 
复制代码

等几个简单的API即可搞定

效果图

?

源码:https://github.com/woshiwzy/trendviews

部分代码节选

onDraw?

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        w = getWidth();
        h = getHeight();
        this.coordinate_pading_v = (int) (h * this.coordinate_pading_v_percent);
        canvas.drawColor(Color.WHITE);
        mPaint.setStrokeWidth(paintSolideSmall);
        mPaint.setTextSize(label_text_size);
        int title_width = (int) mPaint.measureText(this.titlle);
        mPaint.setStyle(Style.FILL);
        mPaint.setColor(Color.BLACK);
        canvas.drawText(this.titlle, w / 2 - title_width / 2, 50, mPaint);
        // 画背景网格
        if (0 == (countListSize(mArrayListHolderAllLastYear) + countListSize(mArrayListHolderAllThisYear) +
                countListSize(mArrayListHolderLocalLastYears) + countListSize(mArrayListHolderLocalThisYears))) {
        } else {
            drawGrid(canvas);
        }
    }

drawGrid 绘制主要内容


    public void drawGrid(Canvas canvas) {
        mPaint.setColor(0x33dbdbdb);
        // 画垂直网格
        // coordinate_pading_h
        int cell_width = (w - coordinate_pading_h * 2) / (months - 1);

        this.mPointsAll.clear();
        this.mPointsLocal.clear();
        float pix_value_unit = (h - h * coordinate_pading_v_percent - coordinate_pading_b) / (this.highest - this.lowerest);

        Path pathLocalThis = new Path();//本公司今年
        pathLocalThis.reset();

        Path pathLocalLast = new Path();//本公司去年
        pathLocalLast.reset();


        Path pathAllThis = new Path();//全市场今年
        pathAllThis.reset();

        Path pathAllLast = new Path();//全市场去年
        pathAllLast.reset();


        for (int i = 0, isize = months; i < isize; i++) {
            int startX = coordinate_pading_h + i * cell_width;
            int startY = 0;
            int stopX = coordinate_pading_h + i * cell_width;
            int stopY = h;
            canvas.drawLine(startX, startY, stopX, stopY, mPaint);// 竖直网格
            canvas.drawLine(startX + cell_width / 2, startY, startX + cell_width / 2, stopY, mPaint);// 竖直网格
            if (i == 0) {
                canvas.drawLine(startX - cell_width / 2, startY, startX - cell_width / 2, stopY, mPaint);// 竖直网格
            }

            if (null != mArrayListHolderLocalThisYears && !mArrayListHolderLocalThisYears.isEmpty()) {
                //本公司今年
                if (i < mArrayListHolderLocalThisYears.size()) {
                    Holder holderLocalThisYear = mArrayListHolderLocalThisYears.get(i);
                    if (null == this.mPointsLocal) {
                        this.mPointsLocal = new ArrayList<MyPoint>();
                    }
                    int x = startX;
                    float y = h - pix_value_unit * (holderLocalThisYear.getValue() - this.lowerest) - coordinate_pading_b;
                    MyPoint mypointlocalthisyear = new MyPoint(x, (int) y, holderLocalThisYear);
                    if (i == 0) {
                        pathLocalThis.moveTo(x, y);
                    } else {
                        pathLocalThis.lineTo(x, y);
                    }
                    this.mPointsLocal.add(mypointlocalthisyear);
                } else {
                    this.mPointsLocal = null;
                }
            }
            if (null != mArrayListHolderLocalLastYears && !mArrayListHolderLocalLastYears.isEmpty()) {
                //本公司去年
                if (i < mArrayListHolderLocalLastYears.size()) {
                    Holder holderLocalLastYear = mArrayListHolderLocalLastYears.get(i);
                    if (null == this.mPointsLast) {
                        this.mPointsLast = null;
                    }
                    int x = startX;
                    float y = h - pix_value_unit * (holderLocalLastYear.getValue() - this.lowerest) - coordinate_pading_b;
                    MyPoint mypointlocallastyear = new MyPoint(x, (int) y, holderLocalLastYear);
                    if (i == 0) {
                        pathLocalLast.moveTo(x, y);
                    } else {
                        pathLocalLast.lineTo(x, y);
                    }
                    this.mPointsLast.add(mypointlocallastyear);
                } else {
                    this.mPointsLast = null;
                }
            }

            if (null != mArrayListHolderAllThisYear && !mArrayListHolderAllThisYear.isEmpty()) {
                //全市场今年
                if (i < mArrayListHolderAllThisYear.size()) {
                    Holder holderAllHolderThisYear = mArrayListHolderAllThisYear.get(i);
                    if (null == this.mPointsAll) {
                        this.mPointsAll = new ArrayList<MyPoint>();
                    }
                    int x = startX;
                    float y = h - pix_value_unit * (holderAllHolderThisYear.getValue() - this.lowerest) - coordinate_pading_b;
                    MyPoint mypointallthisyear = new MyPoint(x, (int) y, holderAllHolderThisYear);
                    if (i == 0) {
                        pathAllThis.moveTo(x, y);
                    } else {
                        pathAllThis.lineTo(x, y);
                    }
                    this.mPointsAll.add(mypointallthisyear);
                } else {
                    this.mPointsAll = null;
                }
            }

            if (null != mArrayListHolderAllLastYear && !mArrayListHolderAllLastYear.isEmpty()) {
                //全市场去年
                if (i < mArrayListHolderAllLastYear.size()) {
                    Holder holderAllHolderLastYear = mArrayListHolderAllLastYear.get(i);
                    if (null == this.mPointsAllLast) {
                        this.mPointsAllLast = new ArrayList<MyPoint>();
                    }
                    int x = startX;
                    float y = h - pix_value_unit * (holderAllHolderLastYear.getValue() - this.lowerest) - coordinate_pading_b;
                    MyPoint mypointalllastyear = new MyPoint(x, (int) y, holderAllHolderLastYear);
                    if (i == 0) {
                        pathAllLast.moveTo(x, y);
                    } else {
                        pathAllLast.lineTo(x, y);
                    }
                    this.mPointsAllLast.add(mypointalllastyear);
                } else {
                    this.mPointsAllLast = null;
                }
            }
        }

        //组装path结束
        // 画水平网格
        int cell_height = h / (months * 2);
        for (int i = 0, isize = months * 2; i <= isize; i++) {
            canvas.drawLine(0, cell_height * i, w, cell_height * i, mPaint);
        }
        mPaint.setStyle(Style.STROKE);
        mPaint.setStrokeWidth(radiusBig);

        mPaint.setColor(0xff667788);//
        canvas.drawPath(pathLocalThis, mPaint);

        mPaint.setColor(0xff908765);//
        canvas.drawPath(pathLocalLast, mPaint);

        mPaint.setColor(0xff114477);//
        canvas.drawPath(pathAllThis, mPaint);

        mPaint.setColor(0xff765432);//
        canvas.drawPath(pathAllLast, mPaint);

        mPaint.setColor(0xffdbe4e6);
        mPaint.setStyle(Style.FILL);

        // canvas.drawLine(0, h-coordinate_pading_b, w, h-coordinate_pading_b, mPaint);
        canvas.drawRect(0, h - coordinate_pading_b / 2, w, h, mPaint);// 标注x刻度
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize((label_text_size / 3) * 2);

        ArrayList<MyPoint> locationPoints = getNotNullPointList();
        if (locationPoints != null) {
            for (int i = 0, isize = months; i < isize; i++) {
                MyPoint p = locationPoints.get(i);
                canvas.drawText(p.getHolder().getStamp(), p.x - 5, h - coordinate_pading_b / 2 + 25, mPaint);
            }
        }

        mPaint.setStrokeWidth(paintSolideBig / 2);
        mPaint.setColor(0xffb0b9c2);// 底部分割线
        canvas.drawLine(0, h, w, h, mPaint);
        mPaint.setStrokeWidth(paintSolideBig);

        if (isSetting) {
            ArrayList<MyPoint> templist = getNotNullPointList();
            if (null != templist) {
                // 找到第一个点
                if (this.startIndex < templist.size()) {
                    MyPoint startPoint = templist.get(this.startIndex);
                    drawMoveLine(canvas, startPoint, cell_width);
                    //画第二根线
                    if (endIndex < startIndex) {
                        endIndex = startIndex + 1;
                    }
                    if (endIndex < templist.size() && templist.size() > 1) {
                        MyPoint endPoint = templist.get(endIndex);
                        drawMoveLine(canvas, endPoint, cell_width);
                        drawRectransparent(canvas, startPoint, endPoint);
                    }

                }
            }
        } else {
            ArrayList<MyPoint> notNullPoints = getNotNullPointList();
            Object rets[] = findNearestPoint(notNullPoints);
            if (rets.length > 1) {
                MyPoint nearestPoint = (MyPoint) rets[0];//最近的触摸点
                //第一个点距离触摸点的
                if (1 == notNullPoints.size()) {
                    mPaint.setColor(0xff34DE71);
                    drawMoveLine(canvas, nearestPoint, cell_width);
                } else {
                    if (startIndex < 0) {
                        startIndex = 0;
                    }
                    if (endIndex < startIndex) {
                        endIndex = startIndex + 1;
                    }
                    if (endIndex >= notNullPoints.size()) {
                        endIndex = notNullPoints.size() - 1;
                    }
                    if (startIndex >= 0 && startIndex < endIndex) {
                        //找到离触摸点最近的点
                        float dstart = cputeDistanceX(mTouchPoint, notNullPoints.get(this.startIndex));
                        float dend = cputeDistanceX(mTouchPoint, notNullPoints.get(this.endIndex));
                        int index = (Integer) rets[1];
                        if (dstart < dend) {
                            //将起始点移动到触摸点	,备注:效果图要求不移动
                            this.startIndex = index;
                        }
                        if (dstart > dend) {
                            //将结束点移动到触摸点
                            this.endIndex = index;
                        }
                        MyPoint startPoint = notNullPoints.get(this.startIndex);
                        MyPoint endPoint = notNullPoints.get(this.endIndex);
                        drawMoveLine(canvas, startPoint, cell_width);//画第一根线
                        drawMoveLine(canvas, endPoint, cell_width);//画第二根线
                        drawRectransparent(canvas, startPoint, endPoint);//画半透明区域
                    }
                }
                if (null != moveListener) {
                    moveListener.onMove(this.endIndex);
                }
            }
        }


    }

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-03-17 22:19:09  更:2022-03-17 22:19:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 18:27:56-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码