Android實(shí)現(xiàn)動(dòng)態(tài)體溫計(jì)
本文實(shí)例為大家分享了Android實(shí)現(xiàn)動(dòng)態(tài)體溫計(jì)的具體代碼,供大家參考,具體內(nèi)容如下
前段時(shí)間在做一個(gè)生理參數(shù)采集的項(xiàng)目,其中涉及到體溫模塊。這是我的部分總結(jié)。
實(shí)現(xiàn)內(nèi)容: 從文件中讀取體溫?cái)?shù)據(jù),動(dòng)態(tài)繪制體溫的效果。即體溫?cái)?shù)據(jù)隨時(shí)間在不停的變化。體溫計(jì)繪制效果為立體效果。
實(shí)現(xiàn)原理:
1、體溫計(jì)的繪制
繪制原理:
體溫計(jì)的大體框架由圖1,2,4,5,6,7構(gòu)成,繪制通過(guò)自定義View,DrawView的onDraw()方法來(lái)實(shí)現(xiàn),體溫計(jì)水銀柱的的繪制通過(guò)SurfaceView來(lái)實(shí)現(xiàn)。根據(jù)屏幕寬度來(lái)設(shè)定體溫計(jì)大小及位置。
圖1,2,6構(gòu)成體溫計(jì)玻璃管,由顏色Color.argb(255, 25, 25, 112)和顏色Color.argb(250, 65,105,225)從左往右一次填充,實(shí)現(xiàn)漸變。圖3是動(dòng)態(tài)矩形,為體溫計(jì)水銀柱,由Color.RED和Color.argb(250, 255, 255, 0)有下往上填充,實(shí)現(xiàn)紅色到橙色的漸變。圖8為體溫計(jì)水銀柱頭部,用紅色填充。圖4,5組合形成光暈,圖4由Color.argb(30, 250, 250, 250)填充,圖5填充顏色與體溫計(jì)玻璃管相同。先繪制圖4再繪制圖5,于是,便形成月牙形光暈。圖7為光暈,由Color.argb(30, 250, 250, 250)填充。然后畫(huà)出刻度線,這樣便制作出具有立體感的體溫計(jì)。感覺(jué)底座部分設(shè)計(jì)的不大好,立體感不強(qiáng)。
動(dòng)態(tài)刷新原理:將從文件中的體溫?cái)?shù)據(jù)讀取,存儲(chǔ)到數(shù)組當(dāng)中,繪制體溫時(shí),根據(jù)數(shù)據(jù)來(lái)確定中間紅色水銀柱的坐標(biāo),其實(shí),也就是動(dòng)態(tài)矩形的繪制,采用定時(shí)繪制的方法實(shí)現(xiàn)動(dòng)態(tài)效果。
原理說(shuō)的差不多了,我們來(lái)看下代碼實(shí)現(xiàn)過(guò)程:
布局文件:textView用來(lái)顯示數(shù)值,surfaceView用來(lái)繪制動(dòng)態(tài)矩形。
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="開(kāi)始" 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>
體溫計(jì)繪制View代碼段:
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;//圓心坐標(biāo) 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 自動(dòng)生成的構(gòu)造函數(shù)存根 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; //設(shè)置最小大刻度線條參數(shù) Paint paintLine1 = new Paint(); paintLine1.setColor(Color.BLUE); paintLine1.setStrokeWidth(Line3_size); //設(shè)置中等刻度線條參數(shù) Paint paintLine2 = new Paint(); paintLine2.setColor(Color.YELLOW); paintLine2.setStrokeWidth(Line2_size); //設(shè)置最小刻度線條參數(shù) Paint paintLine3 = new Paint(); paintLine3.setColor(Color.GREEN); paintLine3.setStrokeWidth(Line1_size); //設(shè)置文字參數(shù) 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); //繪制動(dòng)態(tài)矩形 // 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 自動(dòng)生成的方法存根 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++; } } }; //定時(shí)刷新 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); //設(shè)置背景色為透明 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; } // 停止計(jì)時(shí)器 private void stopTimer() { if (timer != null) { timer.cancel(); timer = null; } if (task != null) { task.cancel(); task = null; } } // 繪制體溫動(dòng)態(tài)柱形圖 private void draw(float min_data) { int ydegree = 9 * t_height; float y = ydegree - (min_data - 35) * t_height; Canvas canvas = holder.lockCanvas(); // 繪制動(dòng)態(tài)矩形 Paint nPaint = new Paint(); nPaint.setColor(Color.RED); canvas.drawRect(x0 + 10, y, x1 - 10, y1, nPaint); holder.unlockCanvasAndPost(canvas);// 更新屏幕顯示內(nèi)容 */ } class MyCallBack implements SurfaceHolder.Callback { @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin StateFlow單數(shù)據(jù)更新熱流設(shè)計(jì)與使用介紹
StateFlow當(dāng)值發(fā)生變化,就會(huì)將值發(fā)送出去,下流就可以接收到新值。在某些場(chǎng)景下,StateFlow比LiveData更適用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09Android車(chē)載空調(diào)系統(tǒng)(HVAC)開(kāi)發(fā)方法分析
HVAC?全稱(chēng):供暖通風(fēng)與空氣調(diào)節(jié)(Heating?Ventilation?and?Air?Conditioning),用戶可以通過(guò)他來(lái)控制整個(gè)汽車(chē)的空調(diào)系統(tǒng),是汽車(chē)中非常重要的一個(gè)功能,汽車(chē)的空調(diào)HMI雖然并不復(fù)雜,但是大多都是用符號(hào)來(lái)表示功能,必須理解空調(diào)的各個(gè)符號(hào)表示的含義2023-12-12Android開(kāi)發(fā)騰訊驗(yàn)證碼遇到的坑
這篇文章主要介紹了Android開(kāi)發(fā)騰訊驗(yàn)證碼遇到的坑,需要的朋友可以參考下2017-12-12Kotlin引用其他xml的view對(duì)象過(guò)程詳解
這篇文章主要介紹了Kotlin中如何引用其他xml中的view對(duì)象,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02Android實(shí)現(xiàn)將View保存成Bitmap的方法
這篇文章主要介紹了Android實(shí)現(xiàn)將View保存成Bitmap的方法,涉及Android畫(huà)布Canvas、位圖bitmap及View的相關(guān)使用技巧,需要的朋友可以參考下2016-06-06Android?Flutter實(shí)現(xiàn)創(chuàng)意時(shí)鐘的示例代碼
時(shí)鐘這個(gè)東西很奇妙,總能當(dāng)做創(chuàng)意實(shí)現(xiàn)的入口。這篇文章主要介紹了如何通過(guò)Android?Flutter實(shí)現(xiàn)一個(gè)創(chuàng)意時(shí)鐘,感興趣的小伙伴可以了解一下2023-03-03Android 混合動(dòng)畫(huà)詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 混合動(dòng)畫(huà)詳解及實(shí)現(xiàn)代碼的相關(guān)資料,簡(jiǎn)單的一種動(dòng)畫(huà)(如旋轉(zhuǎn)、縮放、漸變、位移等)有時(shí)候并不能滿足我們項(xiàng)目的要求,這時(shí)候就需要運(yùn)用到混合動(dòng)畫(huà),需要的朋友可以參考下2016-11-11