记录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);
        }
    }

}

你可能感兴趣的:(记录TV开发写过的自定义控件(1))