可点击价格走势图-贝塞尔曲线

记录下项目中使用贝塞尔曲线实现可点击查看价格功能


1. XML 使用方式


    android:id="@+id/customCurveChart"

    android:layout_width="match_parent"

    android:layout_height="187dp"

    android:layout_below="@+id/online_tv_carve"

    android:layout_marginLeft="16dp"

    android:layout_marginRight="6dp"

    android:layout_marginTop="20dp"

    android:gravity="center"

    android:orientation="vertical"

    android:paddingBottom="10dp"

    >



Activity:


private void initCurveChart2() {

List listY =new ArrayList<>();

    int number= BubbleSort2(listPrice);

        listY.add("0");

        listY.add("2.1w");

        listY.add("4.2w");

        listY.add("6.3w");

        listY.add("8.4w");

        listY.add("10.5w");

        listY.add("12.6w");

List color =new ArrayList<>();

    color.add(R.color.make_money_tv_color_red);

    customCurveChart2.addView(new CustomCurveChart(this, listRiqi, listY, listPrice, color, true));

}



自定义工具类:


package cn.ihk.www.fww.OnLineAssessment;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.CornerPathEffect;

import android.graphics.DashPathEffect;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.PathEffect;

import android.graphics.RectF;

import android.os.Build;

import android.support.annotation.RequiresApi;

import android.support.v4.content.ContextCompat;

import android.text.Layout;

import android.text.StaticLayout;

import android.text.TextPaint;

import android.view.MotionEvent;

import android.view.View;

import java.math.BigDecimal;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import cn.ihk.www.fww.R;

/**

* 2017年4月24日11:47:25

* zhangchengbo

*

* 总是改不完的需求,开始说OK,后面又说不行,现在又搞事情!我呸~

* 我有有一句话不知当讲不当讲  M M P

*/

public class CustomCurveChartextends View {

Map>mapXY =new HashMap>();

    ListmaplistX =new ArrayList();

    ListmaplistY =new ArrayList();

    // 坐标单位

    private ListxLabel;

    private ListyLabel;

    // 曲线数据

    private ListdataList;

    private ListcolorList;

    private boolean showValue;

    // 默认边距

    private int margin =45;

    // 距离左边偏移量

    private int marginX =45;

    // 原点坐标

    private int xPoint;

    private int yPoint;

    // X,Y轴的单位长度

    private int xScale;

    private int yScale;

    // 画笔

    private PaintpaintAxes;

    private PaintpaintCoordinate;

    private PaintpaintTable;

    private PaintpaintCurve;

    private PaintpaintRectF;

    private Paintpaint,paint2,paint3;

    private ListliststrY;

    private ListliststrX;

    private int isFalse;

    public CustomCurveChart(Context context, List xLabel, List yLabel,

                            List dataList, List colorList, boolean showValue) {

super(context);

        this.xLabel = xLabel;

        this.yLabel = yLabel;

        this.dataList = dataList;

        this.colorList = colorList;

        this.showValue = showValue;

    }

public CustomCurveChart(Context context) {

super(context);

    }

/**

* 初始化数据值和画笔

*/

    public void init() {

xPoint =margin +marginX;

        yPoint =this.getHeight() -margin;

        xScale = (this.getWidth() -2 *margin -marginX) / (xLabel.size() -1)-5;

        yScale = (this.getHeight() -2 *margin) / (yLabel.size() -1);

        //X与Y轴的绘制(颜色线条)

        paintAxes =new Paint();

        paintAxes.setStyle(Paint.Style.STROKE);

        paintAxes.setAntiAlias(true);

        paintAxes.setDither(true);

        paintAxes.setColor(ContextCompat.getColor(getContext(), R.color.curve_line));

        paintAxes.setStrokeWidth(5);

        paintAxes.setPathEffect(new DashPathEffect(new float[] {10, 10}, 1f));

        //XY 轴 的字体

        paintCoordinate =new Paint();

        paintCoordinate.setStyle(Paint.Style.STROKE);

        paintCoordinate.setDither(true);

        paintCoordinate.setAntiAlias(true);

        paintCoordinate.setColor(ContextCompat.getColor(getContext(), R.color.color_gray));

        paintCoordinate.setTextSize(30);

        //网格线条的绘制与颜色

        paintTable =new Paint();

        paintTable.setStyle(Paint.Style.STROKE);

        paintTable.setAntiAlias(true);

        paintTable.setDither(true);

        paintTable.setColor(ContextCompat.getColor(getContext(), R.color.curve_line));

        paintTable.setStrokeWidth(3);

        //绘制曲线

        paintCurve =new Paint();

        paintCurve.setStyle(Paint.Style.STROKE);

        paintCurve.setDither(true);

        paintCurve.setAntiAlias(true);

        paintCurve.setStrokeWidth(5);

        PathEffect pathEffect =new CornerPathEffect(5);

        paintCurve.setPathEffect(pathEffect);

        //绘制直线

        paintRectF =new Paint();

        paintRectF.setStyle(Paint.Style.FILL);

        paintRectF.setDither(true);

        paintRectF.setAntiAlias(true);

        paintRectF.setStrokeWidth(3);

        // 定义画笔圆圈

        paint =new Paint();

        paint.setStyle(Paint.Style.FILL);

        // 消除锯齿

        paint.setAntiAlias(true);

        // 设置画笔的颜色

        paint.setColor(Color.RED);

        // 设置paint的外框宽度

        paint.setStrokeWidth(2);

        paint2 =new Paint();

        paint2.setStyle(Paint.Style.FILL);

        paint2.setAntiAlias(true);

        paint2.setColor(Color.RED);

        paint2.setStrokeWidth(2);

        paint3 =new Paint();

        paint3.setStyle(Paint.Style.FILL);

        paint3.setAntiAlias(true);

        paint3.setColor(Color.WHITE);

    }

@RequiresApi(api = Build.VERSION_CODES.M)

@Override

    protected void onDraw(Canvas canvas) {

canvas.drawColor(ContextCompat.getColor(getContext(), R.color.color1));

        init();

        drawTable(canvas, paintTable);

        drawAxesLine(canvas, paintAxes);

        drawCoordinate(canvas, paintCoordinate);

        //逻辑有点乱,瞎写就瞎看吧,改来改去,BB来BB去。M M P

// 就这点破B玩意,不断改,操你大爷

        for (int i =0; i

if (showValue) {

drawCurve(canvas, paintCurve, paint, paint2, dataList, colorList.get(0));

                drawZero(canvas,dataList, paint);

                drawPrice(canvas,dataList);

            }

switch (isFalse){//滴滴万 后台控制 最多显示6个,有问题直接找他。

                case 6:

//                    drawLine(canvas,dataList,colorList.get(0));//为了重新绘制曲线图,但是X Y 坐标发生变化。暂时就这样吧

                    drawCurve(canvas,paint3, paint2, dataList);

                    drawCurve0(canvas, paint,paint2, dataList);

                    drawZeroForThree(canvas,dataList, paint);

break;

                case 1:

drawCurve(canvas,paint3, paint2, dataList);

                    drawCurve1(canvas,paint, paint2, dataList);

                    drawZeroForThree(canvas,dataList, paint);

break;

                case 2:

drawCurve(canvas,paint3, paint2, dataList);

                    drawCurve2(canvas,paint, paint2, dataList);

                    drawZeroForThree(canvas,dataList, paint);

break;

                case 3:

drawCurve3(canvas,paint, paint2, dataList);

                    drawZeroForThree(canvas,dataList, paint);

break;

                case 4:

drawCurve(canvas,paint3, paint2, dataList);

                    drawCurve4(canvas,paint, paint2, dataList);

                    drawZeroForThree(canvas,dataList, paint);

break;

                case 5:

drawCurve(canvas,paint3, paint2, dataList);

                    drawCurve5(canvas,paint, paint2, dataList);

                    drawZeroForThree(canvas,dataList, paint);

break;

                case 7:

drawCurve(canvas,paint3, paint2, dataList);

                    drawCurve6(canvas,paint, paint2, dataList);

                    drawZeroForThree(canvas,dataList, paint);

break;

            }

}

}

private void drawPrice(Canvas canvas,List data) {

//节点价格

        for (int i =3; i <=3; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale -65;

            r2.top = toY(data.get(i)) -75;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -15;

            canvas.drawRoundRect(r2, 12, 12, paint2);//带圆角

            paint2.setColor(Color.parseColor("#474E5E"));// 设置灰色

            TextPaint textPaint =new TextPaint();

            textPaint.setColor(Color.parseColor("#ffffff"));

            textPaint.setTextSize(20.0F);

            textPaint.setAntiAlias(true);

            String aboutTheGame ="参考均价\n" + data.get(i) +"元/m²";

            //StaticLayout 并不是一个 View 或者 ViewGroup ,而是 android.text.Layout 的子类,它是纯粹用来绘制文字的。

// StaticLayout 支持换行,它既可以为文字设置宽度上限来让文字自动换行,也会在 \n 处主动换行

            StaticLayout layout =new StaticLayout(aboutTheGame, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            // 这里的参数110,表示字符串的长度,当满足一定数值,就会换行,也可以使用“\r\n”来实现换行

            canvas.save();

            canvas.translate(xPoint + i *xScale -55, toY(data.get(i)) -70);//从坐标 X,Y 点开始画

            layout.draw(canvas);

            canvas.restore();//restore与save组合使用

        }

}

private void drawZeroForThree(Canvas canvas,List data,Paint paint2) {

paint2.setColor(Color.parseColor("#FF4F51"));

        // 画一个圆

        for (int i =0; i <=3; i++) {

canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

        }

}

private void drawZero(Canvas canvas,List data,Paint paint2) {

paint2.setColor(Color.parseColor("#FF4F51"));

        // 画一个圆

        for (int i =0; i <= (xLabel.size() -1); i++) {

canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

            String str = String.valueOf(toY(data.get(i)));

            maplistX.add(String.valueOf(xPoint + i *xScale));

            maplistY.add(str);

            mapXY.put("xxx", maplistX);

            mapXY.put("yyy", maplistY);

        }

}

/**

* 绘制坐标轴

*/

    private void drawAxesLine(Canvas canvas, Paint paint) {

// X

//        paint.setPathEffect(new DashPathEffect(new float[] {8f, 8f}, 0f));

//        canvas.drawLine(xPoint, yPoint, this.getWidth() - margin / 6, yPoint, paint);

        Path path =new Path();

        path.moveTo(xPoint, yPoint);

        path.lineTo(this.getWidth() -margin /6, yPoint);

        paint.setPathEffect(new DashPathEffect(new float[] {8f, 8f}, 0f));

        canvas.drawPath(path, paint);

        canvas.drawLine(this.getWidth() -margin /6, yPoint, this.getWidth() -margin /2, yPoint -margin /3, paint);

        canvas.drawLine(this.getWidth() -margin /6, yPoint, this.getWidth() -margin /2, yPoint +margin /3, paint);

        // Y

//        canvas.drawLine(xPoint, yPoint, xPoint, margin / 6, paint);

//        canvas.drawLine(xPoint, margin / 6, xPoint - margin / 3, margin / 2, paint);

//        canvas.drawLine(xPoint, margin / 6, xPoint + margin / 3, margin / 2, paint);

    }

/**

* 绘制表格

*/

    private void drawTable(Canvas canvas, Paint paint) {

Path path =new Path();

        path.addCircle(0, 0, 3, Path.Direction.CCW);

        // 横向线

        for (int i =1; (yPoint - i *yScale) >=margin; i++) {

int startX =xPoint;

            int startY =yPoint - i *yScale;

            int stopX =xPoint + (xLabel.size() -1) *xScale;

            path.moveTo(startX, startY);

            path.lineTo(stopX, startY);

            paint.setPathEffect(new DashPathEffect(new float[] {8f, 8f}, 0f));

            canvas.drawPath(path, paint);

        }

// 纵向线

//        for (int i = 1; i * xScale <= (this.getWidth() - margin); i++) {

//            int startX = xPoint + i * xScale;

//            int startY = yPoint;

//            int stopY = yPoint - (yLabel.size() - 1) * yScale;

//            path.moveTo(startX, startY);

//            path.lineTo(startX, stopY);

//            canvas.drawPath(path, paint);

//        }

    }

/**

* 绘制刻度

*/

    private void drawCoordinate(Canvas canvas, Paint paint) {

// X轴坐标

        for (int i =0; i <= (xLabel.size() -1); i++) {

paint.setTextAlign(Paint.Align.CENTER);

            int startX =xPoint + i *xScale;

            canvas.drawText(xLabel.get(i), startX, this.getHeight() -margin /6, paint);

        }

// Y轴坐标

        for (int i =0; i <= (yLabel.size() -1); i++) {

paint.setTextAlign(Paint.Align.LEFT);

            int startY =yPoint - i *yScale;

            int offsetX;

            switch (yLabel.get(i).length()) {

case 1:

offsetX =28;

break;

                case 2:

offsetX =20;

break;

                case 3:

offsetX =12;

break;

                case 4:

offsetX =5;

break;

                default:

offsetX =0;

break;

            }

int offsetY;

            if (i ==0) {

offsetY =0;

            }else {

offsetY =margin /5;

            }

// x默认是字符串的左边在屏幕的位置,y默认是字符串是字符串的baseline在屏幕上的位置

            canvas.drawText(yLabel.get(i), margin /4 + offsetX, startY + offsetY, paint);

        }

}

/**

* 绘制曲线

* paint代表绘制曲线

* paint2代表绘制价格以及背景色

* paint3代表绘制价格节点的实心圆

*/

    private void drawCurve(Canvas canvas, Paint paint, Paint paint2, Paint paint3, List data, int color) {

paint.setColor(ContextCompat.getColor(getContext(), color));

        Path path =new Path();

        for (int i =0; i

if (i ==0) {

path.moveTo(xPoint, toY(data.get(i)));

            }else {

path.quadTo((xPoint + i *xScale)-xPoint,toY(data.get(i))-10,xPoint + i *xScale, toY(data.get(i)));

            }

if (i ==xLabel.size() -1) {

path.lineTo(xPoint + i *xScale, toY(data.get(i)));

            }

}

// 画一个圆

        for (int i =0; i <= (xLabel.size() -1); i++) {

canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint3);

            String str = String.valueOf(toY(data.get(i)));

            maplistX.add(String.valueOf(xPoint + i *xScale));

            maplistY.add(str);

            mapXY.put("xxx", maplistX);

            mapXY.put("yyy", maplistY);

        }

//节点价格

        for (int i =3; i <=3; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale -65;

            r2.top = toY(data.get(i)) -75;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -15;

            canvas.drawRoundRect(r2, 12, 12, paint2);//带圆角

            paint2.setColor(Color.parseColor("#474E5E"));// 设置灰色

            TextPaint textPaint =new TextPaint();

            textPaint.setColor(Color.parseColor("#ffffff"));

            textPaint.setTextSize(20.0F);

            textPaint.setAntiAlias(true);

            String aboutTheGame ="参考均价\n" + data.get(i) +"元/m²";

            //StaticLayout 并不是一个 View 或者 ViewGroup ,而是 android.text.Layout 的子类,它是纯粹用来绘制文字的。

// StaticLayout 支持换行,它既可以为文字设置宽度上限来让文字自动换行,也会在 \n 处主动换行

            StaticLayout layout =new StaticLayout(aboutTheGame, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            // 这里的参数110,表示字符串的长度,当满110时,就会换行,也可以使用“\r\n”来实现换行

            canvas.save();

            canvas.translate(xPoint + i *xScale -55, toY(data.get(i)) -70);//从坐标 X,Y 点开始画

            layout.draw(canvas);

            canvas.restore();//restore与save组合使用

        }

canvas.drawPath(path, paint);

    }

@Override

    public boolean onTouchEvent(MotionEvent event) {

String numX ="";

        String numY ="";

        Canvas canvas =new Canvas();

        canvas.drawColor(ContextCompat.getColor(getContext(), R.color.make_money_tv_color_red));

        switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

int i;

                for (i =0; i <=xLabel.size() -1; i++) {//(小红点)

                    liststrX =mapXY.get("xxx");

                    liststrY =mapXY.get("yyy");

                    numX =liststrX.get(i);

                    numY =liststrY.get(i);

                    BigDecimal bd =new BigDecimal(numX);

                    float f1 = bd.setScale(0, BigDecimal.ROUND_HALF_UP).floatValue();

                    BigDecimal bd2 =new BigDecimal(numY);

                    float f2 = bd2.setScale(0, BigDecimal.ROUND_HALF_UP).floatValue();

                    //点击位置x坐标与圆心的x坐标的距离

                    int distanceX = (int) Math.abs(f1 - event.getX());

                    //点击位置y坐标与圆心的y坐标的距离

                    int distanceY = (int) Math.abs(f2 - event.getY());

                    //点击位置与圆心的直线距离

                    int distanceZ = (int) Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));

//                  LogUtil.i("ceshichart","点击位置x坐标与圆心的x坐标的距离=======================================>"+distanceX);

//                  LogUtil.i("ceshichart","点击位置y坐标与圆心的y坐标的距离=======================================>"+distanceY);

//                    LogUtil.i("ceshichart","点击位置与圆心的直线距离=======================================>"+distanceZ);

                    if (distanceZ >40) {

//                        Snackbar.make(getRootView(), "没在圆内", Snackbar.LENGTH_INDEFINITE).show();

//                        return false;

                    }else {

//                        LogUtil.i("ceshiCurveChart","iiiiiiiiiiiiiii==============================>"+i);

                        if(i==1){

isFalse=1;

                        }else if(i==2){

isFalse=2;

                        }else if(i==3){

isFalse=3;

                        }else if(i==4){

isFalse=4;

                        }else if(i==5){

isFalse=5;

                        }else if(i==0){

isFalse=6;

                        }else if(i==6){

isFalse=7;

                        }else if(i==7){

isFalse=8;

                        }else if(i==8){

isFalse=9;

                        }else if(i==9){

isFalse=10;

                        }else if(i==10){

isFalse=11;

                        }

//                        Snackbar.make(getRootView(),"当前坐标为:"+i+"屏幕坐标值:"+ event.getX() + "" + event.getY(), Snackbar.LENGTH_INDEFINITE).show();

                        break;

                    }

}

break;

        }

invalidate();

//        return super.onTouchEvent(event);

        return  true;

    }

private void drawCurve(Canvas canvas,Paint paint, Paint paint2, List data) {

for (int i =3; i <=3; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale-65;

            r2.top = toY(data.get(i)) -78;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -12;

            canvas.drawRoundRect(r2, 1, 1, paint);//带圆角

            canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

        }

}

private void drawCurve6(Canvas canvas,Paint paint, Paint paint2, List data) {

paint.setColor(Color.parseColor("#474E5E"));

        paint2.setColor(Color.parseColor("#FF4F51"));

        for (int i =6; i <=6; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale-65;

            r2.top = toY(data.get(i)) -75;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -15;

            canvas.drawRoundRect(r2, 12, 12, paint);//带圆角

            paint.setColor(Color.parseColor("#474E5E"));// 设置灰色

            TextPaint textPaint =new TextPaint();

            textPaint.setColor(Color.parseColor("#ffffff"));

            textPaint.setTextSize(20.0F);

            textPaint.setAntiAlias(true);

            String aboutTheGame ="参考均价\n" + data.get(i) +"元/m²";

            StaticLayout layout =new StaticLayout(aboutTheGame, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            // 这里的参数110,表示字符串的长度,当满110时,就会换行,也可以使用“\r\n”来实现换行

            canvas.save();

            canvas.translate(xPoint + i *xScale -55, toY(data.get(i)) -70);//从坐标 X,Y 点开始画

            layout.draw(canvas);

            canvas.restore();//restore与save组合使用

            canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

        }

}

private void drawCurve5(Canvas canvas,Paint paint, Paint paint2, List data) {

paint.setColor(Color.parseColor("#474E5E"));

        paint2.setColor(Color.parseColor("#FF4F51"));

        for (int i =5; i <=5; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale-65;

            r2.top = toY(data.get(i)) -75;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -15;

            canvas.drawRoundRect(r2, 12, 12, paint);//带圆角

            paint.setColor(Color.parseColor("#474E5E"));// 设置灰色

            TextPaint textPaint =new TextPaint();

            textPaint.setColor(Color.parseColor("#ffffff"));

            textPaint.setTextSize(20.0F);

            textPaint.setAntiAlias(true);

            String aboutTheGame ="参考均价\n" + data.get(i) +"元/m²";

            StaticLayout layout =new StaticLayout(aboutTheGame, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            // 这里的参数110,表示字符串的长度,当满110时,就会换行,也可以使用“\r\n”来实现换行

            canvas.save();

            canvas.translate(xPoint + i *xScale -55, toY(data.get(i)) -70);//从坐标 X,Y 点开始画

            layout.draw(canvas);

            canvas.restore();//restore与save组合使用

            canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

        }

}

private void drawCurve4(Canvas canvas,Paint paint, Paint paint2, List data) {

paint.setColor(Color.parseColor("#474E5E"));

        paint2.setColor(Color.parseColor("#FF4F51"));

        for (int i =4; i <=4; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale-65;

            r2.top = toY(data.get(i)) -75;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -15;

            canvas.drawRoundRect(r2, 12, 12, paint);//带圆角

            paint.setColor(Color.parseColor("#474E5E"));// 设置灰色

            TextPaint textPaint =new TextPaint();

            textPaint.setColor(Color.parseColor("#ffffff"));

            textPaint.setTextSize(20.0F);

            textPaint.setAntiAlias(true);

            String aboutTheGame ="参考均价\n" + data.get(i) +"元/m²";

            StaticLayout layout =new StaticLayout(aboutTheGame, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            // 这里的参数110,表示字符串的长度,当满110时,就会换行,也可以使用“\r\n”来实现换行

            canvas.save();

            canvas.translate(xPoint + i *xScale -55, toY(data.get(i)) -70);//从坐标 X,Y 点开始画

            layout.draw(canvas);

            canvas.restore();//restore与save组合使用

            canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

        }

}

private void drawCurve3(Canvas canvas,Paint paint, Paint paint2, List data) {

paint.setColor(Color.parseColor("#474E5E"));

        paint2.setColor(Color.parseColor("#FF4F51"));

        for (int i =3; i <=3; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale-65;

            r2.top = toY(data.get(i)) -75;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -15;

            canvas.drawRoundRect(r2, 12, 12, paint);//带圆角

            paint.setColor(Color.parseColor("#474E5E"));// 设置灰色

            TextPaint textPaint =new TextPaint();

            textPaint.setColor(Color.parseColor("#ffffff"));

            textPaint.setTextSize(20.0F);

            textPaint.setAntiAlias(true);

            String aboutTheGame ="参考均价\n" + data.get(i) +"元/m²";

            StaticLayout layout =new StaticLayout(aboutTheGame, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            // 这里的参数110,表示字符串的长度,当满110时,就会换行,也可以使用“\r\n”来实现换行

            canvas.save();

            canvas.translate(xPoint + i *xScale -55, toY(data.get(i)) -70);//从坐标 X,Y 点开始画

            layout.draw(canvas);

            canvas.restore();//restore与save组合使用

            canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

        }

}

private void drawCurve2(Canvas canvas,Paint paint, Paint paint2, List data) {

paint.setColor(Color.parseColor("#474E5E"));

        paint2.setColor(Color.parseColor("#FF4F51"));

        for (int i =2; i <=2; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale-65;

            r2.top = toY(data.get(i)) -75;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -15;

            canvas.drawRoundRect(r2, 12, 12, paint);//带圆角

            paint.setColor(Color.parseColor("#474E5E"));// 设置灰色

            TextPaint textPaint =new TextPaint();

            textPaint.setColor(Color.parseColor("#ffffff"));

            textPaint.setTextSize(20.0F);

            textPaint.setAntiAlias(true);

            String aboutTheGame ="参考均价\n" + data.get(i) +"元/m²";

            StaticLayout layout =new StaticLayout(aboutTheGame, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            // 这里的参数110,表示字符串的长度,当满110时,就会换行,也可以使用“\r\n”来实现换行

            canvas.save();

            canvas.translate(xPoint + i *xScale -55, toY(data.get(i)) -70);//从坐标 X,Y 点开始画

            layout.draw(canvas);

            canvas.restore();//restore与save组合使用

            canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

        }

}

private void drawCurve1(Canvas canvas, Paint paint,Paint paint2,List data) {

paint.setColor(Color.parseColor("#474E5E"));

        paint2.setColor(Color.parseColor("#FF4F51"));

        for (int i =1; i <=1; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale-65;

            r2.top = toY(data.get(i)) -75;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -15;

            canvas.drawRoundRect(r2, 12, 12, paint);//带圆角

            paint.setColor(Color.parseColor("#474E5E"));// 设置灰色

            TextPaint textPaint =new TextPaint();

            textPaint.setColor(Color.parseColor("#ffffff"));

            textPaint.setTextSize(20.0F);

            textPaint.setAntiAlias(true);

            String aboutTheGame ="参考均价\n" + data.get(i) +"元/m²";

            StaticLayout layout =new StaticLayout(aboutTheGame, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            // 这里的参数110,表示字符串的长度,当满110时,就会换行,也可以使用“\r\n”来实现换行

            canvas.save();

            canvas.translate(xPoint + i *xScale -55, toY(data.get(i)) -70);//从坐标 X,Y 点开始画

            layout.draw(canvas);

            canvas.restore();//restore与save组合使用

            canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

        }

}

private void drawCurve0(Canvas canvas,Paint paint, Paint paint2, List data) {

paint.setColor(Color.parseColor("#474E5E"));

        paint2.setColor(Color.parseColor("#FF4F51"));

        for (int i =0; i <=0; i++) {

RectF r2 =new RectF();

            r2.left =xPoint + i *xScale-65;

            r2.top = toY(data.get(i)) -75;

            r2.right =xPoint + i *xScale +70;

            r2.bottom = toY(data.get(i)) -15;

            canvas.drawRoundRect(r2, 12, 12, paint);//带圆角

//            paint.setColor(Color.parseColor("#474E5E"));// 设置灰色

            TextPaint textPaint =new TextPaint();

            textPaint.setColor(Color.parseColor("#ffffff"));

            textPaint.setTextSize(20.0F);

            textPaint.setAntiAlias(true);

            String aboutTheGame ="参考均价\n" + data.get(i) +"元/m²";

            StaticLayout layout =new StaticLayout(aboutTheGame, textPaint, 300, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            // 这里的参数110,表示字符串的长度,当满110时,就会换行,也可以使用“\r\n”来实现换行

            canvas.save();

            canvas.translate(xPoint + i *xScale -55, toY(data.get(i)) -70);//从坐标 X,Y 点开始画

            layout.draw(canvas);

            canvas.restore();//restore与save组合使用

            canvas.drawCircle(xPoint + i *xScale, toY(data.get(i)), 10, paint2);

        }

}

private void drawLine(Canvas canvas,List data,int color) {

paint.setColor(ContextCompat.getColor(getContext(), color));

        Path path =new Path();

        for (int i =0; i

if (i ==0) {

path.moveTo(xPoint, toY(data.get(i)));

            }else {

path.quadTo((xPoint + i *xScale)-xPoint,toY(data.get(i))-10,xPoint + i *xScale, toY(data.get(i)));

            }

if (i ==xLabel.size() -1) {

path.lineTo(xPoint + i *xScale, toY(data.get(i)));

            }

}

canvas.drawPath(path, paint);

    }

/**

* 数据按比例转坐标

*/

    public float toY(Integer num) {

float y;

        try {

float a = (float) Float.valueOf(num) /21000.0f;

            y =yPoint - a *yScale;

        }catch (Exception e) {

return 0;

        }

return y;

    }

}

你可能感兴趣的:(可点击价格走势图-贝塞尔曲线)