使用
SlbSyView slbView;slbView = (SlbSyView) findViewById(R.id.slbView);slbView.setHeights(new float[]{2.1112f, 2.2311f, 2.3001f, 2.1234f, 2.3421f, 2.0015f, 2.3256f});slbView.setVisibility(View.VISIBLE);<com.bcb.presentation.view.custom.SlbSyViewandroid:id="@+id/slbView"android:layout_width="wrap_content"android:layout_height="125dp"android:layout_below="@+id/title"android:visibility="invisible"/>
自定义View
package com.bcb.presentation.view.custom;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.RectF;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.view.View;import android.view.WindowManager;/*** 描述:生利宝收益* 作者:baicaibang* 时间:2016/8/15 09:34*/public class SlbSyView extends View {Context context;Paint paint;Path path;//用于绘制复杂的图形轮廓,比如折线,圆弧以及各种复杂图案int width, height;//表格的宽高int widthBlank, heightBlank;//表格距离左侧、顶部的空白距离static final int WIDTH_MAX = 6;//View的相对宽高(7个);WIDTH_MAX是不会变的float heightMax = 7.0f;//heightMax是根据需要随便改变;float[] heights = new float[WIDTH_MAX + 1];//高度值float heightOffset = 0.1f;//偏差,根据需要设置float y6Value;//最后一个坐标的值public SlbSyView(Context context) {this(context, null);}public SlbSyView(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;init();}private void init() {paint = new Paint();path = new Path();paint.setAntiAlias(true);paint.setDither(true);heightBlank = dp2px(20);widthBlank = dp2px(35);width = getScreenWidth(context) - widthBlank*2;height = dp2px(100);}public void setHeights(float[] heights) {if (heights.length != 7) throw new RuntimeException("长度不对");y6Value = heights[6];heightMax = getMaxValue(heights) - getMinValue(heights) + heightOffset * 2;//根据需要设置for (int i = 0; i < heights.length; i++) {this.heights[i] = getMaxValue(heights) + heightOffset - heights[i];//根据需要设置}invalidate();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(Color.WHITE);//1、画背景paint.setColor(0x1adb3838);//10%透明度paint.setStyle(Paint.Style.FILL);path.moveTo(widthBlank, height + heightBlank);//指定初始轮廓点,若没指定默认从(0,0)点开始for (int i = 0; i < heights.length; i++) {path.lineTo(widthBlank + width * i / WIDTH_MAX, //中间的点heightBlank + height * heights[i] / heightMax);}path.lineTo(widthBlank + width, heightBlank + height);//最后的一个点path.close(); // 回到初始点形成封闭的曲线canvas.drawPath(path, paint);//2、画基准线条。每条线都需要两个坐标(注意,是每两个值组成一个坐标)paint.setStrokeWidth(dp2px(0.5f));//设置画笔粗细,单位为像素paint.setColor(0xff999999);float[] points = new float[7 * 4];//坐标//画7条竖线for (int i = 0; i < points.length; i += 4) {points[i] = widthBlank + width * i / (4 * WIDTH_MAX);points[i + 1] = heightBlank;points[i + 2] = points[i];points[i + 3] = heightBlank + height;}canvas.drawLines(points, paint);//画2条横线canvas.drawLine(widthBlank, heightBlank, //widthBlank + width, heightBlank, paint);canvas.drawLine(widthBlank, heightBlank + height,//widthBlank + width, heightBlank + height, paint);//3、画利率线paint.setColor(0xffdb3838);paint.setStrokeWidth(dp2px(2));for (int i = 0; i < heights.length - 1; i++) {//注意遍历条件 heights.length - 1canvas.drawLine(widthBlank + width * i / WIDTH_MAX, //heightBlank + height * heights[i] / heightMax, //widthBlank + width * (i + 1) / WIDTH_MAX, //heightBlank + height * heights[i + 1] / heightMax, paint);}//4、画圆形float x6Point = widthBlank + width * 6 / WIDTH_MAX;float y6Point = heightBlank + height * heights[6] / heightMax;//画白色的实心圆圆paint.setStyle(Paint.Style.FILL);paint.setColor(Color.WHITE);canvas.drawCircle(x6Point, y6Point, dp2px(4), paint);//画空心圆paint.setStyle(Paint.Style.STROKE);paint.setColor(0xffdb3838);canvas.drawCircle(x6Point, y6Point, dp2px(4), paint);//圆心坐标,半径//5、画文本(今天的年利率)//画圆角矩形paint.setStyle(Paint.Style.FILL);RectF rect = new RectF(x6Point - dp2px(25), y6Point - dp2px(30), //左上角坐标x6Point + dp2px(25), y6Point - dp2px(10));//右下角坐标canvas.drawRoundRect(rect, dp2px(3), dp2px(3), paint);//两侧圆角弧度的大小//画文字paint.setColor(Color.WHITE);paint.setTextSize(dp2px(13));//单位是px,只在绘制文字时有效paint.setTextAlign(Paint.Align.CENTER);//绘制的文字以drawText时指定的 float x 水平居中,默认值是Align.LEFTcanvas.drawText(y6Value + "", rect.centerX(),//rect.centerY() + dp2px(4.5f), paint);//注意 float y 代表的是 baseline 的值,也即e和f的下边界,而非g的下边界}private int dp2px(float dpValue) {float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}/*** 获取屏幕宽*/public static int getScreenWidth(Context context) {DisplayMetrics metric = new DisplayMetrics();((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metric);return metric.widthPixels;}/*** 取最大值*/public static float getMaxValue(float[] a) {float temp = a[0];for (int i = 0; i < a.length; i++) {if (a[i] > temp) temp = a[i];}return temp;}/*** 取最小值*/public static float getMinValue(float[] a) {float temp = a[0];for (int i = 0; i < a.length; i++) {if (a[i] < temp) temp = a[i];}return temp;}}