記錄TV開發(fā)寫過的自定義控件(1)

記錄TV開發(fā)過程中使用的自定義控件(代碼+圖片)

網(wǎng)絡(luò)測速儀表盤

效果圖

網(wǎng)絡(luò)測速效果圖

代碼


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   : 網(wǎng)絡(luò)測速儀表盤
 */
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("測量網(wǎng)速",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);
                //旋轉(zhuǎn)動畫
                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;
        }
    }

    /**
     * 獲取旋轉(zhuǎn)的角度
     */
    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;
        }

    }

    /**
     * 根據(jù)網(wǎng)速獲取角度值
     */
    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;
    }

}

中醫(yī)五運六氣柱狀圖

效果圖

五運六氣

代碼


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   : 中醫(yī)五運六氣
 */
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);

        //繪制正常區(qū)域
        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("正常區(qū)域",leftWidth + width/3 + 66,height + 149,paint);
        canvas.drawText("臨界點",leftWidth + width/3 + 450,height + 149,paint);

    }


    /**
     * 設(shè)置畫筆
     */
    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);
        }
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末璧亚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子撑刺,更是在濱河造成了極大的恐慌爸吮,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铅忿,死亡現(xiàn)場離奇詭異剪决,居然都是意外死亡,警方通過查閱死者的電腦和手機檀训,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進店門柑潦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人峻凫,你說我怎么就攤上這事渗鬼。” “怎么了荧琼?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵譬胎,是天一觀的道長。 經(jīng)常有香客問我命锄,道長堰乔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任脐恩,我火速辦了婚禮镐侯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘驶冒。我一直安慰自己苟翻,他們只是感情好,可當我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布骗污。 她就那樣靜靜地躺著崇猫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪需忿。 梳的紋絲不亂的頭發(fā)上诅炉,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天,我揣著相機與錄音屋厘,去河邊找鬼汞扎。 笑死,一個胖子當著我的面吹牛擅这,可吹牛的內(nèi)容都是我干的澈魄。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼仲翎,長吁一口氣:“原來是場噩夢啊……” “哼痹扇!你這毒婦竟也來了铛漓?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤鲫构,失蹤者是張志新(化名)和其女友劉穎浓恶,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體结笨,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡包晰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了炕吸。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片伐憾。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖赫模,靈堂內(nèi)的尸體忽然破棺而出树肃,到底是詐尸還是另有隱情,我是刑警寧澤瀑罗,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布胸嘴,位于F島的核電站,受9級特大地震影響斩祭,放射性物質(zhì)發(fā)生泄漏劣像。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一摧玫、第九天 我趴在偏房一處隱蔽的房頂上張望驾讲。 院中可真熱鬧,春花似錦席赂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至掠拳,卻和暖如春癞揉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背溺欧。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工喊熟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人姐刁。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓芥牌,卻偏偏與公主長得像,于是被迫代替她去往敵國和親聂使。 傳聞我的和親對象是個殘疾皇子壁拉,可洞房花燭夜當晚...
    茶點故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容

  • 又是她的夢 醒來谬俄,失落。 夢是個奇怪的東西弃理,可以給你虛構(gòu)無限的場景溃论,而一切你又控制不了。有讓你喜的痘昌,有讓你悲的钥勋。可...
    若苦行閱讀 356評論 0 0
  • 時間管理司馬騰第四期21天訓(xùn)練營辆苔,第4天 1.昨晚睡覺 23:24(昨晚兼職又下線晚了) 2.今天起床 5:5...
    122182c44b49閱讀 159評論 0 0
  • 今天立下flag從內(nèi)到外的改變算灸,我要蛻變,不能安于現(xiàn)狀的茍延殘喘的活著了姑子,沒有任何的意義乎婿,沒有任何的幫助。 對自己...
    小獅子Emily閱讀 188評論 0 0