Android 동적 체온계 구현

14747 단어 Android체온계
본 논문 의 사례 는 안 드 로 이 드 가 동적 체온 계 를 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
얼마 전에 생리 적 인 파 라 메 터 를 수집 하 는 프로젝트 를 했 는데 그 중에서 체온 모듈 과 관련 되 었 다.이것 은 나의 부분 총화 이다.
구현 내용:파일 에서 체온 데 이 터 를 읽 고 체온 을 동적 으로 그립 니 다.즉 체온 데 이 터 는 시간 에 따라 끊임없이 변화 한다.체온계 그리 기 효 과 는 입체 효과 이다.

실현 원리:
1.체온계 의 제작

그리 기 원리:
체온계 의 대체 프레임 워 크 는 그림 1,2,4,5,6,7 로 구성 되 어 있 으 며,사용자 정의 View,DrawView 의 onDraw()방법 을 통 해 구현 되 며,체온계 수은 주의 그리 기 는 Surface View 를 통 해 이 루어 진다.스크린 너비 에 따라 체온계 의 크기 와 위 치 를 설정 합 니 다.
그림 1,2,6 은 체온계 유리관 을 구성 하고 색깔 Color.argb(255,25,25,112)와 색깔 Color.argb(250,65,105,225)를 왼쪽 에서 오른쪽으로 한 번 채 워 점차 적 인 변 화 를 실현 한다.그림 3 은 동적 사각형 으로 체온계 수은 기둥 으로 Color.RED 와 Color.argb(250,255,255,0)에서 아래로 채 워 빨간색 에서 주황색 으로 그 라 데 이 션 한다.그림 8 은 체온계 수은 기둥 머리 에 빨간색 으로 채 워 져 있다.그림 4,5 를 조합 하여 빛 멀미 를 형성 하고 그림 4 는 Color.argb(30,250,250,250)로 채 우 며 그림 5 의 충전 색 은 체온계 유리관 과 같다.먼저 그림 4 를 그리고 그림 5 를 그리 면 초승달 모양 의 빛 이 생 긴 다.그림 7 은 코로나 이 고 Color.argb(30,250,250,250)로 채 워 진다.그리고 눈금 선 을 그 려 입체 감 있 는 체온 계 를 만든다.페 데 스 탈 부분 디자인 이 잘 안 되 고 입체 감 이 강하 지 않 은 것 같 아 요.
동적 리 셋 원리:파일 의 체온 데 이 터 를 읽 고 배열 에 저장 하여 체온 을 그 릴 때 데이터 에 따라 중간 빨간색 수은 기둥 의 좌 표를 확인한다.사실은 동적 사각형 의 그리 기 이 고 시간 에 맞 춰 그 리 는 방법 으로 동적 효 과 를 실현 한다.
원리 에 대한 말 이 많 지 않 습 니 다.코드 실현 과정 을 살 펴 보 겠 습 니 다.
레이아웃 파일:textView 는 수 치 를 표시 하고 surfaceView 는 동적 사각형 을 그립 니 다.
temp.xml

<?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"
 android:orientation="vertical" >

 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/b03"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin" >

  <LinearLayout
   android:id="@+id/linearLayout02"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >
  </LinearLayout>

  <LinearLayout
   android:id="@+id/linearLayout01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" >

   <SurfaceView
    android:id="@+id/surfacetemp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="20dp" />
  </LinearLayout>

  <TextView 
   android:id="@+id/textview01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
   android:layout_gravity="center"
   android:layout_marginTop="160dp"
   android:textColor="#00C957"
   android:textSize="40sp" />

  <TextView
   android:id="@+id/textview02"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginLeft="10dp"
   android:layout_marginTop="130dp"
   android:layout_toRightOf="@+id/textview01"
   android:textColor="#00FF00"
   android:textSize="30sp" />

  <TextView
   android:id="@+id/textview03"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginLeft="2dp"
   android:layout_marginTop="130dp"
   android:layout_toRightOf="@+id/textview02"
   android:textColor="#00FF00"
   android:textSize="60sp" />

  <Button
   android:id="@+id/button01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:layout_marginBottom="40dp"
   android:layout_marginLeft="50dp"
   android:background="@drawable/button_selector"
   android:text="  "
   android:textColor="@color/paleturquoise"
   android:textSize="15sp" />

  <Button
   android:id="@+id/button02"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignParentRight="true"
   android:layout_marginBottom="40dp"
   android:layout_marginRight="50dp"
   android:background="@drawable/button_selector"
   android:text="  "
   android:textColor="@color/paleturquoise"
   android:textSize="15sp" />
 </RelativeLayout>

</LinearLayout>
체온계 보기 코드 세그먼트 그리 기:

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.util.DisplayMetrics;
import android.view.View;

public class DrawTemp extends View{


 private float wide;
 private float high;
 private float t_wide;
 private float t_high ;
 private float r; //    
 private float x0;//    
 private float x1;
 private float y0;
 private float y1;
 /*     */
 private int color_blue = Color.argb(255, 25, 25, 112);
 private int color_bule1 = Color.argb(250, 65,105,225);
 private int color_white = Color.argb(30, 250, 250, 250);
 private int color_white1 = Color.argb(60, 250, 250, 250);
 private int color_orange = Color.argb(250, 255, 255, 0);
 public DrawTemp(Context context) {
  super(context);
  // TODO            
   Paint paint = new Paint();
   paint.setColor(Color.YELLOW);
 }

  protected void onDraw(Canvas canvas)
  {
   DisplayMetrics dm = new DisplayMetrics(); 
   dm = getResources().getDisplayMetrics();
  wide = dm.widthPixels; //    (  , :480px) 
 high = dm.heightPixels; //    (  , :800px)
   t_wide = wide/20;
   t_high = high/20;
   r = t_high-10;
   x0 = wide/2+2*t_wide;
   x1 = wide/2+4*t_wide;
   y0 = t_high*2;
   y1 = 10*t_high;

   float ydegree = 9*t_high; 
   int min_temp = 35; //     35
   int m1 = 4;
   int Line1_size = 1;
    int Line2_size = 3;
    int Line3_size = 5;
   //           
   Paint paintLine1 = new Paint(); 
   paintLine1.setColor(Color.BLUE); 
   paintLine1.setStrokeWidth(Line3_size);
   //          
   Paint paintLine2 = new Paint(); 
   paintLine2.setColor(Color.YELLOW); 
   paintLine2.setStrokeWidth(Line2_size);
   //          
   Paint paintLine3 = new Paint(); 
   paintLine3.setColor(Color.GREEN); 
   paintLine3.setStrokeWidth(Line1_size);
   //      
   Paint text = new Paint();
   text.setColor(Color.MAGENTA);
   text.setTextSize(30);

   Paint mPaint = new Paint(); 
   mPaint.setStrokeWidth(m1);
   LinearGradient ng= new LinearGradient(x0-10, y0, x1-10, y0, color_blue,color_bule1, Shader.TileMode.CLAMP);
   mPaint.setShader(ng);
   canvas.drawRect(x0-10, y0, x1+10, y1, mPaint);//      
   canvas.drawCircle(x0+t_wide, y0, t_wide+10,mPaint );//         
   canvas.drawCircle(x0+t_wide, y1,r+10, mPaint);//      


   //     
   Paint nPaint = new Paint();
   nPaint.setColor(Color.RED);
   canvas.drawCircle(x0+t_wide, y1, r-10, nPaint); 
   //LinearGradient mg= new LinearGradient(x0+10, y1, x0-10, y0, Color.RED,color_orange, Shader.TileMode.CLAMP);
   LinearGradient mg= new LinearGradient(x0+10, y1, x1-10, y0, Color.RED,color_orange, Shader.TileMode.CLAMP); 
   nPaint.setShader(mg);

   //      
   // canvas.drawRect(x0+10, y, x1-10, y1, nPaint);

   //    ,    
   Paint paint = new Paint();
   paint.setColor(color_white);
   RectF Rect = new RectF(x0-5, y0,x0+5, y1-t_high);

   canvas.drawCircle(x0+t_wide, y0-t_wide/2-t_wide/3, t_wide/3,paint );
   canvas.drawCircle(x0+t_wide, y0, t_wide-t_wide/8,mPaint );

   canvas.drawCircle(x0+t_wide-8, y1, r-10, paint);

   canvas.drawCircle(x0+t_wide, y1, r-10, nPaint); 
   paint.setColor(color_white1);
   RectF Rect3 = new RectF(x0, y1, x0+t_wide, y1+t_wide);
   canvas.drawArc(Rect3, 0, 30, false, paint);

   while (ydegree > y0+30) {  
     canvas.drawLine(x1+10, ydegree, x1+15, ydegree, paintLine3); 
    if (ydegree % t_high == 0) { 
     canvas.drawLine(x1+10, ydegree, x1+50, ydegree, paintLine1); 
     canvas.drawText(min_temp + "", x1+55, ydegree + 5, text); 
     min_temp++; 
    } 
    else if(ydegree % (t_high/2) == 0)
    {
      canvas.drawLine(x1+10, ydegree, x1+25, ydegree, paintLine2); 
    }
    ydegree = ydegree - 2 ; 
   }  
} 
}
주 프로그램:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.temp_layout);

  innit(); //    
 ActionBarUtils.initActionBar(getApplicationContext(), getActionBar(),
    "  ");

  timer = new Timer();

   start.setOnClickListener(new OnClickListener()
   {

   @Override
   public void onClick(View v) {
    // TODO          
     timer= new Timer();

     handler = new Handler()
     { 
      @Override 
      public void handleMessage(Message msg) 
      {  //     
        s = String.valueOf(min_data);   

        if(msg.what == 1)
        { 
         text1.setText(s); 
        }
        if(msg.what == 0)
        {
         String num = String.valueOf(number);
         text1.setText(num);
        }
        else if(msg.what == 2)
        {
         text1.setText("   ");
        }

        super.handleMessage(msg); 
      } 
     };

     task = new TimerTask() 
     { 
      @Override 
      public void run() 
      { 
       Message message = new Message();
       // drawPmThread pm = new drawPmThread();
       if( min_data == number)
       {
        onDestroy();
       }

       if(number>40)
        {
        message.what = 2;
        handler.sendMessage(message);
        }

       if(0<min_data && min_data < 35)
       {
        message.what = 0;
        handler.sendMessage(message);
       }
       else if (35<=min_data && min_data<number)
       {

        draw(min_data);
        message.what = 1; 
        handler.sendMessage(message);
        min_data++;
       }

      } 
     };
   //      
     timer.schedule(task, 4, 40);
   }
   }
    );

  stop.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    stopTimer();
   }
  });
 }

 //      
 private void innit() {
  // TODO Auto-generated method stub
  start = (Button) findViewById(R.id.button01);
  stop = (Button) findViewById(R.id.button02);
  text1 = (TextView) findViewById(R.id.textview01);
  text2 = (TextView) findViewById(R.id.textview02);
  text3 = (TextView) findViewById(R.id.textview03);
  save = (TextView) findViewById(R.id.textView1);
  linearLayout02 = (LinearLayout) findViewById(R.id.linearLayout02);
  drawView = new DrawTemp(this);
  linearLayout02.addView(drawView);
  surface = (SurfaceView) findViewById(R.id.surfacetemp);

  String s2 = "o";
  String s3 = "C";
  text2.setText(s2);
  text3.setText(s3);
  TextPaint tp = start.getPaint();
  tp.setFakeBoldText(true);
  TextPaint tp1 = stop.getPaint();
  tp1.setFakeBoldText(true);
  scrn = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(scrn);

  holder = surface.getHolder(); 
  holder.addCallback(this);

  surface.setZOrderOnTop(true); //        
  surface.getHolder().setFormat(PixelFormat.TRANSLUCENT);
  // thread = new Thread(this,"SurfaceView");

  width = scrn.widthPixels; //   (PX)
  height = scrn.heightPixels; //   (PX)
  t_width = width/20;
  t_height = height/20;
  x0 = width/2+2*t_width;
  x1 = width/2+4*t_width;
  y1 = 10*t_height;

 }

 //      
 private void stopTimer() {

  if (timer != null) {
   timer.cancel();
   timer = null;
  }

  if (task != null) {
   task.cancel();
   task = null;
  }
 }
 //          
 private void draw(float min_data)
 {

  int ydegree = 9 * t_height;
  float y = ydegree - (min_data - 35) * t_height;
  Canvas canvas = holder.lockCanvas();
  //       
  Paint nPaint = new Paint();
  nPaint.setColor(Color.RED);
  canvas.drawRect(x0 + 10, y, x1 - 10, y1, nPaint);

  holder.unlockCanvasAndPost(canvas);//          */
 }

 class MyCallBack implements SurfaceHolder.Callback {

  @Override
  public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
    int arg3) {
   // TODO Auto-generated method stub
  }
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기