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

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


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;

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,639评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,277评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,221评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,474评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,570评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,816评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,957评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,718评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,176评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,511评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,646评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,322评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,934评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,755评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,987评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,358评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,514评论 2 348

推荐阅读更多精彩内容