Android自定义-曲线利用和认识

Android自定义-曲线利用和认识,第1张

概述曲线开发中并不常用,但是学会曲线绘制能让你的软件更具创造性和无穷的魅力。其他API是绘制基础,我认为曲线是绘画的灵魂。有了它直接起飞。这节课我们学习曲线以及曲线的应用等。一、曲线认识与理解曲线常见的API1.一阶曲线2.二阶曲线3.三阶曲线我们在初中高中

曲线开发中并不常用,但是学会曲线绘制能让你的软件更具创造性无穷的魅力

其他API是绘制基础,我认为曲线是绘画的灵魂。有了它直接起飞。这节课我们学习曲线以及曲线的应用等。





一、曲线认识与理解
曲线常见的API1.一阶曲线2.二阶曲线3.三阶曲线

我们在初中高中学习中学习了各种直线,,椭圆,正玄曲线等对应的坐标系方程吧,接下来我们回顾一下我们的直线和曲线等方程。

第一步我们还是定义一个类新建坐标系,屏幕可旋转横屏显示
package com.example.androID_draw.vIEwimport androID.content.Contextimport androID.graphics.Canvasimport androID.util.AttributeSetimport androID.vIEw.VIEw/** * *  ┌─────────────────────────────────────────────────────────────┐ *  │┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐│ *  ││Esc│!1 │@2 │#3 │$4 │%5 │^6 │&7 │*8 │(9 │)0 │_- │+= │|\ │`~ ││ *  │├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴───┤│ *  ││ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{[ │}] │ BS  ││ *  │├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤│ *  ││ Ctrl │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  ││ *  │├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────┬───┤│ *  ││ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│Shift │Fn ││ *  │└─────┬──┴┬──┴──┬┴───┴───┴───┴───┴───┴──┬┴───┴┬──┴┬─────┴───┘│ *  │      │Fn │ Alt │         Space         │ Alt │Win│   HHKB   │ *  │      └───┴─────┴───────────────────────┴─────┴───┘          │ *  └─────────────────────────────────────────────────────────────┘ * 版权:渤海新能 版权所有 * * @author feiWang * 版本:1.5 * 创建日期:2/8/21 * 描述:AndroID_Draw * E-mail : [email protected] * CSDN:https://blog.csdn.net/m0_37667770/article * GitHub:https://github.com/luhenchang */class LHC_Cubic_VIEw @JvmOverloads constructor(    context: Context?,    attrs: AttributeSet? = null,    defStyleAttr: Int = 0) : VIEw(context, attrs, defStyleAttr) {    init {    }    overrIDe fun onDraw(canvas: Canvas?) {        super.onDraw(canvas)    }}

为了方便观察和绘制进行了网格和坐标轴的绘制。我相信学过上一篇文章的对于画布的变换 *** 作已经熟练掌握了,网格坐标轴的代码我就不再讲解,看图。

1.方程式映射到坐标系

记得我们初中学过Y(x)=ax+b的直线方程吧。我们来看看这个方程映射到坐标系中的图像。
首先定义一个函数 y=2x-80获取一段点集合,为了看效果我们x偶数时候绘制,然后绘制点即可。代码如下:

    private var number=0..420    //直线方程y=2x-80    private fun drawzxline(canvas: Canvas) {        pointList= ArrayList()        //绘制方程式y=10x+20        val gPaint = getPaint()        number.forEach { t ->            val point=PointF()            if (t%2==0) {//x轴偶数点进行绘制                point.x = t.tofloat()                point.y = 2f * t - 80                pointList.add(point)                canvas.drawPoint(point.x, point.y, gPaint)            }        }    }

1.同样圆的方程,椭圆的方程等都可以这样进行映射到坐标系。

2.

所表示的曲线是以O(a,b)为圆心,以r为半径的圆。

3.例如:(x-10)2+(y-10)2=1602我们进行映射到坐标系内。

解方程应该没问题吧。我们来变换为我们熟悉的方程式:

(x-10)2+(y-10)2=1602

1.移项 (y-10)2=1602-(x-10)2

2.开方 转换为函数Math方程式如下:

开方之后有正负值需要注意

y=sqrt(160.0.pow(2.0).tofloat() - ((point.x - 10).todouble()).pow(2.0)).tofloat() + 10y=-sqrt(160.0.pow(2.0).tofloat() - ((pointDown.x - 10).todouble()).pow(2.0)).tofloat() + 10
 //绘制圆圈        number.forEach { t ->            val point = PointF()            val pointDown = PointF()            //(x-10)2+(y-10)2=1602            point.x = t.tofloat()            pointDown.x = t.tofloat()            //y计算应该不用我说吧。            point.y =                sqrt(160.0.pow(2.0).tofloat() - ((point.x - 10).todouble()).pow(2.0)).tofloat() + 10            pointDown.y = -sqrt(                160.0.pow(2.0).tofloat() - ((pointDown.x - 10).todouble()).pow(2.0)            ).tofloat() + 10            canvas.drawPoint(point.x, point.y, gPaint)            canvas.drawPoint(pointDown.x, pointDown.y, gPaint)        }

2.贝塞尔曲线

通过上面我们发现凡是函数都可以和坐标系绘制进行一一映射,当然了贝塞尔曲线也是有方程式的。有如下:

线性贝塞尔曲线

给定点P0、P1,线性贝塞尔曲线只是一条两点之间的直线。这条线由下式给出:
     

二次方贝塞尔曲线

二次方贝塞尔曲线的路径由给定点P0、P1、P2的函数B(t)追踪:
     


三次方贝塞尔曲线

P0、P1、P2、P3四个点在平面或在三维空间中定义了三次方贝塞尔曲线。曲线起始于P0走向P1,并从P2的方向来到P3。一般不会经过P1或P2;公式如下:

当然在AndroID端的Native层已经封装好了方法,二次方贝塞尔曲线三次方贝塞尔曲线,已知函数当然可以进行封装。

在AndroID端提供了二阶和三阶 `二次方贝塞尔曲线`:    public voID quadTo(float x1, float y1, float x2, float y2)    public voID rQuadTo(float dx1, float dy1, float dx2, float dy2)  `三次方贝塞尔曲线`:    public voID cubicTo(float x1, float y1, float x2, float y2,float x3, float y3)    public voID rCubicTo(float x1, float y1, float x2, float y2,float x3, float y3) 

接下来我们绘制一个二阶曲线,控制点可以随着手势的移动和下按进行对应的屏幕移动,对于手势坐标系和屏幕坐标系的映射转换上节折线里面说很明白了,这里不多做解释。

quadTo(float x1, float y1, float x2, float y2)
    //记录移动的canvas画布坐标,不是手势坐标,由手势坐标转换为canvas坐标进行刷新    private var moveX: float = 160f    private var moveY: float = 160f   private fun drawQuz(canvas: Canvas) {        controllRect = Rect(            (moveX - 30f).toInt(),            (moveY + 30f).toInt(),            (moveX + 30).toInt(),            (moveY - 30f).toInt()        )        val quePath = Path()        canvas.drawCircle(0f, 0f, 10f, getPaintCir(Paint.Style.FILL))        canvas.drawCircle(320f, 0f, 10f, getPaintCir(Paint.Style.FILL))        //第一个点和控制点的连线到最后一个点链线。为了方便观察        val lineleft = Path()        lineleft.moveto(0f, 0f)        lineleft.lineto(moveX, moveY)        lineleft.lineto(320f, 0f)        canvas.drawPath(lineleft, getPaint(Paint.Style.stroke))        //第一个p0处画一个圆。第二个p1处画一个控制点圆,最后画一个。        canvas.drawCircle(moveX, moveY, 10f, getPaintCir(Paint.Style.FILL))        quePath.quadTo(moveX, moveY, 320f, 0f)        canvas.drawPath(quePath, getPaint(Paint.Style.stroke))    }    overrIDe fun ontouchEvent(event: MotionEvent): Boolean {        when (event.action) {            ACTION_DOWN,            ACTION_MOVE -> {                //在控制点附近范围内部,进行移动                Log.e("x=", "ontouchEvent: (x,y)"+(event.x - wIDth / 2).toInt()+":"+(-(event.y - height / 2)).toInt())                //将手势坐标转换为屏幕坐标                moveX = event.x - wIDth / 2                moveY = -(event.y - height / 2)                invalIDate()            }        }        return true    }

上图可以拖动控制点,在起点和结尾之间的曲线随着控制点发生了变形。控制点靠近那一侧弧度的凸起就偏向那一侧,初步的认识这一个规律即可,而练习中不断的去调节控制点达到我们的需求。但是在上图中我们回发现弧度不够圆圈,在三阶函数里面可以很好的调节弧度。接下来我们来看看三阶函数

三阶曲线

public voID cubicTo(float x1, float y1, float x2, float y2,float x3, float y3)
同样我们在坐标系内绘制三阶曲线。
为了很好的看到效果我们这次进行来精细的控制,我们可以拖动任意我们想要拖动的控制点进行观察我们的三阶曲线。在上章节折线中对于手势配合Rect的contains方法可以进行局部的点击,当然了拖动也是没问题的。
如下图:我们只需要在控制点附近进行绘制距形包裹住控制点,手势滑动时时刷新控制点对应的距形即可。

  private fun drawCubic(canvas: Canvas) {       val cubicPath=Path()       cubicPath.moveto(0f,0f)       cubicleftRect= Rect(           (moveCubiX - 30f).toInt(),           (moveCubiY - 30f).toInt(),           (moveCubiX + 30).toInt(),           (moveCubiY + 30f).toInt()       )       cubicRightRect=Rect(           (moveCubiXX - 30f).toInt(),           (moveCubiYY - 30f).toInt(),           (moveCubiXX + 30).toInt(),           (moveCubiYY + 30f).toInt()       )        val lineleft = Path()        lineleft.moveto(0f, 0f)        lineleft.lineto(moveCubiX, moveCubiY)        lineleft.lineto(moveCubiXX, moveCubiYY)        lineleft.lineto(320f, 0f)        canvas.drawPath(lineleft, getPaint(Paint.Style.stroke,color.GRAY))        //canvas.drawRect(cubicleftRect, getPaint(Paint.Style.FILL,color.RED))        //canvas.drawRect(cubicRightRect, getPaint(Paint.Style.FILL,color.RED))        canvas.drawCircle(moveCubiX, moveCubiY, 10f, getPaintCir(Paint.Style.FILL))        canvas.drawCircle(moveCubiXX, moveCubiYY, 10f, getPaintCir(Paint.Style.FILL))        cubicPath.cubicTo(moveCubiX,moveCubiY,moveCubiXX,moveCubiYY,320f,0f)        canvas.drawPath(cubicPath, getPaint(Paint.Style.stroke,color.RED))    }       overrIDe fun ontouchEvent(event: MotionEvent): Boolean {        when (event.action) {            ACTION_DOWN,            ACTION_MOVE -> {                //在控制点附近范围内部,进行移动                Log.e(                    "x=",                    "ontouchEvent: (x,y)" + (event.x - wIDth / 2).toInt() + ":" + (-(event.y - height / 2)).toInt()                )                //二阶曲线                if (controllRect.contains((event.x - wIDth / 2).toInt(),(-(event.y - height / 2)).toInt())) {                    Log.e("点击来","对" )                    moveX = event.x - wIDth / 2                    moveY = -(event.y - height / 2)                    invalIDate()                //三阶曲线控制点1                }else if(cubicleftRect.contains((event.x - wIDth / 2).toInt(),(-(event.y - height / 2)).toInt())){                    moveCubiX= event.x - wIDth / 2                    moveCubiY= -(event.y - height / 2)                    invalIDate()                 //三阶曲线控制点2                }else if(cubicRightRect.contains((event.x - wIDth / 2).toInt(),(-(event.y - height / 2)).toInt())){                    moveCubiXX= event.x - wIDth / 2                    moveCubiYY= -(event.y - height / 2)                    invalIDate()                }            }        }        return true    }

到这里我想我们应该大概的明白二阶和三阶曲线对于弧度的大致方向控制了吧。你以为这样就结束了么。接下来下来开始正式的进入曲线应用。

二、曲线-简单应用

在qq群里么有人问过这样的效果如何去做。我想到这里对于大家来说应该很简单了吧。我们接下来就利用上面学习的曲线内容进行编写。

首先准备

        1.一张图片

        2.绘制矩形框

        3.绘制直线和曲线

        4.拖动曲线

        5.改变色相饱和度亮度等

1.图片色相饱和度等的了解

图片在AndroID中并没有提供直接 *** 作Bitmap来改变色相饱和度的API,所以我们需要将bitmap绘制到画布上。接下来我们开始写代码。首先新建类LHC_Image_VIEw,attrs里面添加属性。

属性

<declare-styleable name="LHC_Image_VIEw">  <attr name="defaultImag" format="reference" /></declare-styleable>

获取属性图片,进行绘制到canvas上面,在这里我们需要知道一点关于颜色的API。colorMatrix
图像是由无数的像素点组成,每一个像素点用RGBA四个值来描述,具体是由一个4*5的矩阵来控制每个像素的显示,点的RGBA又共同组成了位图的外观表现形式,所以改变控制像素点的RGBA值,就可以改变位图展示的效果

`colorMatrix`  用4x5矩阵来表示,用于转换位图的颜色和Alpha分量。  [ a, b, c, d, e,    f, g, h, i, j,    k, l, m, n, o,    p, q, r, s, t ]    计算如下:    R = a*R + b*G + c*B + d*A + e;    G = f*R + g*G + h*B + i*A + j;    B = k*R + l*G + m*B + n*A + o;    A = P*R + q*G + r*B + s*A + t; 通常见的默认标准矩阵    [ 1 0 0 0 0               R=225            R1=225 + 0 + 0 + 0+偏移量=225  0 1 0 0 0       x       G=225     =      G1=225 + 0 + 0 + 0+偏移量=225  =  RGBA=[225,225,225,225]  0 0 1 0 0               B=225            B1=225 + 0 + 0 + 0+偏移量=225    0 0 0 1 0 ]             A=225            A1=225 + 0 + 0 + 0+偏移量=225   我想降低绿色和蓝色,那么我们可以将矩阵中的2行2列1变为0.5,3行3列的1变为0.5等 *** 作。 colorMatrix构造方法:我们使用colorMatrix()     public colorMatrix() {        reset();    }    /**     * Create a new colormatrix initialized with the specifIEd array of values.     */    public colorMatrix(float[] src) {        System.arraycopy(src, 0, mArray, 0, 20);    }    /**     * Create a new colormatrix initialized with the specifIEd colormatrix.     */    public colorMatrix(colorMatrix src) {        System.arraycopy(src.mArray, 0, mArray, 0, 20);    }

在colorMatrix中常见的使用方法:setRotate(int axis, float degrees)中用到了degrees * Math.PI / 180d这里为了控制矩阵数组开发者便于编写使用来正余玄函数,将-1到1放大到了0-360度,当然这是我的猜想,毕竟-1到1之间的小数不好把握。为了便于大家使用不妨附上一副正玄图像:


由图我们可以看到0度和90度分别是0和1,那接下来我们进行绘制一个图片设置为int=0代表红色…degrees=90度,源码中修改了矩阵的为

[ 1 0 0 0 0               R      0 1 0 0 0       x       G  0 0 1 0 0               B  =R G B A  0 0 0 1 0 ]             A    [ 1 0 0 0 0               R  0 0 1 0 0       x       G =R B -G A   0 -1 0 0 0              B  0 0 0 1 0 ]             A    结果红没变,绿变为蓝,蓝色变为负,我们知道红和蓝成黄色。那么我们接下来验证一下我们的结果。
    public voID setRotate(int axis, float degrees) {        reset();        double radians = degrees * Math.PI / 180d;        float cosine = (float) Math.cos(radians);        float sine = (float) Math.sin(radians);        switch (axis) {        // Rotation around the red color        case 0:            mArray[6] = mArray[12] = cosine;            mArray[7] = sine;            mArray[11] = -sine;            break;        // Rotation around the green color        case 1:            mArray[0] = mArray[12] = cosine;            mArray[2] = -sine;            mArray[10] = sine;            break;        // Rotation around the blue color        case 2:            mArray[0] = mArray[6] = cosine;            mArray[1] = sine;            mArray[5] = -sine;            break;        default:            throw new RuntimeException();        }    }

验证结果:

class LHC_Image_VIEw @JvmOverloads constructor(    context: Context,    attrs: AttributeSet? = null,    defStyleAttr: Int = 0) : VIEw(context, attrs, defStyleAttr) {    private val MIDDLE_VALUE=127    private var mdrawable: Drawable?    lateinit var bitmap:Bitmap    init {        val array: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.LHC_Image_VIEw)        mdrawable = array.getDrawable(R.styleable.LHC_Image_VIEw_defaultImag)    }    overrIDe fun onDraw(canvas: Canvas) {        super.onDraw(canvas)        if (mdrawable!=null){            bitmap =mdrawable!!.toBitmap(wIDth,height, Bitmap.Config.ARGB_8888)        }else{          return        }        //实例化一支画笔        val mPaint = Paint()        mPaint.strokeWIDth=10f        //实例化处理色相的颜色矩阵        val colorMatrix =  colorMatrix()        //获得色相的计算公式        val gress=90f        //源码里面0表示红色        colorMatrix.setRotate(0, gress)        //将调好的颜色设置给画笔        mPaint.colorFilter = colorMatrixcolorFilter(colorMatrix)        //然后我们用调整好的颜色画笔将原来的图片bmp画到新的bitmap上        canvas.drawBitmap(bitmap, 0f, 0f, mPaint)    }}

验证成功。那么我们继续进行我们曲线绘制学习,有点跑题了。

2.绘制矩形框

这个我想超级简单。

   private fun drawGrID(canvas: Canvas) {        //1.我们左下角为屏幕的圆点进行 *** 作        canvas.translate(0f, height.tofloat())        canvas.scale(1f,-1f)        canvas.save()        val xpath=Path()        xpath.moveto(0f,0f)        xpath.lineto(wIDth.tofloat(), 0f)        val paint=Paint()        paint.color=color.GRAY        paint.strokeWIDth=2f        paint.style= Paint.Style.stroke        for (index in 0 until 6){            canvas.translate(0f,160f)            canvas.drawPath(xpath,paint)        }        canvas.restore()        val ypath=Path()        ypath.moveto(0f,0f)        ypath.lineto(0f, wIDth.tofloat()-120)        val painty=Paint()        painty.color=color.GRAY        painty.strokeWIDth=2f        painty.style= Paint.Style.stroke        canvas.save()        for (index in 0 until 6){            canvas.translate(160f,0f)            canvas.drawPath(ypath,paint)        }        canvas.restore()    }

3.直线和曲线加个手势

如下绘制直线和曲线以及圆圈加个手势。还讲个辣椒,上面都学过了对吧,我想努力点的小伙伴只有想不到没有做不到对不对。如果你还不会我相信你没有仔细阅读我的文章了,代码如下:

      private fun drawlineAndCubit(canvas: Canvas) {        val paint=Paint()        paint.color=color.GRAY        paint.strokeWIDth=2f        paint.style= Paint.Style.stroke        //斜线段        val xpath=Path()        xpath.moveto(10f,10f)        xpath.lineto(wIDth.tofloat(), wIDth.tofloat()-120)        canvas.drawPath(xpath,paint)        //起点和终点圆圈        paint.style= Paint.Style.FILL        paint.color=color.RED        canvas.drawCircle(15f,15f,15f,paint)        canvas.drawCircle(wIDth.tofloat()-15, wIDth.tofloat()-120-15,15f,paint)        val cubicPath=Path()        cubicPath.moveto(0f,0f)        paint.style= Paint.Style.stroke        paint.strokeWIDth=5f        cubicPath.quadTo(moveX, moveY,wIDth.tofloat()-15, wIDth.tofloat()-120-15)        //绘制曲线        canvas.drawPath(cubicPath,paint)    }    overrIDe fun ontouchEvent(event: MotionEvent): Boolean {        when (event.action) {            MotionEvent.ACTION_DOWN,            MotionEvent.ACTION_MOVE -> {                //在控制点附近范围内部,进行移动                moveX= event.x                 moveY= -(event.y - height)                invalIDate()            }        }        return true    }

三、曲线、贴图、裁剪的使用

很多能量球或者进度球等悬浮球体等都有着白塞尔曲线的身影。我们接下来进行简单的应用,写个鱼儿游动的小动画。这个过程我们需要的就是简单的数学计算画布的裁剪绘制图片到画布等基本常用 *** 作。下面小案例我们结合曲线动画和相关API进行逐步分析。

绘制分析
1.海浪的绘制->曲线的绘制2.会动的海浪->画布的平移3.始终运动的动画4.随着海浪游动的鱼儿->画布上图片的绘制
1.海浪的绘制

如上图我们可以看到不断运动着的曲线海浪。那么海浪是如何绘制而成的呢?白塞尔曲线完全可以完成下面的绘制吧。


代码:

//绘制波浪    private fun drawWave(canvas: Canvas) {        val wavePath=Path()        wavePath.moveto(0f, -waveWIDth * 6)        wavePath.lineto(0f, 0f)        wavePath.quadTo(waveWIDth, waveHeight, waveWIDth * 2, 0f)        wavePath.quadTo(waveWIDth * 3, -waveHeight, waveWIDth * 4, 0f)        wavePath.quadTo(waveWIDth * 5, waveHeight, waveWIDth * 6, 0f)        wavePath.quadTo(waveWIDth * 7, -waveHeight, waveWIDth * 8, 0f)        wavePath.lineto(waveWIDth * 8, -waveWIDth * 6)        canvas.drawPath(wavePath, getPaintBefor(Paint.Style.FILL))
2.会动的海浪

海浪的绘制仅仅是曲线的绘制闭合之后填充颜色而已,曲线的运动简单的可以通过移动画布来完成。

//海浪通过画布的平移随之而动。canvas.translate(animal.animatedValue as float, 0f) 

海浪在一个或者矩形或者各种形状容器中运动的效果。
我们只需要将画布进行裁剪出一个想要的形状然后让海浪在裁剪过的部分观看即可。


代码:

    private fun clipCanvas(canvas: Canvas,type:Int) {        if (type==0) {            val rect = Rect(                (waveWIDth * 4).toInt(),                160,                (waveWIDth * 8).toInt(),                (-waveWIDth * 4).toInt()            )            canvas.clipRect(rect)        }else  if (type==1) {            val circlePath = Path()            circlePath.addCircle(480f, 0f, 160f, Path.Direction.ccw)            canvas.drawCircle(480f, 0f, 160f, getPaint(Paint.Style.stroke))            canvas.clipPath(circlePath)        }else  if (type==2){            val rundRect = Path()            rundRect.addRoundRect(waveWIDth * 4,waveWIDth*3,waveWIDth* 8,-waveWIDth*3,60f,60f,Path.Direction.ccw)            //canvas.drawPath(rundRect,getPaint(Paint.Style.stroke))            canvas.clipPath(rundRect)        }    }

但是动画的值设置是一个简单的数学计算。

动画的值只需要平移一整个波长即可= waveWIDth * 4

 init {        animal=ObjectAnimator.offloat(0f, waveWIDth * 4)        animal.duration = 2000        animal.repeatCount = ValueAnimator.INFINITE;        animal.interpolator = linearInterpolator()        animal.addUpdateListener {            invalIDate()        }    }
3.随着海浪游动的鱼儿

画布平移,如果在画布上固定位置贴图,那么图也会随着画布的移动而移动。

//可以阅读相关的API,将一个图片绘制到画布坐标系(left,top)的位置。drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)

素材可以在阿里巴巴图片素材库进行下载等。

  val arrList= arraylistof( R.drawable.yu, R.drawable.ziyuan, R.drawable.jingyu)    private fun drawFish(canvas: Canvas) {        val bmp = BitmapFactory.decodeResource(resources, arrList[0])        canvas.drawBitmap(bmp, waveWIDth * 3, -waveHeight * 2.2f, getPaint(Paint.Style.FILL))        val bmpzy = BitmapFactory.decodeResource(resources,arrList[1])        canvas.drawBitmap(bmpzy, waveWIDth, -waveHeight *3f, getPaint(Paint.Style.FILL))        val bmpjy= BitmapFactory.decodeResource(resources,arrList[2])        canvas.drawBitmap(bmpjy, waveWIDth* 6, -waveHeight * 3.5f, getPaint(Paint.Style.FILL))    }

最终效果:

                                       


代码:

package com.example.androID_draw.vIEwimport androID.animation.ObjectAnimatorimport androID.animation.ValueAnimatorimport androID.content.Contextimport androID.graphics.*import androID.util.AttributeSetimport androID.util.Logimport androID.vIEw.MotionEventimport androID.vIEw.MotionEvent.ACTION_DOWNimport androID.vIEw.MotionEvent.ACTION_MOVEimport androID.vIEw.VIEwimport androID.vIEw.animation.linearInterpolatorimport com.example.androID_draw.Rimport kotlin.math.maximport kotlin.random.Random/** * *  ┌─────────────────────────────────────────────────────────────┐ *  │┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐│ *  ││Esc│!1 │@2 │#3 │$4 │%5 │^6 │&7 │*8 │(9 │)0 │_- │+= │|\ │`~ ││ *  │├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴───┤│ *  ││ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{[ │}] │ BS  ││ *  │├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤│ *  ││ Ctrl │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  ││ *  │├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────┬───┤│ *  ││ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│Shift │Fn ││ *  │└─────┬──┴┬──┴──┬┴───┴───┴───┴───┴───┴──┬┴───┴┬──┴┬─────┴───┘│ *  │      │Fn │ Alt │         Space         │ Alt │Win│   HHKB   │ *  │      └───┴─────┴───────────────────────┴─────┴───┘          │ *  └─────────────────────────────────────────────────────────────┘ * 版权:渤海新能 版权所有 * * @author feiWang * 版本:1.5 * 创建日期:2/8/21 * 描述:AndroID_Draw * E-mail : [email protected] * CSDN:https://blog.csdn.net/m0_37667770/article * GitHub:https://github.com/luhenchang */@Suppress("DEPRECATION")class LHC_wave_VIEw @JvmOverloads constructor(    context: Context?,    attrs: AttributeSet? = null,    defStyleAttr: Int = 0) : VIEw(context, attrs, defStyleAttr) {    private var moveX: float = 0f    private var moveY: float = 0f    private var hCount: Int = 0    private var wCount: Int = 0    private lateinit var pointList: ArrayList<PointF>    //网格的宽度    var grIDWIDth = 80    //半波长    val waveWIDth=80f    val waveHeight=30f    lateinit var animal:ValueAnimator    init {        animal=ObjectAnimator.offloat(0f, waveWIDth * 4)        animal.duration = 2000        animal.repeatCount = ValueAnimator.INFINITE;        animal.interpolator = linearInterpolator()        animal.addUpdateListener {            invalIDate()        }    }    overrIDe fun onDraw(canvas: Canvas) {        super.onDraw(canvas)        //绘制网格线        drawGrIDline(canvas)        //绘制文字x和y轴的        draw@R_403_5929@AndY(canvas)        //裁剪画布        clipCanvas(canvas,2)        //绘制波浪        drawWave(canvas)        //绘制        drawFish(canvas)    }    val arrList= arraylistof( R.drawable.yu, R.drawable.ziyuan, R.drawable.jingyu)    private fun drawFish(canvas: Canvas) {        val bmp = BitmapFactory.decodeResource(resources, arrList[0])        canvas.drawBitmap(bmp, waveWIDth * 3, -waveHeight * 2.2f, getPaint(Paint.Style.FILL))        val bmpzy = BitmapFactory.decodeResource(resources,arrList[1])        canvas.drawBitmap(bmpzy, waveWIDth, -waveHeight *3f, getPaint(Paint.Style.FILL))        val bmpjy= BitmapFactory.decodeResource(resources,arrList[2])        canvas.drawBitmap(bmpjy, waveWIDth* 6, -waveHeight * 3.5f, getPaint(Paint.Style.FILL))    }    private fun clipCanvas(canvas: Canvas,type:Int) {        if (type==0) {            val rect = Rect(                (waveWIDth * 4).toInt(),                160,                (waveWIDth * 8).toInt(),                (-waveWIDth * 4).toInt()            )            canvas.clipRect(rect)        }else  if (type==1) {            val circlePath = Path()            circlePath.addCircle(480f, 0f, 160f, Path.Direction.ccw)            canvas.drawCircle(480f, 0f, 160f, getPaint(Paint.Style.stroke))            canvas.clipPath(circlePath)        }else  if (type==2){            val rundRect = Path()            rundRect.addRoundRect(waveWIDth * 4,waveWIDth*3,waveWIDth* 8,-waveWIDth*3,60f,60f,Path.Direction.ccw)            //canvas.drawPath(rundRect,getPaint(Paint.Style.stroke))            canvas.clipPath(rundRect)        }    }    //绘制波浪    private fun drawWave(canvas: Canvas) {        canvas.translate(animal.animatedValue as float, 0f) //内层海浪        val wavePath=Path()        wavePath.moveto(0f, -waveWIDth * 6)        wavePath.lineto(0f, 0f)        wavePath.quadTo(waveWIDth, waveHeight, waveWIDth * 2, 0f)        wavePath.quadTo(waveWIDth * 3, -waveHeight, waveWIDth * 4, 0f)        wavePath.quadTo(waveWIDth * 5, waveHeight, waveWIDth * 6, 0f)        wavePath.quadTo(waveWIDth * 7, -waveHeight, waveWIDth * 8, 0f)        wavePath.lineto(waveWIDth * 8, -waveWIDth * 6)        canvas.drawPath(wavePath, getPaintBefor(Paint.Style.FILL))        canvas.translate(animal.animatedValue as float, 0f) //内层海浪        val wavePath_out=Path()        wavePath_out.moveto(-waveWIDth * 7, -waveWIDth * 6)        wavePath_out.lineto(-waveWIDth * 7, 0f)        wavePath_out.quadTo(-waveWIDth * 7, waveHeight, -waveWIDth * 6, 0f)        wavePath_out.quadTo(-waveWIDth * 5, -waveHeight, -waveWIDth * 4, 0f)        wavePath_out.quadTo(-waveWIDth * 3, waveHeight, -waveWIDth * 2, 0f)        wavePath_out.quadTo(-waveWIDth, -waveHeight, 0f, 0f)        wavePath_out.quadTo(waveWIDth, waveHeight, waveWIDth * 2, 0f)        wavePath_out.quadTo(waveWIDth * 3, -waveHeight, waveWIDth * 4, 0f)        wavePath_out.quadTo(waveWIDth * 5, waveHeight, waveWIDth * 6, 0f)        wavePath_out.quadTo(waveWIDth * 7, -waveHeight, waveWIDth * 8, 0f)        wavePath_out.lineto(waveWIDth * 8, -waveWIDth * 6)        canvas.drawPath(wavePath_out, getPaint(Paint.Style.FILL))    }    private fun getPaintIn(style: Paint.Style): Paint {        val gPaint = Paint()        gPaint.color = color.BLUE        gPaint.strokeWIDth = 2f        gPaint.isAntiAlias = true        gPaint.style = style        gPaint.textSize = 26f        gPaint.color = color.argb(255, 75, 151, 79)        var linearGradIEnt = linearGradIEnt(            waveWIDth * 4, -waveWIDth * 8,            waveWIDth * 4,            80f,            color.argb(255, 47, 26, 253),            color.argb(255, 24, 220, 253),            Shader.TileMode.CLAMP        )        gPaint.shader=linearGradIEnt        return gPaint    }    overrIDe fun ontouchEvent(event: MotionEvent): Boolean {        when (event.action) {            ACTION_DOWN,            ACTION_MOVE -> {                animal.start()                //在控制点附近范围内部,进行移动                Log.e(                    "x=",                    "ontouchEvent: (x,y)" + (event.x - wIDth / 2).toInt() + ":" + (-(event.y - height / 2)).toInt()                )                moveX = event.x - wIDth / 2                moveY = -(event.y - height / 2)                invalIDate()            }        }        return true    }    private fun draw@R_403_5929@AndY(canvas: Canvas) {        val gPaint = Paint()        gPaint.color = color.BLUE        gPaint.strokeWIDth = 2f        gPaint.isAntiAlias = true        gPaint.style = Paint.Style.stroke        gPaint.textSize = 26f        gPaint.color = color.argb(255, 75, 151, 79)        canvas.scale(-1f, 1f)        canvas.save()        canvas.scale(1f, -1f)        //x轴正方形文字        for (index in 1 until wCount / 2) {            val rectText = Rect()            canvas.translate(160f, 0f)            gPaint.getTextBounds(                (80 * index * 2).toString(),                0,                (80 * index * 2).toString().length,                rectText            )            canvas.drawText(                (80 * index * 2).toString(),                -(rectText.wIDth() / 2).tofloat(), rectText.height().tofloat() * 2f, gPaint            )        }        canvas.restore()        canvas.save()        //x轴负方向文字绘制        canvas.scale(1f, -1f)        for (index in 1 until wCount / 2) {            val rectText = Rect()            canvas.translate(-160f, 0f)            gPaint.getTextBounds(                "-${(80 * index * 2)}",                0,                (80 * index * 2).toString().length,                rectText            )            canvas.drawText(                "-${(80 * index * 2)}",                -(rectText.wIDth() / 2).tofloat(), rectText.height().tofloat() * 2f, gPaint            )        }        canvas.restore()        canvas.save()        //x轴负方向文字绘制        canvas.scale(1f, -1f)        canvas.translate(20f, 0f)        //y轴负方向        for (index in 1 until hCount / 2) {            val rectText = Rect()            canvas.translate(0f, 160f)            gPaint.getTextBounds(                "-${(80 * index * 2)}",                0,                (80 * index * 2).toString().length,                rectText            )            canvas.drawText(                "-${(80 * index * 2)}",                0f, rectText.height().tofloat(), gPaint            )        }        canvas.restore()        canvas.save()        canvas.scale(1f, 1f)        canvas.translate(20f, 0f)        //y轴正方向        for (index in 1 until hCount / 2) {            val rectText = Rect()            canvas.translate(0f, 160f)            canvas.save()            canvas.scale(1f, -1f)            gPaint.getTextBounds(                "${(80 * index * 2)}",                0,                (80 * index * 2).toString().length,                rectText            )            canvas.drawText(                "${(80 * index * 2)}",                0f, rectText.height().tofloat(), gPaint            )            canvas.restore()        }        canvas.restore()    }    private fun drawGrIDline(canvas: Canvas) {        //初始化一个画笔        val gPaint = Paint()        gPaint.color = color.BLUE        gPaint.strokeWIDth = 2f        gPaint.isAntiAlias = true        gPaint.style = Paint.Style.FILL        gPaint.shader = RadialGradIEnt(            0f,            0f,            max(wIDth, height) / 2f,            color.BLUE,            color.YELLOW,            Shader.TileMode.CLAMP        )        //onDraw中已经知道屏幕宽度和高度        val screenWIDth = wIDth        val screenHeight = height        //宽的格子个数        wCount = screenWIDth / grIDWIDth        //高的格子个数        hCount = screenHeight / grIDWIDth        //1.将坐标点移动到屏幕的中点        canvas.translate((screenWIDth / 2).tofloat(), (screenHeight / 2).tofloat())        //整体坐标系方向顺时针进行变化        //2.修改y轴上方为正方向。        canvas.scale(1f, -1f)        //绘制x轴和y轴        canvas.drawline(-screenWIDth / 2f, 0f, screenWIDth / 2f, 0f, gPaint)        canvas.drawline(0f, -screenHeight / 2f, 0f, screenHeight / 2f, gPaint)        gPaint.color = color.argb(61, 111, 111, 111)        drawGrIDCode(canvas, screenWIDth, gPaint, hCount, screenHeight, wCount)        //2.修改y轴下方为正方向。        canvas.scale(1f, -1f)        drawGrIDCode(canvas, screenWIDth, gPaint, hCount, screenHeight, wCount)        //3.修改x轴左正方向。        canvas.scale(-1f, 1f)        drawGrIDCode(canvas, screenWIDth, gPaint, hCount, screenHeight, wCount)        //4.修改x作为正y上为正        canvas.scale(1f, -1f)        drawGrIDCode(canvas, screenWIDth, gPaint, hCount, screenHeight, wCount)    }    private fun drawGrIDCode(        canvas: Canvas,        screenWIDth: Int,        gPaint: Paint,        hCount: Int,        screenHeight: Int,        wCount: Int    ) {        //这里保存好坐标圆点为屏幕中心的快照到堆栈里面。方便后期 *** 作。        canvas.save()        //绘制一条横着的线条,重圆点(0,0)开始        //canvas.drawline(0f, 0f, (screenWIDth / 2).tofloat(), 0f, gPaint)        //3.绘制完成第一象限的平行x轴的线        for (index in 0 until hCount) {            //坐标系圆点不断向上平移grIDWIDth的高度            canvas.translate(0f, grIDWIDth.tofloat())            //在平移完的圆点直接画直线即可            canvas.drawline(0f, 0f, (screenWIDth / 2).tofloat(), 0f, gPaint)        }        //恢复到快照状态。即圆点在中心        canvas.restore()        canvas.save()        //4.绘制平行y轴的        //canvas.drawline(0f, 0f, 0f, screenHeight / 2f, gPaint)        for (index in 0 until wCount) {            //坐标系圆点不断向上平移grIDWIDth的高度            canvas.translate(grIDWIDth.tofloat(), 0f)            //在平移完的圆点直接画直线即可            canvas.drawline(0f, 0f, 0f, screenHeight / 2f, gPaint)        }        //恢复到快照状态。即圆点在中心        canvas.restore()    }    private fun getPaintBefor(style: Paint.Style): Paint {        val gPaint = Paint()        gPaint.color = color.BLUE        gPaint.strokeWIDth = 2f        gPaint.isAntiAlias = true        gPaint.style = style        gPaint.textSize = 26f        gPaint.color = color.argb(255, 75, 151, 79)        var linearGradIEnt = linearGradIEnt(            waveWIDth * 4, -waveWIDth * 8,            waveWIDth * 4,            80f,            color.argb(155, 27, 134, 244),            color.argb(195, 24, 220, 253),            Shader.TileMode.CLAMP        )        gPaint.shader=linearGradIEnt        return gPaint    }    private fun getPaint(style: Paint.Style): Paint {        val gPaint = Paint()        gPaint.color = color.BLUE        gPaint.strokeWIDth = 2f        gPaint.isAntiAlias = true        gPaint.style = style        gPaint.textSize = 26f        gPaint.color = color.argb(255, 75, 151, 79)        var linearGradIEnt = linearGradIEnt(            waveWIDth * 4, -waveWIDth * 2,            waveWIDth * 4,            80f,            color.argb(255, 27, 134, 244),            color.argb(255, 24, 220, 253),            Shader.TileMode.CLAMP        )        gPaint.shader=linearGradIEnt        return gPaint    }}
四、曲线图表

图表最常用而最简单,无非绘制API简单的数学计算手势即可。接下来我们进行绘制市面上常见的曲线图表。

1.变换坐标

首先我们进行左边变换为我们常见的坐标系:将左边和底部文字空白处进行留出来。如下进行变换如果没看懂那就看看上一节折线里面讲解的坐标系变换。

        //y轴向上为负方向        canvas.scale(1f, -1f)        //坐标系向右平移leftWIDth,向下平移动 height-bootomHeight,这里向上是正哦。所以向下平移是负数        canvas.translate(leftWIDth, -(height - bottomHeight))
//新建类LHC_curve_VIEwclass LHC_curve_VIEw @JvmOverloads constructor(    context: Context?,    attrs: AttributeSet? = null,    defStyleAttr: Int = 0) : VIEw(context, attrs, defStyleAttr) {    //距离左边的距离    val leftWIDth = 180f    //距离最底部的高度    val bottomHeight = 240f    //x轴的宽度    var x_scaleWIDth=0f    //方格的宽高    private var grID_wIDth=0f    init {    }    overrIDe fun onDraw(canvas: Canvas) {        super.onDraw(canvas)        //2.画布平移        translateCanvas(canvas)    }    //平移画布    private fun translateCanvas(canvas: Canvas) {        //y轴向上为负方向        canvas.scale(1f, -1f)        //坐标系向右平移leftWIDth,向下平移动 height-bootomHeight,这里向上是正哦。所以向下平移是负数        canvas.translate(leftWIDth, -(height - bottomHeight))     }    }
2.绘制平行x轴的线

绘制平行x轴的线,上面我们已经转换坐标系接下来我们绘制平行x轴的线

//绘制平行x轴的线    private fun drawline(canvas: Canvas) {        //canvas.drawcolor(color.argb(22, 255, 255, 111))        val line_paint = Paint()        line_paint.strokeWIDth = 2f        line_paint.style = Paint.Style.stroke        line_paint.color = color.argb(100, 188, 188, 188)        x_scaleWIDth = (wIDth - leftWIDth - 80f)        grID_wIDth = x_scaleWIDth / 6        //绘制最底部一条线        val x_path = Path()        x_path.moveto(0f, 0f)        x_path.lineto(x_scaleWIDth, 0f)        canvas.drawPath(x_path, line_paint)        canvas.save()        //通过平移画布绘制剩余的平行x轴线        (0 until 3).forEach { index ->            canvas.translate(0f, grID_wIDth-40f)            canvas.drawPath(x_path, line_paint)        }        canvas.restore()    }

效果如下:

3.绘制文字文字绘制x轴下方的文字,无非涉及到文字的测量和画布的变换。
1.Paint的getTextBounds结合Rect进行测量2.利用canvas的save和restore进行画布随意的变换
 //水平方向的x轴下面的文字    private fun drawDownOfXlineText(canvas: Canvas) {        val text_paint = Paint()        text_paint.strokeWIDth = 2f        text_paint.style = Paint.Style.stroke        text_paint.color = color.argb(100, 111, 111, 111)        text_paint.textSize=24f        val rectText=Rect()        canvas.save()        //将文字旋转摆正,此时坐标系y向下是正        canvas.scale(1f,-1f)        (0 until 7).forEach { index ->            if(index>0) {                canvas.translate(grID_wIDth, 0f)            }            val strTx= "11.${11+index}"            text_paint.getTextBounds(strTx,0,strTx.length,rectText)            canvas.drawText(strTx, -rectText.wIDth().tofloat()/2, rectText.height().tofloat()*2.5f,text_paint)        }        canvas.restore()    }             private fun drawleftOfYlineText(canvas: Canvas) {        val text_paint = Paint()        text_paint.strokeWIDth = 2f        text_paint.style = Paint.Style.stroke        text_paint.color = color.argb(100, 111, 111, 111)        text_paint.textSize=24f        val rectText=Rect()        canvas.save()        //将文字旋转摆正,此时坐标系y向下是正        (0 until 4).forEach { index ->            if(index>0) {                canvas.translate(0f, grID_wIDth-40f)            }            var strTx=""            if(index==0){                strTx="${index}"            }else if(index==1){                strTx="${500}"            }else if(index==2){                strTx="1k"            }else{                strTx="1.5k"            }            canvas.save()            canvas.scale(1f,-1f)            text_paint.getTextBounds(strTx,0,strTx.length,rectText)            canvas.drawText(strTx, -rectText.wIDth().tofloat()-42f, rectText.height().tofloat()/2,text_paint)            canvas.restore()        }        canvas.restore()    }

效果

3.曲线的绘制二阶曲线绘制,我们会发现缺少控制点。下面我们来进行控制点的计算

如上图我们不难得出控制点 controX=( x 1 x_1 x1​+ x 2 x_2 x2​)/2、controY=( y 1 y_1 y1​+ y 2 y_2 y2​)/2。接下来我们进行绘制曲线。x轴grID_wIDth=1天的时间。不难计算出中间控制点的x。y轴500=grID_wIDth-40f。

代码如下:

    private fun drawCaves(canvas: Canvas) {        val text_paint = Paint()        text_paint.strokeWIDth = 2f        text_paint.style = Paint.Style.stroke        text_paint.color = color.argb(100, 111, 111, 111)       val caves_path=Path()        //500=grID_wIDth-40 每个单位的长度的=像素长度       val danweiY=(grID_wIDth-40)/500       val danweiX=(grID_wIDth)       for (index in 0 until dataList.size-1){            if (dataList[index]==dataList[index+1]){               caves_path.quadTo(                   (grID_wIDth * index + grID_wIDth * (1 + index)) / 2,                   0f,                   grID_wIDth * (index + 1),                   (dataList[index + 1].tofloat()) * danweiY               )           }else {               caves_path.quadTo(                   (grID_wIDth * index + grID_wIDth * (1 + index)) / 2,                   (dataList[index].tofloat() * danweiY + dataList[index + 1].tofloat() * danweiY) / 2 +100,                   grID_wIDth * (index + 1),                   (dataList[index + 1].tofloat()) * danweiY               )           }       }       canvas.drawCircle(0f,0f,10f,text_paint)       canvas.drawPath(caves_path,text_paint)    }


但是我们会发现在两个焦点处没有弧度。很生硬,接下来我们继续分析和解决达到完美。至于最终效果我们可以更酷更炫。

4.三阶曲线的拯救

当 y 1 y_1 y1​< y 2 y_2 y2​如上图1.求出中点坐标x轴下部分控制点x+40px,上部分x-40px,y轴也可以调整来搞搞平滑度下部分控制点y-40x,上部分y+40。
1.获取中点的坐标( X 中 X_中 X中​、 Y 中 Y_中 Y中​)= (( x 1 x_1 x1​+ x 2 x_2 x2​)/2、( y 1 y_1 y1​+ y 2 y_2 y2​)/2)
2. x 1 x_1 x1​到 X 中 X_中 X中​之间的坐标=(( x 1 x_1 x1​+ x 中 x_中 x中​)/2、( y 1 y_1 y1​+ y 中 y_中 y中​)/2)
3. x 中 x_中 x中​到 X 2 X_2 X2​之间的坐标=(( x 中 x_中 x中​+ x 2 x_2 x2​)/2、( y 中 y_中 y中​+ y 2 y_2 y2​)/2)

当 y 1 y_1 y1​> y 2 y_2 y2​如上图2.求出中点坐标x轴上部分+40px,下部分x-40px,y轴也可以调整,y轴也可以调整来搞搞平滑度上部分控制点y+40x,下部分y-40。
1.获取中点的坐标( X 中 X_中 X中​、 Y 中 Y_中 Y中​)= (( x 1 x_1 x1​+ x 2 x_2 x2​)/2、( y 1 y_1 y1​+ y 2 y_2 y2​)/2)
2. x 1 x_1 x1​到 X 中 X_中 X中​之间的坐标=(( x 1 x_1 x1​+ x 中 x_中 x中​)/2、( y 1 y_1 y1​+ y 中 y_中 y中​)/2)
3. x 中 x_中 x中​到 X 2 X_2 X2​之间的坐标=(( x 中 x_中 x中​+ x 2 x_2 x2​)/2、( y 中 y_中 y中​+ y 2 y_2 y2​)/2)

接下来我们堆代码:

   //绘制曲线    private fun drawCaves(canvas: Canvas) {        val text_paint = Paint()        text_paint.strokeWIDth = 2f        text_paint.style = Paint.Style.stroke        text_paint.color = color.argb(100, 111, 111, 111)        val caves_path = Path()        //500=grID_wIDth-40 每个单位的长度的=像素长度        val danweiY = (grID_wIDth - 40) / 500        val danweiX = (grID_wIDth)        for (index in 0 until dataList.size - 1) {            //二阶曲线显示比较尴尬            //           if (dataList[index]==dataList[index+1]){            //               caves_path.quadTo(            //                   (grID_wIDth * index + grID_wIDth * (1 + index)) / 2,            //                   0f,            //                   grID_wIDth * (index + 1),            //                   (dataList[index + 1].tofloat()) * danweiY            //               )            //           }else {            //               caves_path.quadTo(            //                   (grID_wIDth * index + grID_wIDth * (1 + index)) / 2,            //                   (dataList[index].tofloat() * danweiY + dataList[index + 1].tofloat() * danweiY) / 2 +100,            //                   grID_wIDth * (index + 1),            //                   (dataList[index + 1].tofloat()) * danweiY            //               )            //           }            //三阶曲线来显示            val xMovedistance=40            val yMovedistance=40            if (dataList[index] == dataList[index + 1]) {                caves_path.lineto(danweiX*(index+1),0f)            } else if(dataList[index] < dataList[index + 1]){//y1<y2情况                val centerX=(grID_wIDth * index + grID_wIDth * (1 + index)) / 2                val centerY=(dataList[index].tofloat() * danweiY + dataList[index + 1].tofloat() * danweiY) / 2                val controX0=(grID_wIDth * index+centerX)/2                val controY0=(dataList[index].tofloat() * danweiY+centerY)/2                val controX1=(centerX+ grID_wIDth * (1 + index))/2                val controY1=(centerY+dataList[index+1].tofloat() * danweiY)/2                caves_path.cubicTo(controX0+xMovedistance,controY0-yMovedistance,controX1-xMovedistance,controY1+yMovedistance,grID_wIDth * (1 + index),dataList[index + 1].tofloat() * danweiY)            }else{                val centerX=(grID_wIDth * index + grID_wIDth * (1 + index)) / 2                val centerY=(dataList[index].tofloat() * danweiY + dataList[index + 1].tofloat() * danweiY) / 2                val controX0=(grID_wIDth * index+centerX)/2                val controY0=(dataList[index].tofloat() * danweiY+centerY)/2                val controX1=(centerX+ grID_wIDth * (1 + index))/2                val controY1=(centerY+dataList[index+1].tofloat() * danweiY)/2                caves_path.cubicTo(controX0+xMovedistance,controY0+yMovedistance,controX1-xMovedistance,controY1-yMovedistance,grID_wIDth * (1 + index),dataList[index + 1].tofloat() * danweiY)            }        }        canvas.drawCircle(0f, 0f, 10f, text_paint)        canvas.drawPath(caves_path, text_paint)    }

看看效果吧

是不是有点感觉了,无非控制点进行调节罢了,我们看到人家的比较尖,我们对于的xMovedistance,yMovedistance进行调整

val xMovedistance=20
val yMovedistance=40

到这里我们最大的困难也简单的解决了吧?无非简单的加减乘除是不是?接下来我们进行装饰美丽的曲线,学过之前的文章对于这几点技能我想我们都熟能生巧的创作了吧?渐变填充动画点击手势贴图

6.美化曲线我记得说过要画比它更骚的,我想我们应该能办到,多和骚不一定美,但是处处是技术,那下来我们在多和骚方面入手,至于结果我们不敢说比UI设计更美对吧。1.美化技能之渐变填充我们发现设计图有渐变从上到下逐渐变淡。我们选择线性渐变最高点(0,y)到(0,0)即可。心里没数的遍历求最大值。我数据为了方便写死的。
 val linearGradIEnt = linearGradIEnt(            0f, 1500 * danweiY,            0f,            0f,            color.argb(255,229,160,144),            color.argb(255,251,244,240),            Shader.TileMode.CLAMP        )        text_paint.shader = linearGradIEnt

2.美化技能之环绕Path我们发现人家的UI有个红色线进行了环绕。我们直接拿着曲线进行设置画笔即可。每个顶点需要加个圈。
         //绘制闭合渐变曲线        canvas.drawPath(caves_path, text_paint)        val line_paint = Paint()        line_paint.strokeWIDth = 3f        line_paint.style = Paint.Style.stroke        line_paint.color = color.argb(255, 212, 100, 77)        //绘制外环红色线        canvas.drawPath(caves_path, line_paint)        line_paint.style = Paint.Style.FILL        //画圈。        for (index in 0 until dataList.size ) {           canvas.drawCircle(grID_wIDth*index,danweiY*dataList[index],6f,line_paint)        }

看看效果:


绘制文字

 //绘制前7天和后7天文字按钮    private fun drawTextbutton(canvas: Canvas) {        val line_paint = Paint()        line_paint.strokeWIDth = 2f        line_paint.style = Paint.Style.stroke        line_paint.color = color.argb(188, 76, 126, 245)        line_paint.textSize=38f        val buttonPath = Path()        buttonPath.addRoundRect(100f, -120f, 320f, -200f, 80f, 80f, Path.Direction.ccw)        canvas.drawPath(buttonPath,line_paint)        canvas.save()        canvas.scale(1f,-1f)        canvas.drawText("前 七 天",140f,175f,line_paint)        canvas.restore()        canvas.save()        canvas.translate(260f,0f)        canvas.drawPath(buttonPath,line_paint)        canvas.scale(1f,-1f)        canvas.drawText("后 七 天",140f,175f,line_paint)        canvas.restore()    }

效果如下:

3.美化技能之贴图

贴图之前博客就用过了,这里不多讲了。加个裁剪就完事了。

  //绘制头像在顶部    private fun drawheaderToCanvas(canvas: Canvas) {        val bitmap_paint = Paint()        bitmap_paint.strokeWIDth = 2f        bitmap_paint.style = Paint.Style.stroke        bitmap_paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)        bitmap_paint.isAntiAlias =true        canvas.save()        val srcRect1=Rect(0, 0, 80, 80)        val dstRect1=Rect(0, 0, 40, 40)        val danweiY = (grID_wIDth - 40) / 500        for (index in 0 until dataList.size) {            val mdrawable = ContextCompat.getDrawable(context, imgList[index])            val bitmap = getBitmap(bitmap_paint, mdrawable!!)            canvas.save()            canvas.translate(                grID_wIDth * index - bitmap.wIDth / 4,                danweiY * dataList[index] + 20            )            //这里绘制图片到画布上            val circlePath = Path()            circlePath.addCircle(20f,20f, 20f, Path.Direction.ccw)            canvas.clipPath(circlePath)            canvas.drawBitmap(bitmap, srcRect1, dstRect1, bitmap_paint)            canvas.restore()        }        canvas.restore()    }

到这里我想已经达到UI设计图了吧。我们多加了个图片。文字这么简单的东西我就不啰嗦了。

4.强迫症

为了达到一致效果,自己强迫症没法子,绘制完最终效果。那些写死的文字和距离不是一个专业开发人员的素质,希望你们写的时候自己多测量多计算。我这里为了速度都是写死的数据。切勿模仿。

 private fun drawtopTextToCanvas(canvas: Canvas) {        val text_paint = Paint()        text_paint.strokeWIDth = 2f        text_paint.style = Paint.Style.FILL        text_paint.color = color.argb(255, 0, 0, 0)        text_paint.textSize =66f        val rectText = Rect()        val rectTextYuan = Rect()        canvas.save()        canvas.scale(1f, -1f)        canvas.translate((wIDth/2).tofloat()-100,-500f)        val text="1347"        val textyu="元"        text_paint.getTextBounds(text, 0,text.length, rectText)        canvas.drawText(            text,            -rectText.wIDth().tofloat() - 42f,            rectText.height().tofloat() / 2,            text_paint        )        text_paint.color = color.argb(111, 111, 111, 111)        text_paint.getTextBounds(textyu, 0,textyu.length, rectTextYuan)        text_paint.textSize =33f        canvas.drawText(            textyu,             80+ -rectTextYuan.wIDth().tofloat() - 42f,            rectTextYuan.height().tofloat() / 2,            text_paint        )        canvas.translate(0f,50f)        canvas.drawText(            "较前天",            -rectTextYuan.wIDth().tofloat() - 180f,            rectTextYuan.height().tofloat() / 2,            text_paint        )        canvas.translate(100f,0f)        text_paint.color = color.argb(255, 223, 129, 120)        canvas.drawText(            "+971.99(251.19%)",            -rectTextYuan.wIDth().tofloat() - 180f,            rectTextYuan.height().tofloat() / 2,            text_paint        )        canvas.translate(-100f,50f)        text_paint.color = color.argb(111, 111, 111, 111)        canvas.drawText(            "对应图中虚线部分进行最高评奖",            -rectTextYuan.wIDth().tofloat() - 180f,            rectTextYuan.height().tofloat() / 2,            text_paint        )        //暂时没找到canvas绘制富文本的方法。只能一个个测量绘制文字了。别学我,好好测量测量有待提高自己的小学计算。//        val textSpanned1 =  SpannableString("Hello World");//        textSpanned1.setSpan(ForegroundcolorSpan(color.RED), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)//        text_paint.reset()//        text_paint.textSize=44f//        canvas.drawText(textSpanned1,0,10,0f,0f,text_paint)        canvas.restore()    }
7.美化技能之手势动画

如果不明白手势滑动缩放动画的多看看之前的几篇文章,这个作业留给你们

五、曲线-游戏

明天再写吧睡觉

总结

以上是内存溢出为你收集整理的Android自定义-曲线利用和认识全部内容,希望文章能够帮你解决Android自定义-曲线利用和认识所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1048158.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存