日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android加速度传感器

發布時間:2025/3/20 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android加速度传感器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android加速度傳感器

效果圖

手機平放桌面的兩張截屏,數據一直在刷新


源碼

下載地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9065313

步驟

傳感器使用步驟之前已經介紹過,地址:http://blog.csdn.net/q4878802/article/details/48112477

代碼

package com.example.kongqw.kqwsensorforaccelerometerdemo;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.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView;public class MainActivity extends Activity implements SensorEventListener {private TextView mTvShow;private SensorManager mSensorManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTvShow = (TextView) findViewById(R.id.tv_show);// 獲取傳感器管理者對象mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);// 獲取加速度傳感器對象Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);// 添加監聽器mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);}@Overridepublic void onSensorChanged(SensorEvent event) {// 傳感器返回的數據float[] values = event.values;StringBuffer buffer = new StringBuffer();buffer.append("X方向的加速度為:").append(values[0]).append("\n");buffer.append("Y方向的加速度為:").append(values[1]).append("\n");buffer.append("Z方向的加速度為:").append(values[2]).append("\n");mTvShow.setText(buffer);}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {} }

XML頁面布局

<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=".MainActivity"><TextView android:id="@+id/title"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:padding="10dp"android:text="加速度傳感器"android:textSize="20dp" /><TextView android:id="@+id/tv_show"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@+id/title"android:textSize="18dp" /></RelativeLayout>

轉載于:https://www.cnblogs.com/sesexxoo/p/6190525.html

總結

以上是生活随笔為你收集整理的Android加速度传感器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。