zoukankan      html  css  js  c++  java
  • 新体温上报系统阶段二(2)

    TemperatureView代码

    package com.example;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;
    
    import static android.content.ContentValues.TAG;
    
    
    public class TemperatureView extends View {
    
    
        private int minValue;
        private int maxValue;
        private int currentValue;
        private int lastValue;
        private int nextValue;
        private Paint mPaint;
        private int viewHeight;
        private int viewWidth;
        private int pointX;
        private int pointY;
        private boolean isDrawLeftLine;
        private boolean isDrawRightLine;
        private int pointTopY = (int) (40 * Util.getDensity(getContext()));
        private int pointBottomY = (int) (200 * Util.getDensity(getContext()));
        private int mMiddleValue;
    
        public TemperatureView(Context context) {
            super(context);
        }
    
        public TemperatureView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public TemperatureView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
    
        //设置最小值
        public void setMinValue(int minValue){
            this.minValue = minValue;
        }
    
        //设置最大值
        public void setMaxValue(int maxValue){
            this.maxValue = maxValue;
        }
    
        //设置目前的值
        public void setCurrentValue(int currentValue){
            this.currentValue = currentValue;
        }
    
        //设置是否画左边线段(只有第一个View是false)
        public void setDrawLeftLine(boolean isDrawLeftLine){
            this.isDrawLeftLine = isDrawLeftLine;
        }
    
        //设置是否画右边线段(只有最后一个View是false)
        public void setDrawRightLine(boolean isDrawRightLine){
            this.isDrawRightLine = isDrawRightLine;
        }
    
        //设置之前温度点的值
        public void setLastValue(int lastValue){
            this.lastValue = lastValue;
        }
    
        //设置下一个温度点的值
        public void setNextValue(int nextValue){
            this.nextValue = nextValue;
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            //给一个初始长、宽
            int mDefaultWidth = 200;
            int mDefaultHeight = (int) (220 * Util.getDensity(getContext()));
            setMeasuredDimension(resolveSize(mDefaultWidth,widthMeasureSpec),resolveSize(mDefaultHeight,heightMeasureSpec));
            viewHeight = getMeasuredHeight();
            viewWidth = getMeasuredWidth();
            pointX = viewWidth / 2;
            Log.d(TAG, "onMeasure: " + viewWidth);
        }
    
        @Override
        public void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            mMiddleValue = (pointTopY + pointBottomY) / 2;
            pointY = mMiddleValue + (int) ((pointBottomY-pointTopY) * 1f / (maxValue - minValue) * ((maxValue + minValue) / 2 - currentValue));
            Log.d(TAG, "onDraw: " + pointY);
            mPaint = new Paint();
            drawGraph(canvas);
            drawValue(canvas);
            drawPoint(canvas);
        }
    
        //绘制数值
        private void drawValue(Canvas canvas){
            mPaint.setTextSize(40);
            setTextColor();
            mPaint.setStrokeWidth(0);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText(currentValue+"°",pointX , pointY - 20, mPaint);
        }
    
        //设置字体颜色
        public void setTextColor(){
            if(currentValue <= 35 && currentValue >=32){
                mPaint.setColor(Color.BLUE);
            }else if(currentValue <= 37 && currentValue > 35){
                mPaint.setColor(Color.GREEN);
            }else if(currentValue <= 40 && currentValue > 37){
                mPaint.setColor(Color.RED);
            }
        }
    
        //绘制温度点
        public void drawPoint(Canvas canvas){
            mPaint.setColor(Color.BLUE);
            mPaint.setStrokeWidth(2);
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawCircle(pointX, pointY, 10, mPaint);
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(pointX, pointY, 5, mPaint);
        }
    
        //绘制线段(线段组成折线)
        public void drawGraph(Canvas canvas){
            mPaint.setColor(0xFF24C3F1);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(3);
            mPaint.setAntiAlias(true);    //设置抗锯齿
    
            //判断是否画左线段(第一个View不用,其他全要)
            if(isDrawLeftLine){
                float middleValue = currentValue - (currentValue - lastValue) / 2f;
    
                float middleY = mMiddleValue + (int) ((pointBottomY-pointTopY) * 1f / (maxValue - minValue) * ((maxValue + minValue) / 2 - middleValue));
                canvas.drawLine(0, middleY, pointX, pointY, mPaint);
            }
            //判断是否画右线段(最后View不用,其他全要)
            if(isDrawRightLine){
                float middleValue = currentValue - (currentValue - nextValue) / 2f;
                float middleY = mMiddleValue + (int) ((pointBottomY-pointTopY) * 1f / (maxValue - minValue) * ((maxValue + minValue) / 2 - middleValue));
                canvas.drawLine(pointX, pointY, viewWidth, middleY, mPaint);
            }
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/jieguo"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        ></TextView>
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:orientation="vertical"
        android:gravity="center_horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <com.example.TemperatureView
            android:id="@+id/temp_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    
    </LinearLayout>
  • 相关阅读:
    1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
    1389. Create Target Array in the Given Order
    1385. Find the Distance Value Between Two Arrays
    1382. Balance a Binary Search Tree
    PHP的 __DIR__ 作用【转】
    PHP:Allowed memory size of 134217728 bytes exhausted问题解决方法【转】
    PHP: POST Content-Length of xxx bytes exceeds the limit of 8388608 bytes【转】
    PhpStorm 如何快速查找文件【转】
    .gitkeep的作用【转】
    php copy 函数使用 【转】
  • 原文地址:https://www.cnblogs.com/feng747/p/14909664.html
Copyright © 2011-2022 走看看