Android 曲线覆盖图

news2024/9/23 18:19:02

看图

 

样例代码

layout.xml

<com.XXXXX.utils.GraphBendLine
    android:id="@+id/ghost_view"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_margin="40dp"
    app:node_bar_line_color="#F9FEFD"
    app:node_bar_line_dan_color="#BAEFE6"
    app:node_color="#D7F5F0"
    app:node_line_color="#8BE4D4" />

attr.xml

<declare-styleable name="nodeLine">
    <!--节点默认颜色-->
    <attr name="node_color" format="color" />
    <!--曲线颜色-->
    <attr name="node_bar_line_color" format="color" />
    <!--曲线颜色浅色-->
    <attr name="node_bar_line_dan_color" format="color" />
    <!--进度条默认颜色-->
    <attr name="node_line_color" format="color" />

</declare-styleable>

activity

GraphBendLine mLine = (GraphBendLine) findViewById(R.id.ghost_view);

timeList.add(80f);
timeList.add(0f);
timeList.add(50f);
timeList.add(20f);
timeList.add(60f);
timeList.add(20f);
timeList.add(90f);
dataList.add("3.15");
dataList.add("3.16");
dataList.add("3.17");
dataList.add("3.18");
dataList.add("3.19");
dataList.add("3.20");
dataList.add("3.21");
mLine.updateTime(timeList, dataList);

画布类

public class GraphBendLine extends View {

    private float sumHeight;//总控件的高度
    private float sumWidth;//总空间的宽度
    private float maxY;//y轴最大值
    private float minY;//y轴最小值
    private float centerY;//y轴中间值
    private String unitY;//y轴值单位
    private float maxTime;//最大的时间 用来划分单位的 最小就是20 X1.2是为了给上方和下方预留空间
    private Paint linePaint;//线的画笔
    private Paint shadowPaint;//阴影的画笔
    private Paint mPaint;//曲线画笔
    private Paint circlePaint;//圆点画笔
    private Paint circlePaint2;//圆点画笔
    private Paint scorePaint;
    private Paint textPaint;//文字的画笔
    private ArrayList<Float> dataList;//监测值
    private ArrayList<String> timeList;//底部的时间
    private float oneHeight; //每一个小段所要分成的高
    private float oneWidth;//每一个小段所要分成的宽
    private float buttomHeiht; //给底部一排日期预留出的时间
    private Path baseLinePath;//折线路径
    private float smoothness = 0.36f; //折线的弯曲率
    private Paint baseShadow;//折线下的阴影的画笔
    private String lineColor = "#CBF2ED";//节点默认颜色 "#17CAAA"
    private String barLineColor = "#BAEFE6";//曲线覆盖区颜色
    private String barLineDanColor = "#D7F5F0";//曲线覆盖区浅颜色颜色  "#F9FEFD"
    private String nodeColor = "#8BE4D4";//节点默认颜色
    private ArrayList<PointF> xyList;//储存定好的坐标点的集合

    public GraphBendLine(Context context) {
        super(context);
        initPaint();
    }

    public GraphBendLine(Context context, AttributeSet attrs) {
        super(context, attrs);

//        mContext = context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.nodeLine);
        lineColor = ta.getString(R.styleable.nodeLine_node_line_color);
        nodeColor = ta.getString(R.styleable.nodeLine_node_color);
        barLineColor = ta.getString(R.styleable.nodeLine_node_bar_line_color);
        barLineDanColor = ta.getString(R.styleable.nodeLine_node_bar_line_dan_color);
        ta.recycle();
        initPaint();
    }

    /**
     * 初始化画笔
     *
     * @linpaint 线条画笔
     * @shadowPaint 阴影画笔
     */
    private void initPaint() {
        //画线的画笔
        linePaint = new Paint();
        linePaint.setColor(Color.parseColor("#333333"));
        linePaint.setAntiAlias(true);
        linePaint.setTextSize(dp2px(getContext(), 9));
        linePaint.setStrokeWidth(dp2px(getContext(), 1));
        //画背景的画笔
        shadowPaint = new Paint();
        shadowPaint.setColor(Color.parseColor(lineColor));
        shadowPaint.setAntiAlias(true);
        //画最下方文字的画笔
        textPaint = new Paint();
        textPaint.setColor(Color.parseColor("#333333"));
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(dp2px(getContext(), 8));

        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.WHITE);
        circlePaint.setStrokeWidth(dp2px(getContext(), 2));
        circlePaint.setStyle(Paint.Style.STROKE);

        circlePaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint2.setColor(Color.parseColor(lineColor));
        circlePaint2.setStyle(Paint.Style.FILL);

        baseShadow = new Paint();
        baseShadow.setAntiAlias(true);
        baseShadow.setColor((Color.WHITE & 0x40FFFFFF) | 0x10000000);
        baseShadow.setStyle(Paint.Style.FILL);

        buttomHeiht = dp2px(35);//线距离底部高度

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.parseColor(lineColor));
        mPaint.setStrokeWidth(dp2px(getContext(), 2));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);


        scorePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        scorePaint.setStyle(Paint.Style.STROKE);
        scorePaint.setStrokeCap(Paint.Cap.ROUND);
        scorePaint.setColor(Color.parseColor("#DDDDDD"));
        scorePaint.setStrokeWidth(dp2px(0.5f));
        baseLinePath = new Path();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        sumHeight = getMeasuredHeight();
        sumWidth = getMeasuredWidth();

    }

    private void measure() {
        maxTime = maxY;//最大为300
        for (int i = 0; i < dataList.size(); i++) {
            if (maxTime <= dataList.get(i)) {
                maxTime = dataList.get(i);
            }
        }
        if (maxTime < 20) {
            maxTime = 20;
        }
        String text = "V";
        Rect rect = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), rect);
        oneHeight = ((sumHeight - buttomHeiht - 2 * rect.height()) / maxTime);
        oneWidth = sumWidth / 28;
    }

    /**
     * 设置最大最小值
     */
    public void setMaxMin(float max, float min, float center, String unit) {
        this.maxY = max;
        this.minY = min;
        this.centerY = center;
        this.unitY = unit;
    }

    /**
     * 更新阅读时间
     */

    public void updateTime(ArrayList<Float> timeList, ArrayList<String> bottomList) {
        this.dataList = timeList;
        this.timeList = bottomList;
        if (this.dataList != null && this.dataList.size() > 0 && timeList != null && timeList.size() > 0) {
            invalidate();
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (dataList == null || timeList == null) return;
        maxTime = getMaxTime(dataList);
        measure();
        toGetXy();//获取x和y的坐标

        toDrawLine(canvas);
    }

    private void toGetXy() {
        int leftWidth = dp2px(13);//距离右边宽度
        xyList = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            float x = oneWidth + i * 4 * oneWidth;
            float time = dataList.get(i);//每点时间
            float y = (sumHeight - (oneHeight * time));
            xyList.add(new PointF(x + leftWidth, y - buttomHeiht));
        }
    }

    /**
     * 画线
     */
    private void toDrawLine(Canvas canvas) {
        if (xyList == null || xyList.size() == 0) {
            return;
        }
        List<PointF> NewPoints = new ArrayList<>();
        NewPoints.addAll(xyList);
        float lX = 0;
        float lY = 0;
        baseLinePath.reset();
        baseLinePath.moveTo(NewPoints.get(0).x, NewPoints.get(0).y);
        for (int i = 1; i < NewPoints.size(); i++) {
            PointF p = NewPoints.get(i);
            PointF firstPointF = NewPoints.get(i - 1);
            float x1 = firstPointF.x + lX;
            float y1 = firstPointF.y + lY;

            PointF secondPointF = NewPoints.get(i + 1 < NewPoints.size() ? i + 1 : i);
            lX = (secondPointF.x - firstPointF.x) / 2 * smoothness;
            lY = (secondPointF.y - firstPointF.y) / 2 * smoothness;
            float x2 = p.x - lX;
            float y2 = p.y - lY;
            if (y1 == p.y) {
                y2 = y1;
            }
            baseLinePath.cubicTo(x1, y1, x2, y2, p.x, p.y);
        }
        canvas.drawPath(baseLinePath, mPaint);
        if (timeList.size() > 1) {
            for (int i = 0; i < timeList.size(); i++) {
                float x = NewPoints.get(i).x;
                float y = NewPoints.get(i).y;
                float time = dataList.get(i);
                String mThumbText = String.valueOf(time);
                Rect rect = new Rect();
                linePaint.getTextBounds(mThumbText, 0, mThumbText.length(), rect);
                canvas.drawText(mThumbText, x - rect.width() / 2, y - dp2px(6), linePaint);//画最上面字体
                canvas.drawCircle(x, y, dp2px(3), circlePaint);
                canvas.drawCircle(x, y, dp2px(2), circlePaint2);
                Rect rectf = new Rect();
                textPaint.getTextBounds(timeList.get(i), 0, timeList.get(i).length(), rectf);
                canvas.drawText(timeList.get(i), x - rectf.width() / 2, sumHeight - dp2px(2), textPaint);//画最下方的字体
            }
            drawArea(canvas, NewPoints);
        }
        drawBottomLine(canvas);
        drawLine(canvas);

    }

    /**
     * 分割线
     *
     * @param canvas
     */
    private void drawLine(Canvas canvas) {
        //
        String text = minY + "";
        Rect rect = new Rect();
        scorePaint.getTextBounds(text, 0, text.length(), rect);
        //0
        canvas.drawText(text, 0, sumHeight - buttomHeiht - rect.height() / 2, textPaint);
        //60
        text = centerY + "";
        float y = (sumHeight - (oneHeight * centerY));
        canvas.drawText(text, 0, y - buttomHeiht - rect.height() / 2 - dp2px(4), textPaint);
        canvas.drawLine(0, y - buttomHeiht, sumWidth, y - buttomHeiht, scorePaint);
        //100
        text = maxY + "(" + unitY + ")";
        float y2 = (sumHeight - (oneHeight * maxY));
        canvas.drawText(text, 0, y2 - buttomHeiht - rect.height() / 2 - dp2px(4), textPaint);
        canvas.drawLine(0, y2 - buttomHeiht, sumWidth, y2 - buttomHeiht, scorePaint);
    }

    /**
     * 底部标线
     *
     * @param canvas
     */
    private void drawBottomLine(Canvas canvas) {
        Rect rect = new Rect();
        scorePaint.getTextBounds("0", 0, "0".length(), rect);
        canvas.drawLine(0, sumHeight - dp2px(15) - rect.height() / 2, sumWidth, sumHeight - dp2px(15) - rect.height() / 2, scorePaint);//画底部灰线
    }

    /**
     * 阴影层叠
     *
     * @param canvas
     * @param Points
     */

    private void drawArea(Canvas canvas, List<PointF> Points) {
        LinearGradient mShader = new LinearGradient(0, 0, 0, getMeasuredHeight(), new int[]{Color.parseColor(nodeColor), Color.parseColor(barLineColor), Color.parseColor(barLineDanColor)}, new float[]{0.5f, 0.65f, 0.85f}, Shader.TileMode.REPEAT);
        baseShadow.setShader(mShader);
        if (Points.size() > 0 && xyList != null && xyList.size() > 0) {
            baseLinePath.lineTo(xyList.get(Points.size() - 1).x, sumHeight - buttomHeiht);
            baseLinePath.lineTo(xyList.get(0).x, sumHeight - buttomHeiht);
            baseLinePath.close();
            canvas.drawPath(baseLinePath, baseShadow);
        }

    }

    public int dp2px(float dp) {
        final float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    /**
     * 取出时间里面的最大的一个用来计算总长度
     *
     * @param timeList
     * @return
     */
    public float getMaxTime(List<Float> timeList) {
        maxTime = 0;
        for (int i = 0; i < timeList.size(); i++) {
            if (maxTime < timeList.get(i)) {
                maxTime = timeList.get(i);
            }
        }
        if (maxTime <= 10) {
            maxTime = 10;
        }
        maxTime *= 1.2;
        return maxTime;
    }

    public int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public float dp2px(Resources resources, float dp) {
        final float scale = resources.getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }

    public float sp2px(Resources resources, float sp) {
        final float scale = resources.getDisplayMetrics().scaledDensity;
        return sp * scale;
    }
}

以上就是全部实现

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/728211.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

药物设计学笔记:基础篇

笔记来自《药物设计学》 文章目录 1.药物设计的分子基础2.药物设计的理论基础 1.药物设计的分子基础 化学小分子药物与生物大分子靶标之间相互作用是药物设计的分子基础临床所用的药物50%都属于手性化合物&#xff0c;这是因为药物靶标的基本单元如氨基酸或核苷酸都是手性结构…

快来收藏这几款免费的图标设计软件!

图标素材为网站、应用程序、文档等提供简洁明了的标识&#xff0c;尽管AdobeIllustrator等付费图标设计软件功能强大&#xff0c;但是对于一些轻量级的设计要求&#xff0c;使用免费的图标设计软件是个不错的选择。本文将向大家介绍5款免费的图标设计软件&#xff0c;一起来看看…

【自我提升】Yolov5使用云端GPU训练模型(谷歌免费版和AutoDL收费版对比)

写在前面&#xff1a;继我前几篇Yolov5系列的博客后&#xff0c;来填一个云端训练数据的坑。我公司电脑只能使用CPU训练模型&#xff0c;很拉跨。我自己的笔记本虽然支持GPU&#xff0c;但是运行起来很卡&#xff0c;而且稍微设置设置大一点轮数和批次&#xff0c;就会报当前磁…

CentOS7 主机网卡怎么配置端口聚合

环境&#xff1a; CentOS7 CentOS Linux release 7.7.1908 (Core) 网卡1&#xff1a;瑞昱普通千兆板载网卡 网卡2&#xff1a;EB-LINK intel 82599芯片PCI-E X8 10G 光模块千兆单模1310X1 问题描述&#xff1a; CentOS7 主机网卡怎么配置端口聚合 配置网卡聚合链路的作…

揭秘验证码,你可能不知道的那些事儿

在现代网络世界中&#xff0c;我们经常遇到验证码这个概念。它是一种用于验证使用者身份或防止恶意行为的安全机制。然而&#xff0c;除了表面上的使用和功能&#xff0c;验证码还有许多有趣而不为人所知的方面。本文将带你揭秘验证码背后的秘密&#xff0c;探索你可能不知道的…

Ctfshow web入门 nodejs篇 web334-web344

CTFshow NodeJs web334 前言&#xff1a;做原型链污染&#xff0c;入门的话个人建议先看看P神的文章&#xff0c;只看前四部分就行了。 深入理解 JavaScript Prototype 污染攻击 | 离别歌 (leavesongs.com) 然后也得有一点js基础&#xff0c;不用太多&#xff0c;要不然看起…

浅谈电力监控系统在地铁运维中的应用

贾丽丽 安科瑞电气股份有限公司 上海嘉定 201801 摘要&#xff1a;随着我国工业化进程的不断推进&#xff0c;我国监控系统也实现了长足的发展。以往传统的地铁监控模式已经无法满足当前需求&#xff0c;将电力监控系统和地铁综合监控系统已经进行资源整合&#xff0c;有效…

产品需求还原度低应该怎么办?

在实际开发过程中&#xff0c;产品设计的完整性较难保障和实现&#xff0c;产品需求还原度较低&#xff0c;前后端对产品设计理解不深入&#xff0c;可能会随意修改产品需求。从而造成后期需求设计不完整&#xff0c;产品风险增大等问题。 为了解决这一问题&#xff0c;我们可以…

【Linux进阶之路】gcc/g++、Makefile、git

前言 在正式开始之前我们先补充一个知识点——普通用户提权的操作。 sudo 【指令】那如何将tmp1添加到信任白名单——sudoers文件 中呢&#xff1f; 肯定不能自己加&#xff0c;得超级管理员root加——第一步&#xff1a;切到超级用户。 找到sudoers文件——/etc/sudoers …

生物信息学_玉泉路_课堂笔记_01 第一章生物信息学简介

&#x1f345; 课程&#xff1a;生物信息学_玉泉路_课堂笔记 中科院_2022秋季课 第一学期 &#x1f345; 个人笔记使用 &#x1f345; 2023/7/3 &#x1f345; 教材使用&#xff1a; 生物信息学是应用计算机科学和数学方法研究生物学问题的交叉学科。它包括了多个领域&#xf…

显示Gif菜单

需要 最近研究在底部菜单中加入gif播放。这样显得高级一些。研究了一些技术方案&#xff0c;现在写篇博客&#xff0c;沉淀一下。 效果 实现 通过Glide实现。虽然android官方有AnimatedImageDrawable 但是只支持API28以上&#xff0c;也就是android9.0以上的手机。兼容性太差…

怎样做好客户自助服务?

在当前高速发展的信息化时代&#xff0c;人们已经习惯了即时满足的方式。对于品牌来说&#xff0c;当客户遇到问题时&#xff0c;他们希望能够获得即时细致的解答。如果客户需要等待很长时间才能获取答案&#xff0c;他们的满意度就会降低。因此&#xff0c;企业是否提供客户自…

flutter开发实战-自定义Switch开关控件Widget

flutter开发实战-自定义Switch开关控件 在flutter中实现自定义Switch&#xff0c;主要实现类似IOS的UISwitch样式的开关控件 一、效果图 二、实现Switch开关的Widget 实现自定义Switch的Widget&#xff0c;主要实现交织动画。 交织动画 有些时候我们可能会需要一些复杂的动画…

apache 安装配置 基础篇(二)

在使用Apache时&#xff0c;配置虚拟主机可以允许一个单一的Apache服务器提供不同的网站、域名或IP地址。 Apache虚拟主机的一个重要作用是更好的网站管理能力。当一个虚拟主机被创建时&#xff0c;您可以轻松地分配不同的域名和IP地址&#xff0c;设置不同的目录、文件和权限…

【电影推荐系统】基于 ALS 的协同过滤推荐算法

目录 目的 用户电影推荐矩阵主要思路如下 1 UserId 和 MovieID 做笛卡尔积&#xff0c;产生&#xff08;uid&#xff0c;mid&#xff09;的元组 2 通过模型预测&#xff08;uid&#xff0c;mid&#xff09;的元组。 3 将预测结果通过预测分值进行排序。 4 返回分值最大的 …

js debugger的两种方式

第一种&#xff1a;在js代码中加上debugger class ReactiveEffect {constructor(fn, scheduler) {this.fn fn;this.scheduler scheduler;this.active true;this.deps [];console.log("创建 ReactiveEffect 对象");}run() {debugger; console.log("run…

Spring高手之路8——Spring Bean模块装配的艺术:@Import详解

文章目录 1. Spring手动装配基础2. Spring框架中的模块装配2.1 Import注解简单使用 3. Import模块装配的四种方式3.1 Import注解的功能介绍3.2 导入普通类与自定义注解的使用3.3 导入配置类的策略3.4 使用ImportSelector进行选择性装配3.5 使用ImportBeanDefinitionRegistrar进…

指针进阶详解

目录 指针基本概念 1.字符指针 2.指针数组 3.数组指针 对数组名的理解 小结 指针基本概念 在初阶指针中我们了解到一些指针的基本概念: 1.指针就是个变量&#xff0c;用来存放地址&#xff0c;地址唯一标识一块内存 2.指针的大小是固定的4/8个字节&#xff08;32位/64位平台&…

详解c++---哈希封装

目录标题 哈希桶的代码哈希桶的修改迭代器的实现const迭代器 哈希桶的代码 通过前面的学习大家应该能够很容易的写出下面的代码&#xff1a; #pragma once #include<iostream> #include<vector> using namespace std; template<class K,class V> struct Ha…

2023年互联网行业研究报告

第一章 行业概况 互联网行业是一个广泛的领域&#xff0c;包括所有利用互联网技术进行商业活动的企业和组织。这个行业的核心是互联网&#xff0c;一个全球性的网络&#xff0c;连接着数以亿计的计算设备和用户&#xff0c;使他们可以共享信息、资源和服务。 互联网行业包括网…