我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计

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

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

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

3、android:layout_weight属性的合理运用,关于android:layout_weight属性的讲解,可以参考:《我的Android进阶之旅------>关于android:layout_weight属性的一个面试题》

地址为:http://blog.csdn.net/ouyang_peng/article/details/13236519

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

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

 

 

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

 

 

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

 

 

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

 

                                                                                          

本文《我的Android进阶之旅------>Android利用温度传感器实现有动画效果的电子温度计

(地址:http://blog.csdn.net/ouyang_peng/article/details/48790289)

 

 

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Parent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_thermometer"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:gravity="bottom|center"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="bottom|center" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <!-- 顶部图片 -->
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/background_top" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="100.0"
                android:orientation="horizontal" >

                <!-- 横向空白占1份 -->
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="bottom|center" >
                </LinearLayout>

                <!-- 横向占4份 -->
                <LinearLayout
                    android:id="@+id/meter"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_weight="4"
                    android:animationCache="true"
                    android:background="@drawable/thermometer_dial"
                    android:clipChildren="false"
                    android:clipToPadding="false"
                    android:persistentDrawingCache="all" >

                    <!-- 横向占189份,刻度表左边刻度摄氏温度所占比例 -->
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="189"
                        android:visibility="invisible" />

                    <!-- 横向占69份,刻度表水银柱子所在区域所占比例 -->
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="69"
                        android:orientation="vertical" >

                        <!-- 竖向占131.0份,刻度表50°以上的部分 -->
                        <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="0dp"
                            android:layout_weight="131.0" />

                        <!--
                              	竖向占773.0份,水银针正好等于刻度表从-20°到50°
 							   	因为柱子和圆球直接有点断层,把773变成774
 							   	默认不显示柱子,当有传感器数据时,才显示出来
                        -->
                        <LinearLayout
                            android:id="@+id/alcohol"
                            android:layout_width="fill_parent"
                            android:layout_height="0dp"
                            android:layout_weight="774.0"
                            android:background="@drawable/alcohol"
                            android:orientation="horizontal"
                            android:visibility="invisible" />

                        <!--
                             	竖向占104.0份 ,刻度表-20°以上的部分
								因为柱子和圆球直接有点断层,把104变成103
                        -->
                        <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="0dp"
                            android:layout_weight="103.0" />
                    </LinearLayout>

                    <!-- 横向占189份,刻度表右边刻度华氏温度所占比例 -->
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="187"
                        android:visibility="invisible" />
                </LinearLayout>

                <!-- 横向占4份 -->
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="150dp"
                    android:layout_weight="4"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingLeft="30dp" >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:orientation="horizontal" >

                        <!-- 摄氏温度 -->
                        <TextView
                            android:id="@+id/thermo_c"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#e9bc57"
                            android:textSize="38sp" />
                        <!-- 摄氏温度图标 -->
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:paddingLeft="2dp"
                            android:src="@drawable/thermo_c" />
                    </LinearLayout>

                    <!-- 分割线 -->
                    <ImageView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:src="@drawable/divider" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:orientation="horizontal" >

                        <!-- 华氏温度 -->
                        <TextView
                            android:id="@+id/thermo_f"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#dadada"
                            android:textSize="18sp" />
                        <!-- 华氏温度图标 -->
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:paddingLeft="2dp"
                            android:src="@drawable/thermo_f" />
                    </LinearLayout>
                </LinearLayout>

                <!-- 横向空白占1份 -->
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="bottom|center" >
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center_vertical"
                android:orientation="vertical" >

                <!-- 作者信息-->
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#ffff00"
                    android:textSize="18sp"
                    android:text="@string/author"
                    android:layout_gravity="center"/>
                <!-- 博客地址 -->
                 <TextView
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:autoLink="web"
                     android:text="@string/blog_address"
                     android:textColor="#ffff00"
                     android:textSize="18sp" />
            </LinearLayout>

            <!-- 尾部图片 -->
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/background_bottom" />
        </LinearLayout>
    </FrameLayout>

</LinearLayout>

接着看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日 <br>
 *       <a href="http://blog.csdn.net/ouyang_peng">欧阳鹏CSDN博客地址</a></n>
 */
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() + "");
	}
}

 

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

 

 http://download.csdn.net/detail/qq446282412/9145961

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

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

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

 

时间: 2024-08-29 11:58:16

我的Android进阶之旅------&gt;Android利用温度传感器实现带动画效果的电子温度计的相关文章

我的Android进阶之旅------&amp;gt;Android疯狂连连看游戏的实现之状态数据模型(三)

对于游戏玩家而言,游戏界面上看到的"元素"千变万化:但是对于游戏开发者而言,游戏界面上的元素在底层都是一些数据,不同数据所绘制的图片有所差异而已.因此建立游戏的状态数据模型是实现游戏逻辑的重要步骤. 1.定义数据模型 连连看的界面是一个NxM的"网格",每个网格上显示一张图片.而这个网格只需要一个二维数组来定义即可,而每个网格上所显示的图片,对于底层数据模型来说,不同的图片对于着不同的数值即可. 对于上图所示的数据模型,只要让数值为0的网格上不绘制图片,其他数值的网

我的Android进阶之旅------&amp;gt;Android疯狂连连看游戏的实现之开发游戏界面(二)

连连看的游戏界面十分简单,大致可以分为两个区域: 游戏主界面区 控制按钮和数据显示区 1.开发界面布局 本程序使用一个RelativeLayout作为整体的界面布局元素,界面布局上面是一个自定义组件,下面是一个水平排列的LinearLayout. 下面是本程序的布局文件:/res/layout/main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=

我的Android进阶之旅------&amp;gt;Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)

正如在<我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)>一文中看到的,在AbstractBoard的代码中,当程序需要创建N个Piece对象时,程序会直接调用ImageUtil的getPlayImages()方法去获取图片,该方法会随机从res/drawable目录中取得N张图片. 下面是res/drawable目录视图: 为了让getPlayImages()方法能随机从res/drawable目录中取得N张图片,具体实现分为以下几步: 通

我的Android进阶之旅------&amp;gt; Android为TextView组件中显示的文本添加背景色

通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article/details/46916963)      我们学会了在TextView中显示图片的方法,现在我们来学习如何为TextView组件中显示的文本添加背景色.要求完成的样子如图所示: 首先来学习使用BackgroundColorSpan对象设置文字背景色,代码如下: TextView textView=(

我的Android进阶之旅------&amp;gt;Android权限参考大全

访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permission.ACCESS_COARSE_LOCATION,通过WiFi或移动基站的方式获取用户错略的经纬度信息,定位精度大概误差在30~1500米 获取精确位置 android.permission.ACCESS_FINE_LOCATION,通过GPS芯片接收卫星的定位信息,定位精度达10米以内 访问定

我的Android进阶之旅------&amp;gt;Android颜色值(#AARRGGBB)透明度百分比和十六进制对应关系以及计算方法

我的Android进阶之旅-->Android颜色值(RGB)所支持的四种常见形式 透明度百分比和十六进制对应关系表格 透明度 十六进制 100% FF 99% FC 98% FA 97% F7 96% F5 95% F2 94% F0 93% ED 92% EB 91% E8 90% E6 89% E3 88% E0 87% DE 86% DB 85% D9 84% D6 83% D4 82% D1 81% CF 80% CC 79% C9 78% C7 77% C4 76% C2 75% B

我的Android进阶之旅------&amp;gt;Android颜色值(RGB)所支持的四种常见形式

Android中颜色值是通过红(Red).绿(Green).蓝(Blue)三原色,以及一个透明度(Alpha)值来表示的,颜色值总是以井号(#)开头,接下来就是Alpha-Red-Green-Blue的形式.其中Alpha值可以省略,如果省略了Alpha的值,那么该颜色默认是完全不透明的. Android的颜色值支持常见的四种形式如下所示: #RGB:分别指定红.绿.蓝三原色的值(只支持0~f这16级颜色)来代表颜色. #ARGB:分别指定红.绿.蓝三原色的值(只支持0~f这16级颜色)及透明度

我的Android进阶之旅------&amp;gt;Android疯狂连连看游戏的实现之游戏效果预览(一)

今天看完了李刚老师的<疯狂Android讲义>一书中的第18章<疯狂连连看>,从而学会了如何编写一个简单的Android疯狂连连看游戏.      开发这个流行的小游戏,难度适中,而且能充分激发学习热情,适合Android初学者来说是一个不错的选择.对于该游戏的开发,需要重点掌握单机游戏的界面分析和数据建模能力:游戏玩家严重看到的是游戏界面,但是在开发者眼中看到的应该是数据模型.除此之外,单机游戏通常需要一个比较美观的界面,需要通过自定义View来实现游戏主界面.      开发连

我的Android进阶之旅------&amp;gt;Android疯狂连连看游戏的实现之实现游戏逻辑(五)

在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的初始化设置信息. GameService:负责游戏的逻辑实现. 其中GameConf的代码如下:cn\oyp\link\utils\GameConf.java package cn.oyp.link.utils; import android.content.Context; /** * 保存游戏配置的对象