Android开发-Sensor传感器-AndroidStudio(一)摇一摇

    xiaoxiao2021-12-13  31

    转载请注明出处: http://blog.csdn.net/iwanghang/article/details/53407647 我正在参加 2016博客之星评选,希望得到您的宝贵一票~ http://blog.csdn.net/vote/candidate.html?username=iwanghang 请为我投票,谢谢 ~~ 如果没有账号 直接使用微信/QQ/微博登陆 就能投票了~~ 项目源码: http://download.csdn.net/detail/iwanghang/9697899(包含摇一摇"咔咔"的mp3文件) 这边我们实现一个摇一摇的效果,摇一摇手机 监听到传感器数据变化达到目标值,修改textView以及播放一个mp3 基本上就是实现了微信摇一摇的效果~ 我们看一下gif动图,以及代码,注意,传感器的监听不需要任何权限,不同于网络等功能 另外,Demo里面还有温度传感器的代码,可惜调试机没有这个传感器,并没有体现出效果 有兴趣的同学,可以自己试试 MainActivity.java: package com.iwanghang.sensordemo; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorListener; import android.hardware.SensorManager; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity { private TextView tv; private TextView tv_1; // 显示摇一摇 private TextView tv_2; // 显示温度 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); tv_1 = (TextView) findViewById(R.id.tv_1); // 显示摇一摇 tv_2 = (TextView) findViewById(R.id.tv_2); // 显示温度 // 获取 系统传感器管理器 SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); // 通过传感器管理器 获取 本地所有的传感器 List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL); for (Sensor s: sensors) { System.out.println("Sensor == " + s.toString()); } // 获取指定的某一个传感器 Sensor type_accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); if (type_accelerometer!=null) { System.out.println("Sensor 获取指定的某一个传感器 " + type_accelerometer.toString()); } // 注册传感器的监听器 (摇一摇) sm.registerListener(new SensorEventListener() { @Override public void onSensorChanged(SensorEvent sensorEvent) { // 传感器数据变化,在该方法中我们可以获取传感器变化的值 float x = sensorEvent.values[0]; float y = sensorEvent.values[1]; float z = sensorEvent.values[2]; tv.setText("x="+x+",y="+y+",z="+z); if (Math.abs(x)+Math.abs(y)+Math.abs(z)>=ringValue && flag == false){ flag = true; tv_1.setVisibility(View.VISIBLE); MediaPlayer mp = MediaPlayer.create(MainActivity.this,R.raw.yaoyiyao); mp.start(); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { mediaPlayer.release(); flag = false; MediaPlayer.create(MainActivity.this,R.raw.yaodaole).start(); tv_1.setVisibility(View.GONE); } }); } } @Override public void onAccuracyChanged(Sensor sensor, int i) { // 传感器精度的变化 } },type_accelerometer,SensorManager.SENSOR_DELAY_NORMAL); // 温度传感器 Sensor type_ambient_temperature = sm.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); System.out.println(type_ambient_temperature); sm.registerListener(new SensorEventListener() { @Override public void onSensorChanged(SensorEvent sensorEvent) { float temp = sensorEvent.values[0]; System.out.println(sensorEvent.values[0]); System.out.println(sensorEvent.values[1]); System.out.println(sensorEvent.values[2]); temp = (float) (Math.round(temp * 10.0) / 10.0); // 四舍五入 保留一位小数 tv_2.setText("温度:" + temp + "°C"); } @Override public void onAccuracyChanged(Sensor sensor, int i) { System.out.println(sensor); System.out.println(i); } },type_ambient_temperature,SensorManager.SENSOR_DELAY_NORMAL); } int ringValue = 40; boolean flag = false; } activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.iwanghang.sensordemo.MainActivity"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/tv_1" android:visibility="gone" android:layout_width="match_parent" android:layout_height="match_parent" android:text="摇一摇\n开始啦~\\(≧▽≦)/~" android:gravity="center" android:textSize="40sp" android:textColor="#ffffff" android:background="#000000" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="温度" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="100dp" /> </RelativeLayout> 转载请注明出处: http://blog.csdn.net/iwanghang/article/details/53407647 欢迎移动开发爱好者交流沈阳或周边城市公司有意开发Android,请与我联系联系方式微信:iwanghangQQ:413711276邮箱:iwanghang@qq.com
    转载请注明原文地址: https://ju.6miu.com/read-950076.html

    最新回复(0)