记录TV开发写过的自定义控件(1)

记录TV开发过程中使用的自定义控件(代码+图片)

网络测速仪表盘

效果图

网络测速效果图

代码


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * author : TiaoPi
 * date   : 2018/12/28  14:17
 * desc   : 网络测速仪表盘
 */
public class NetSpeedView extends View {

    private int width = 500;
    int strokeWidth = 60;
    float radius = width/2;

    private Paint roundPaint;
    private Paint linePaint;
    private Paint textPaint;
    private Paint arrowPaint;
    private Paint roundPaint2;

    private float speedAngle = 40;//角度
    float speedOld,speedNew;

    private int[] speeds = {0,1,2,4,8,10,20,50,90};

    int blueColor = Color.parseColor("#2A95E8");
    int blueTColor = Color.parseColor("#352A95E8");
    int greeColor = Color.parseColor("#26D0A9");
    int greeTColor = Color.parseColor("#3526D0A9");
    int textColor = Color.parseColor("#FFFFFF");

    public NetSpeedView(Context context) {
        super(context);
        initRoundPaint();
    }

    public NetSpeedView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initRoundPaint();
    }

    public NetSpeedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initRoundPaint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width,width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawRoundView(canvas);

    }

    //绘制刻度和圆形
    private void drawRoundView(Canvas canvas){
        //绘制一个圆形的背景

        RectF rectF=new RectF(strokeWidth/2, strokeWidth/2, width - strokeWidth/2, width - strokeWidth/2);
        roundPaint.setStrokeWidth(strokeWidth);
        roundPaint.setColor(greeTColor);
        canvas.drawArc(rectF, -30, 75, false, roundPaint);
        roundPaint.setColor(blueTColor);
        canvas.drawArc(rectF, 135, 195, false, roundPaint);

        roundPaint.setStrokeWidth(strokeWidth/3);
        RectF rectF2 = new RectF(strokeWidth/2, strokeWidth/2, width - strokeWidth/2, width - strokeWidth/2);
        roundPaint.setColor(greeColor);
        canvas.drawArc(rectF2, -30, 75, false, roundPaint);
        roundPaint.setColor(blueColor);
        canvas.drawArc(rectF2, 135, 195, false, roundPaint);

        drawLineView(canvas);
    }

    /**
     * 绘制刻度  这个地方比较尴尬  还没找到合适的方法( 让字正着)
     */
    private void drawLineView(Canvas canvas){
        linePaint.setColor(blueColor);

        float dst = strokeWidth/3;

        canvas.save();
        for (int i = 0; i < speeds.length; i++) {
            if (i >= 6) {
                linePaint.setColor(greeColor);
            }
            canvas.rotate(getDegrees(i), radius, radius);
            canvas.drawLine(dst, radius, 50, radius, linePaint);
        }
        canvas.restore();
        textPaint.setTextSize(22);
        canvas.drawText(speeds[0] + "", 110, radius + 90, textPaint);
        canvas.drawText(speeds[1] + "", 80, radius + 5, textPaint);
        canvas.drawText(speeds[2] + "", 110, radius - 75, textPaint);
        canvas.drawText(speeds[3] + "", 160, radius - 130, textPaint);
        canvas.drawText(speeds[4] + "", radius, radius - 160, textPaint);
        canvas.drawText(speeds[5] + "", width - 160, radius - 130, textPaint);
        canvas.drawText(speeds[6] + "", width - 110, radius - 75, textPaint);
        canvas.drawText(speeds[7] + "", width - 80, radius + 5, textPaint);
        canvas.drawText(speeds[8] + "", width - 110, radius + 90, textPaint);

        drawArrowView(canvas);
    }

    //绘制指针
    private void drawArrowView(Canvas canvas){

        canvas.save();
        canvas.rotate(speedAngle, radius, radius);
        canvas.drawLine(120, radius, radius + 30, radius, linePaint);
        canvas.restore();

        canvas.drawCircle(width/2,width/2,10,roundPaint2);

        textPaint.setTextSize(40);
        canvas.drawText(speedNew + " MB/S",width/2,width - 50,textPaint);
        textPaint.setTextSize(22);
        canvas.drawText("测量网速",width/2,width - 10,textPaint);
    }

    public void startRotate(final float speedOld,final float speedNew){
        invalidate();
        this.speedOld = speedOld;
        this.speedNew = speedNew;
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                super.applyTransformation(interpolatedTime, t);
                //旋转动画
                float Angle = getAngle(speedOld,speedNew);
//                Log.d("-----" , "----------" + Angle);
                speedAngle = Angle * interpolatedTime + getSpeedAngle(speedOld);
                invalidate();
            }
        };
        animation.setDuration(1000);//毫秒
        animation.setRepeatCount(0);
        animation.setStartOffset(600);

        startAnimation(animation);
    }

    private void initRoundPaint(){
        roundPaint = new Paint();
        roundPaint.setStrokeWidth(2.0f);
        roundPaint.setColor(Color.parseColor("#333333"));
        roundPaint.setStyle(Paint.Style.STROKE);
        roundPaint.setAntiAlias(true);

        roundPaint2 = new Paint();
        roundPaint2.setColor(blueColor);
        roundPaint2.setStyle(Paint.Style.FILL);

        initLinePaint();
        initTextPaint();
        initArrowPaint();
    }

    private void initArrowPaint(){
        arrowPaint = new Paint();
        arrowPaint.setStrokeWidth(2.0f);
        arrowPaint.setStyle(Paint.Style.FILL);
        arrowPaint.setAntiAlias(true);
    }

    private void initLinePaint(){
        linePaint = new Paint();
        linePaint.setStrokeWidth(4.0f);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setAntiAlias(true);
    }

    private void initTextPaint(){
        textPaint = new Paint();
        textPaint.setStrokeWidth(4.0f);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.parseColor("#AEAEAE"));
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(22);
        textPaint.setAntiAlias(true);
    }

    private int getDegrees(int pos){
        if (pos == 0) {
            return -30;
        }else{
            return 30;
        }
    }

    /**
     * 获取旋转的角度
     */
    private float getAngle(float speedOld,float speedNew){

//        Log.d("-----" , getArrowPos(speedOld) + "-----" + getArrowPos(speedNew));
//        Log.d("-----" , getSpeedAngle(speedOld) + "-----" + getSpeedAngle(speedNew));

        if (getArrowPos(speedOld) >= 7 && getArrowPos(speedNew) >= 7) {
            return getSpeedAngle(speedNew) - getSpeedAngle(speedOld);
        }else if (getArrowPos(speedOld) < 7 && getArrowPos(speedNew) >= 7){
            return ((180 - getSpeedAngle(speedOld)) + (180 + getSpeedAngle(speedNew)));
        }else if (getArrowPos(speedOld) >= 7 && getArrowPos(speedNew) < 7){
            return - ((180 + getSpeedAngle(speedOld)) + (180 - getSpeedAngle(speedNew)));
        }else if (getArrowPos(speedOld) < 7 && getArrowPos(speedNew) < 7){
            return  getSpeedAngle(speedNew) - getSpeedAngle(speedOld);
        }else {
            return 0;
        }

    }

    /**
     * 根据网速获取角度值
     */
    public float getSpeedAngle(float speed) {
        int arrowPos = getArrowPos(speed);
        if (arrowPos < 1){ //第一象限
            return (speed/1) * 30 - 30;
        }else if (arrowPos >= 1 && arrowPos < 7){//第二象限
            return ((speed - speeds[arrowPos]) / (speeds[arrowPos + 1] - speeds[arrowPos])) * 30 + 30 * (arrowPos - 1);
        }else if (arrowPos >= 7){
            return ((speed - speeds[arrowPos]) / (speeds[arrowPos + 1] - speeds[arrowPos])) * 30 - 180;
        }else {
            return 0;
        }
    }

    /**
     * 获取再第几象限
     * @param speed
     * @return
     */
    private int getArrowPos(float speed){
        if (speed >= 90){
            speed = 89;
        }
        int pos = 0;
        for (int i = 0; i < speeds.length - 1; i++) {

            if (speed >= speeds[i] && speed < speeds[i+1]){
                pos = i;
            }
        }
        return pos;
    }

}

中医五运六气柱状图

效果图

五运六气

代码


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * author : TiaoPi
 * date   : 2019/5/17  11:30
 * desc   : 中医五运六气
 */
public class FiveSixView extends View {

    private int width = 1100;
    private int height = 800;
    private int feWidth = 200;

    private int textColor1 = Color.parseColor("#FFFFFF");
    private int textColor2 = Color.parseColor("#FF999999");

    private int redColor1 = Color.parseColor("#FFFF457F");
    private int redColor2 = Color.parseColor("#FFF8454B");
    private int blueColor1 = Color.parseColor("#FF5581F1");
    private int blueColor2 = Color.parseColor("#FF1153FC");

    private String[] titleTop = {"肝","心","脾","肺","肾"};
    private String[] titlesBottom = {"胆","小肠","胃","大肠","膀胱"};

    private int[] valueTop = {0,0,0,0,0};
    private int[] valueBottom = {0,0,0,0,0};

    private Paint paint;

    public void setValue(int[] valueTop,int[] valueBottom){
        this.valueTop = valueTop;
        this.valueBottom = valueBottom;
        invalidate();
    }

    public FiveSixView(Context context) {
        super(context);
        paint = new Paint();
        setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
    }

    public FiveSixView(Context context,  @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
    }

    public FiveSixView(Context context,  @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        onDrawCenterLine(canvas);
        initValueLines(canvas);
    }

    private void onDrawCenterLine(Canvas canvas){
        //绘制中线
        initCenterLinePaint();
        canvas.drawLine(0,height/2,900,height/2,paint);

        //绘制上下的文字
        initTextPaint(36,textColor1);
        int textStart = 50;

        int valueTopSum = 0;
        int valueBottomSum = 0;

        for (int i = 0; i < titleTop.length; i++) {
            canvas.drawText(String.valueOf(valueTop[i]),textStart + (i * feWidth),40,paint);
            canvas.drawText(String.valueOf(valueBottom[i]),textStart + (i * feWidth),height - 20,paint);

            valueTopSum = valueTopSum + valueTop[i];
            valueBottomSum = valueBottomSum + valueBottom[i];
        }

        //绘制灰色的文字
        initTextPaint(30,textColor2);
        textStart = 50;
        for (int i = 0; i < titleTop.length; i++) {
            canvas.drawText(titleTop[i],textStart + (i * feWidth),90,paint);
            canvas.drawText(titlesBottom[i],textStart + (i * feWidth),height - 70,paint);
        }

        //计算总的
        initTextPaint(48,blueColor2);
        canvas.drawText(String.valueOf(valueTopSum),1000, 60,paint);
        initTextPaint(48,redColor2);
        canvas.drawText(String.valueOf(valueBottomSum),1000,height - 40,paint);
        initTextPaint(48,textColor1);
        canvas.drawText(String.valueOf(valueBottomSum + valueTopSum),1000,height/2 + 15,paint);

    }

    /**
     * 绘制进度条
     */
    private void initValueLines(Canvas canvas){

        int lineStart = 40;
        int lineWidth = 20;

        //绘制进度
        int colorArr1[] = {redColor1,redColor2};
        int colorArr2[] = {blueColor2,blueColor1};

        float colorArrFloat[] =  {0,0.5f};

        LinearGradient linearGradient = new LinearGradient(
                0, 0, 0, height/2,
                colorArr2,colorArrFloat, Shader.TileMode.MIRROR);

        paint.setShader(linearGradient);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);

        for (int i = 0; i < valueTop.length; i++) {
            int Start =  lineStart +  i * feWidth;

            if (valueTop[i] == 1){
                RectF rectF = new RectF();
                rectF.set(Start,height/2 - lineWidth/2,Start + lineWidth,height/2 + lineWidth/2);
                canvas.drawArc(rectF,180,180,false,paint);
            }else if (valueTop[i] == 0){

            }else {
                RectF rectF = new RectF();
                rectF.set(Start,height/2 - ((height/2 - 120)/25  * valueTop[i]) ,Start + lineWidth,height/2);
                canvas.drawRoundRect(rectF,lineWidth/2,lineWidth/2,paint);

                RectF rectF2 = new RectF();
                rectF2.set(Start,height/2 -  lineWidth/2,Start + lineWidth,height/2);
                canvas.drawRoundRect(rectF2,0,lineWidth/2,paint);
            }

        }

        LinearGradient linearGradient2 = new LinearGradient(
                0,height/2,0,height,
                colorArr1,colorArrFloat, Shader.TileMode.MIRROR);
        paint.setShader(linearGradient2);

        for (int i = 0; i < valueBottom.length; i++) {
            int Start =  lineStart +  i * feWidth;

            if (valueBottom[i] == 1){
                RectF rectF = new RectF();
                rectF.set(Start,height/2 - lineWidth/2,Start + lineWidth,height/2 + lineWidth/2);
                canvas.drawArc(rectF,0,180,false,paint);
            }else if (valueBottom[i] == 0){

            }else {
                RectF rectF = new RectF();
                rectF.set(Start,height/2 ,Start + lineWidth,height/2 + ((height/2 - 120)/25  * valueBottom[i]));
                canvas.drawRoundRect(rectF,lineWidth/2,lineWidth/2,paint);

                RectF rectF2 = new RectF();
                rectF2.set(Start,height/2 ,Start + lineWidth,height/2 + lineWidth/2);
                canvas.drawRoundRect(rectF2,0,lineWidth/2,paint);
            }
        }

    }


    /**
     * 绘制文字
     * @param size
     * @param color
     */
    private void initTextPaint(int size,int color){
        paint.reset();
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(size);
        paint.setColor(color);
    }

    //初始化画笔
    private void initCenterLinePaint(){
        paint.reset();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1.0f);
        paint.setColor(textColor2);
        paint.setAntiAlias(true);
    }

}

血脂分布柱状图

效果图

血脂分布柱状图

代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.text.DecimalFormat;

/**
 * author : TiaoPi
 * date   : 2018/12/4  10:57
 * desc   : 血脂分布图
 */
public class BloodFatView extends View {

    private float TCNum = 4.8f;
    private float TGNum = 1f;
    private float HDL_GNum = 4f;
    private float LDL_GNum = 2.5f;

    private int width = 1600;
    private int leftWidth = 60;
    private int height = 500;
    private int bottomHeight = 150;

    float itemWidth = width / 5;
    float itemHeight = height / 12;

    private int textColor = Color.parseColor("#333333");
    private int redColor = Color.parseColor("#FF4C62");
    private int blueColor1 = Color.parseColor("#4065CBFE");
    private int blueColor2 = Color.parseColor("#2DB7FE");
    private int wColor = Color.parseColor("#FFFFFF");
    private int linesColor = Color.parseColor("#DDDDDD");
    private int[] strs = {7,6,5,4,3,2,1,0,-1,-2,-3};

    private Paint paint;
    private Paint paint2;


    public BloodFatView(Context context) {
        super(context);
        paint = new Paint();
        setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
    }

    public BloodFatView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
    }

    public BloodFatView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(leftWidth + width + 18,height +bottomHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initLinesChat(canvas);
        initLinesChat2(canvas);
        initBottomText(canvas);
        initChatNum(canvas);
    }

    /**
     * 绘制刻度
     */
    private void initChatNum(Canvas canvas){
        initChatPaint();
        RectF rectF1 = new RectF(
                leftWidth + itemWidth/2 - 20,8 * itemHeight,
                leftWidth + itemWidth/2 + 20,getYHeight(TCNum)
        );
        canvas.drawRect(rectF1,paint);

        RectF rectF2 = new RectF(
                leftWidth + itemWidth/2 - 20 + itemWidth,8 * itemHeight,
                leftWidth + itemWidth/2 + 20 + itemWidth,getYHeight(TGNum)
        );
        canvas.drawRect(rectF2,paint);

        RectF rectF3 = new RectF(
                leftWidth + itemWidth/2 - 20 + itemWidth * 2,8 * itemHeight,
                leftWidth + itemWidth/2 + 20 + itemWidth * 2,getYHeight(HDL_GNum)
        );
        canvas.drawRect(rectF3,paint);

        RectF rectF4 = new RectF(
                leftWidth + itemWidth/2 - 20 + itemWidth * 3,8 * itemHeight,
                leftWidth + itemWidth/2 + 20 + itemWidth * 3,getYHeight(LDL_GNum)
        );
        canvas.drawRect(rectF4,paint);

        RectF rectF5 = new RectF(
                leftWidth + itemWidth/2 - 20 + itemWidth * 4,8 * itemHeight,
                leftWidth + itemWidth/2 + 20 + itemWidth * 4,getYHeight(TCNum/HDL_GNum)
        );
        canvas.drawRect(rectF5,paint);

    }


    /**
     * 绘制基本线
     */
    private void initLinesChat(Canvas canvas){
        initGridPaint();
        //绘制竖排的线
        for (int i = 0; i < 6; i++) {
            canvas.drawLine(leftWidth + itemWidth * i,0,
                    leftWidth + itemWidth * i,height,paint);
        }

        //绘制横排的线
        canvas.drawLine(leftWidth,itemHeight * 8,
                leftWidth +width,itemHeight * 8,paint);
        paint.setStrokeWidth(1f);
        DashPathEffect dashPathEffect =  new DashPathEffect(new float[]{2f,2f},0);
        paint.setPathEffect(dashPathEffect);
        for (int i = 1; i < 12; i++) {
            canvas.drawLine(leftWidth,itemHeight * i,
                    leftWidth +width,itemHeight * i,paint);
        }

        //文字坐标值
        initTextPaint();
        for (int i = 1; i < 12; i++) {
            //绘制刻度
            canvas.drawText(strs[i - 1] + "",leftWidth/2,itemHeight * i + 10,paint);
        }

    }

    /**
     * 绘制临界值
     */
    private void initLinesChat2(Canvas canvas){
        initPaint();
        //总胆固醇 正常范围为:2.84~5.2,6.2=临界点(红线)
        //甘油三酯 正常范围为:0.56~1.7,2.3=临界点(红线)
        //高密度脂蛋白 正常范围为:大于等于1,
        //低密度脂蛋白  正常范围为:2.07~<3.4,4=临界点

        paint.setColor(redColor);
        paint.setStrokeWidth(1f);
        DashPathEffect dashPathEffect =  new DashPathEffect(new float[]{4f,4f},0);
        paint.setPathEffect(dashPathEffect);

        canvas.drawLine(leftWidth,1.8f * itemHeight,
                leftWidth + itemWidth,1.8f * itemHeight,paint);

        canvas.drawLine(leftWidth + itemWidth,5.7f * itemHeight,
                leftWidth + itemWidth * 2,5.7f * itemHeight,paint);

        canvas.drawLine(leftWidth + itemWidth * 3,4f * itemHeight,
                leftWidth + itemWidth * 4,4f * itemHeight,paint);

        //绘制提示
        canvas.drawLine(leftWidth + width/3 + 330,height + 140,
                leftWidth + width/3 + 410,height + 140,paint);


        paint.reset();
        paint.setColor(redColor);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(leftWidth,1.8f * itemHeight,7,paint);
        canvas.drawCircle(leftWidth + itemWidth,1.8f * itemHeight,7,paint);

        canvas.drawCircle(leftWidth + itemWidth * 1,5.7f * itemHeight,7,paint);
        canvas.drawCircle(leftWidth + itemWidth * 2,5.7f * itemHeight,7,paint);

        canvas.drawCircle(leftWidth + itemWidth * 3,4f * itemHeight,7,paint);
        canvas.drawCircle(leftWidth + itemWidth * 4,4f * itemHeight,7,paint);

        //绘制提示
        canvas.drawCircle(leftWidth + width/3 + 330,height + 140,7,paint);
        canvas.drawCircle(leftWidth + width/3 + 410,height + 140,7,paint);

        paint.setColor(wColor);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(leftWidth,1.8f * itemHeight,5,paint);
        canvas.drawCircle(leftWidth + itemWidth,1.8f * itemHeight,5,paint);

        canvas.drawCircle(leftWidth +itemWidth * 1,5.7f * itemHeight,5,paint);
        canvas.drawCircle(leftWidth + itemWidth * 2,5.7f * itemHeight,5,paint);

        canvas.drawCircle(leftWidth +itemWidth * 3,4f * itemHeight,5,paint);
        canvas.drawCircle(leftWidth + itemWidth * 4,4f * itemHeight,5,paint);

        //绘制提示
        canvas.drawCircle(leftWidth + width/3 + 330,height + 140,5,paint);
        canvas.drawCircle(leftWidth + width/3 + 410,height + 140,5,paint);

        //绘制正常区域
        paint2 = new Paint();
        paint2.setColor(blueColor1);
        paint2.setStyle(Paint.Style.FILL);

        RectF rectF1 = new RectF(leftWidth,(8f - 2.84f) * itemHeight,
                leftWidth + itemWidth,(8f - 5.2f) * itemHeight);
        canvas.drawRect(rectF1,paint2);

        RectF rectF2 = new RectF(leftWidth + itemWidth,(8f - 0.56f) * itemHeight,
                leftWidth + itemWidth * 2,(8f - 1.7f) * itemHeight);
        canvas.drawRect(rectF2,paint2);

        RectF rectF3 = new RectF(leftWidth + itemWidth * 2,(8f - 1f) * itemHeight,
                leftWidth + itemWidth * 3,(8f - 7.5f) * itemHeight);
        canvas.drawRect(rectF3,paint2);

        RectF rectF4 = new RectF(leftWidth + + itemWidth * 3,(8f - 2.07f) * itemHeight,
                leftWidth + itemWidth * 4,(8f - 3.4f) * itemHeight);
        canvas.drawRect(rectF4,paint2);

        RectF rectF5 = new RectF(leftWidth + + itemWidth * 4,(8f - 0f) * itemHeight,
                leftWidth + itemWidth * 5,(8f - 5.2f) * itemHeight);
        canvas.drawRect(rectF5,paint2);

    }

    /**
     * 绘制最底部的文字
     */
    private void initBottomText(Canvas canvas){
        initTextPaint();
        paint.setTextSize(24);
        canvas.drawText("总胆固醇(TC)",
                leftWidth + itemWidth/2,height + 40,paint);
        canvas.drawText("甘油三酯(TG)",
                leftWidth + itemWidth + itemWidth/2,height + 40,paint);
        canvas.drawText("高密度脂蛋白(HDL-C)",
                leftWidth + itemWidth * 2 + itemWidth/2,height + 40,paint);
        canvas.drawText("低密度脂蛋白(LDL-G)",
                leftWidth + itemWidth * 3 + itemWidth/2,height + 40,paint);
        canvas.drawText("总胆固醇/高密度脂蛋白(TC/HDL)",
                leftWidth + itemWidth * 4 + itemWidth/2,height + 40,paint);

        paint.setTextSize(22);
        canvas.drawText(TCNum + "mmol/l",
                leftWidth + itemWidth/2,height + 70,paint);
        canvas.drawText(TGNum + "mmol/l",
                leftWidth + itemWidth + itemWidth/2,height + 70,paint);
        canvas.drawText(HDL_GNum + "mmol/l",
                leftWidth + itemWidth * 2 + itemWidth/2,height + 70,paint);
        canvas.drawText(LDL_GNum + "mmol/l",
                leftWidth + itemWidth * 3 + itemWidth/2,height + 70,paint);

        DecimalFormat decimalFormat=new DecimalFormat(".00");
        canvas.drawText( decimalFormat.format(TCNum / HDL_GNum),
                leftWidth + itemWidth * 4 + itemWidth/2,height + 70,paint);

        //绘制提示
        canvas.drawRect(new RectF(
                leftWidth + width/3,height + 130,
                leftWidth + width/3 + 20,height + 150
        ),paint2);
        paint.setTextSize(20);
        canvas.drawText("正常区域",leftWidth + width/3 + 66,height + 149,paint);
        canvas.drawText("临界点",leftWidth + width/3 + 450,height + 149,paint);

    }


    /**
     * 设置画笔
     */
    private void initPaint(){
        paint.reset();
        paint.setStrokeWidth(2.0f);
        paint.setColor(linesColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
    }

    private void initTextPaint(){
        paint.reset();
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(30);
        paint.setColor(textColor);
    }

    private void initGridPaint(){
        //表格线画笔
        paint.reset();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2.0f);
        paint.setColor(linesColor);
        paint.setAntiAlias(true);
    }

    private void initChatPaint(){
        //表格线画笔
        paint.reset();
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1.0f);
        paint.setColor(blueColor2);
        paint.setAntiAlias(true);
//        LinearGradient shader = new LinearGradient(0,0,50,0,
//                new int[]{blueColor2, blueColor2, blueColor1},
//                new float[]{0 , 0.5f, 1.0f}, Shader.TileMode.MIRROR
//        );
//        paint.setShader(shader);
    }

    /**
     * 获取高度
     * @param value
     * @return
     */
    private float getYHeight(float value){
        if (value >= 7.5){
            value = 7.5f;
        }
        if (value >= 0){
          return itemHeight * (8 - value);
        }else {
          return itemHeight + itemHeight * (-value);
        }
    }

}

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

推荐阅读更多精彩内容

  • 又是她的梦 醒来,失落。 梦是个奇怪的东西,可以给你虚构无限的场景,而一切你又控制不了。有让你喜的,有让你悲的。可...
    若苦行阅读 359评论 0 0
  • 时间管理司马腾第四期21天训练营,第4天 1.昨晚睡觉 23:24(昨晚兼职又下线晚了) 2.今天起床 5:5...
    122182c44b49阅读 161评论 0 0
  • 今天立下flag从内到外的改变,我要蜕变,不能安于现状的苟延残喘的活着了,没有任何的意义,没有任何的帮助。 对自己...
    小狮子Emily阅读 188评论 0 0