1 package com.example.test; 2 3 4 5 import android.content.Context; 6 import android.graphics.Bitmap; 7 import android.graphics.Canvas; 8 import android.graphics.Color; 9 import android.graphics.Paint; 10 import android.graphics.RectF; 11 import android.util.AttributeSet; 12 import android.util.TypedValue; 13 import android.view.SurfaceHolder; 14 import android.view.SurfaceView; 15 16 17 18 19 import java.text.DecimalFormat; 20 21 /** 22 * Created by yinxiaofei on 2016/1/13. 23 */ 24 25 public class Sphygmomanometer extends SurfaceView implements SurfaceHolder.Callback ,Runnable 107 108 public Sphygmomanometer(Context context, AttributeSet attrs) 114 @Override 115 protected void onMeasure(int with,int height) 135 @Override 136 public void surfaceCreated(SurfaceHolder surfaceHolder) 156 157 @Override 158 public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { 159 160 } 161 162 @Override 163 public void surfaceDestroyed(SurfaceHolder surfaceHolder) { 164 165 isRunning=false; 166 167 } 168 169 @Override 170 public void run() catch (InterruptedException e) { 181 e.printStackTrace(); 182 } 183 } 184 } 185 } 186 187 private void draw() 200 } catch (Exception e) { 201 // e.printStackTrace();这里的异常不处理, 202 } finally 206 } 207 208 } 209 210 private void drawShowHeightAndShow() elseelseelseelseelseelse{ 239 mSpeed= (float) 0.008; 240 } 241 242 } 243 } 244 } 245 } 246 } 247 if(addORsub){ 248 CurrentTemperature+=mSpeed*100; 249 }else{ 250 CurrentTemperature-=mSpeed*100; 251 } 252 253 // 254 255 Paint RectPaint=new Paint(); 256 RectPaint.setStyle(Paint.Style.FILL); 257 // RectPaint.setColor(getResources().getColor(R.color.theme_color)); 258 // 这里主要是对温度的显示,画矩形的过程中,唯一改变的就是Top这一个值了 259 //这里(CurrentTemperature-BeginTenperature)/20表示的是刻度值的换算,从温度计过度到血压计的刻度值 260 if(Math.abs(CurrentTemperature-TargetTemperature)>0.8) 261 mCanvas.drawRect(centerWith-MercuryWith/2, 262 (scaleLong)*10*(temperatureRange)+abHeight*2- 263 (CurrentTemperature-BeginTenperature)/60*10*scaleLong, 264 centerWith+MercuryWith/2, 265 (scaleLong)*10*(temperatureRange)+abHeight*2, 266 RectPaint); 267 else 275 276 //这里开始画显示的数字 277 Paint ShowNumberTextPaint=new Paint(); 278 ShowNumberTextPaint.setColor(Color.BLACK); 279 ShowNumberTextPaint.setTextSize(mShowSymbolTextSize); 280 ShowNumberTextPaint.setShader(null); 281 fomat = new DecimalFormat("##0.0"); 282 float display = Float.parseFloat(fomat.format(trueTemperature)); 283 mCanvas.drawText(display + " kpa", 284 mWith * 3 / 2 - ShowNumberTextPaint.getTextSize() * 2, 285 temperatureAllLong / 2 - ShowNumberTextPaint.getTextSize(), 286 ShowNumberTextPaint 287 ); 288 289 290 mCanvas.drawText(display + " kpa", 291 mWith*3/2- ShowNumberTextPaint.getTextSize() * 2, 292 temperatureAllLong/2+ShowNumberTextPaint.getTextSize(), 293 ShowNumberTextPaint 294 ); 295 296 } 297 298 private void drawBg() else{ 318 mCanvas.drawLine(centerWith+MercuryWith/2, 319 everyTemparaturHeight*i+j*(scaleLong)+abHeight*2, 320 centerWith+MercuryWith/2+MinLineLong, 321 everyTemparaturHeight*i+j*(scaleLong)+abHeight*2,LinePaint); 322 } 323 324 } 325 326 } 327 //画左边的刻度 328 for(int i=0;i<temperatureRange;i++)else{ 350 mCanvas.drawLine(centerWith-MercuryWith/2, 351 everyTemparaturHeight*i+j*(scaleLong)+abHeight*2, 352 centerWith-MercuryWith/2-MinLineLong, 353 everyTemparaturHeight*i+j*(scaleLong)+abHeight*2,LinePaint); 354 } 355 356 } 357 //画最后一个刻度 358 if(i==temperatureRange-1) 366 } 367 //画红色的园 368 Paint CirclePaint=new Paint(); 369 CirclePaint.setStyle(Paint.Style.FILL); 370 // CirclePaint.setColor(getResources().getColor(R.color.theme_color)); 371 mCanvas.drawCircle(centerWith, 372 everyTemparaturHeight*(temperatureRange)+abHeight*2+MercuryWith, 373 MercuryWith*3/2,CirclePaint); 374 //画摄氏度的符号 375 Paint symbolTextPaint=new Paint(); 376 symbolTextPaint.setColor(Color.BLACK); 377 symbolTextPaint.setTextSize(mSymbolTextSize); 378 symbolTextPaint.setShader(null); 379 mCanvas.drawText("BP", 380 centerWith - MaxLineLong / 2 - MercuryWith / 2 - symbolTextPaint.getTextSize() / 2, 381 abHeight * 2 - symbolTextPaint.getTextSize(), 382 symbolTextPaint 383 ); 384 mCanvas.drawText("BP", 385 centerWith + MaxLineLong / 2 + MercuryWith / 2 - symbolTextPaint.getTextSize() / 2, 386 abHeight * 2 - symbolTextPaint.getTextSize(), 387 symbolTextPaint 388 ); 389 390 //绘制显示数字的符号和虚线 391 Paint ShowsymbolTextPaint=new Paint(); 392 ShowsymbolTextPaint.setColor(Color.BLACK); 393 ShowsymbolTextPaint.setTextSize(mShowSymbolTextSize); 394 ShowsymbolTextPaint.setShader(null); 395 396 mCanvas.drawText("- - - - - - - -", 397 mWith * 3 / 2 - ShowsymbolTextPaint.getTextSize() * 2, 398 temperatureAllLong/2, 399 ShowsymbolTextPaint 400 ); 401 402 } 403 404 private float trueTemperature = 0; 405 406 public void setTargetTemperature(float targetTemperature) 411 if(targetTemperature>EndTenperrature) 414 TargetTemperature = targetTemperature; 415 } 416 }









