博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计
阅读量:7015 次
发布时间:2019-06-28

本文共 4928 字,大约阅读时间需要 16 分钟。

     要想实现带动画效果的电子温度计,需要以下几个知识点:

1、温度传感器相关知识。

2、ScaleAnimation动画相关知识,来进行水印刻度的缩放效果。

3、android:layout_weight属性的合理运用,关于android:layout_weight属性的讲解,可以参考:

地址为:

     首先来看看本实例的具体效果,然后再来具体实现功能。

1、将温度强制设置为0度时,画面如下:

 

 

2、将温度强制设置为50度时,画面如下:

 

 

3、将温度强制设置为-20度时,画面如下:

 

 

4、从传感器动态得到温度值,并实时更新画面,如下所示:

 

                                                                                          

本文《

(地址:)

 

 

首先来看布局文件的代码:layout_thermometer.xml

接着看Activity的代码:ThermometerActivity.java

package com.oyp.thermometer;import android.app.Activity;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.view.animation.ScaleAnimation;import android.widget.LinearLayout;import android.widget.TextView;/** * @author 欧阳鹏 * @date 2015年9月14日 
* 欧阳鹏CSDN博客地址 */public class ThermometerActivity extends Activity implements SensorEventListener { private LinearLayout alcohol; private LinearLayout meter; private SensorManager mSensorManager; private Sensor temperatureSensor; private TextView thermo_c; private TextView thermo_f; public float staratemp; public float temp; private float temperatureC; /** * 获取华氏温度 * * @author ouyang * @date 2015年9月14日 * @return */ public float getTemperatureF() { float temperatureF = (temperatureC * 9 / 5) + 32; return getFloatOne(temperatureF); } /** * 保留一位小数点 * * @author ouyang * @date 2015年9月14日 * @param tempFloat * @return */ public float getFloatOne(float tempFloat) { return (float) (Math.round(tempFloat * 10)) / 10; } /** * 获取摄氏温度 * * @author ouyang * @date 2015年9月14日 * @return */ public float getTemperatureC() { return getFloatOne(temperatureC); } public void setTemperatureC(float temperatureC) { this.temperatureC = temperatureC; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_thermometer); meter = ((LinearLayout) findViewById(R.id.meter)); alcohol = ((LinearLayout) findViewById(R.id.alcohol)); thermo_c = (TextView) findViewById(R.id.thermo_c); thermo_f = (TextView) findViewById(R.id.thermo_f); } @Override protected void onResume() { super.onResume(); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); temperatureSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); mSensorManager.registerListener(this, temperatureSensor, SensorManager.SENSOR_DELAY_NORMAL); } @Override public final void onSensorChanged(SensorEvent event) { float temperatureValue = event.values[0]; // 得到温度 setTemperatureC(temperatureValue);// 设置温度 mUpdateUi();// 更新UI } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } /** * 更新刻度上水银柱的长度 * * @author ouyang * @date 2015年9月14日 */ private void mUpdateUi() { ScaleAnimation localScaleAnimation1 = new ScaleAnimation(1.0F, 1.0F, this.staratemp, this.temp, 1, 0.5F, 1, 1.0F); localScaleAnimation1.setDuration(2000L); localScaleAnimation1.setFillEnabled(true); localScaleAnimation1.setFillAfter(true); this.alcohol.startAnimation(localScaleAnimation1); this.staratemp = this.temp; ScaleAnimation localScaleAnimation2 = new ScaleAnimation(1.0F, 1.0F, 1.0F, 1.0F, 1, 0.5F, 1, 0.5F); localScaleAnimation2.setDuration(10L); localScaleAnimation2.setFillEnabled(true); localScaleAnimation2.setFillAfter(true); this.meter.startAnimation(localScaleAnimation2); // 把刻度表看出总共700份,如何计算缩放比例。从-20°到50°。 // 例如,现在温度是30°的话,应该占(30+20)*10=500份 其中20是0到-20°所占有的份 this.temp = (float) ((20.0F + getTemperatureC()) * 10) / (70.0F * 10); thermo_c.setText(getTemperatureC() + ""); thermo_f.setText(getTemperatureF() + ""); }}

 

 具体代码可以在下面地址中免费下载:

 

        ====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址

====================================================================================

 

转载于:https://www.cnblogs.com/ouyangpeng/p/8537888.html

你可能感兴趣的文章
eclipse连接android设备的问题
查看>>
.pem引发的血案
查看>>
如何在rootscope 获取angular ui的tab子域 scope 也叫子域暴露
查看>>
HashMap,HashTable,HashSet区别
查看>>
如何学习iOS开发——对新手的几句废话
查看>>
无延时显示Toast的方法
查看>>
算法学习笔记(四)---第k个二进制数字问题
查看>>
忘记sa密码,又删除了windows身份验证账号的解决方法
查看>>
如何判断 Linux 是否运行在虚拟机上
查看>>
通过注册表开通关闭局域网共享
查看>>
Asp.net 导入导出Excel
查看>>
Oracle 10.2.0.4(5)EM不能启动的解决方案
查看>>
AngularJS—— 独立作用域
查看>>
缓存Redis和Memcached的使用和对比
查看>>
Android开机自动运行APP——BroadcastReceiver
查看>>
java.io几种读写文件的方式
查看>>
Spring中常用的注解(@Entity,@Table,@Column,@Repository,@Service)
查看>>
SharePoint 2013 配置传入电子邮件(一)
查看>>
Linux命令篇之chsh命令和chfn命令
查看>>
Useful websites
查看>>