Android项目开发:简易计步器

Android项目开发:简易计步器的实现

本文将介绍基于Android的加速度传感器和陀螺仪传感器开发一个简易的计步器,基本原理:当检测到加速度发生改变时,使步数加一。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:text="简易计步器"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_step"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="0"
        android:textColor="#DE5347"
        android:textSize="100sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:text="开始"
        android:textSize="25sp" />

</LinearLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, SensorEventListener {

    private SensorManager sensorManager;
    private Sensor sensor;
    private TextView textView_step;
    private Button button_start;
    private int step;
    private double original_value;
    private double last_value;
    private double current_value;
    private boolean motionState=true; //是否处于运动状态
    private boolean processState=false;  //是否已经开始计步

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        step=0;
        original_value=0;
        last_value =0;
        current_value =0;

        sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
        sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //获取传感器,在计步器中需要使用的是加速度传感器
        sensorManager.registerListener(this,sensor,sensorManager.SENSOR_DELAY_UI);

        textView_step=(TextView) findViewById(R.id.textview_step);
        button_start=(Button) findViewById((R.id.button_start));
        button_start.setOnClickListener(this);

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        double range=1; //设置一个精度范围
        float[] value=event.values;
        current_value =magnitude(value[0],value[1],value[2]); //计算当前的模

        //向上加速的状态
        if(motionState==true){
            if (current_value >= last_value)
                last_value = current_value;
            else {
                //检测到一次峰值
                if(Math.abs(current_value-last_value)>range){
                    original_value=current_value;
                    motionState=false;
                }
            }
        }
        //向下加速的状态
        if(motionState==false){
            if (current_value <= last_value)
                last_value = current_value;
            else {
                //检测到一次峰值
                if(Math.abs(current_value-last_value)>range){
                    original_value=current_value;
                    if (processState==true){
                        step++; //检测到开始记录,步数加1
                        if(processState==true){
                            textView_step.setText(step+""); //更新读数
                        }
                    }
                    motionState=true;
                }
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    public void onClick(View v) {
        step=0;
        textView_step.setText("0");
        if (processState==true){
            button_start.setText("出发!GO!");
            processState=false;
        }else{
            button_start.setText("走不动了歇一会~");
            processState=true;
        }
    }

    private double magnitude(float x, float y, float z) {
        double magnitude=0;
        magnitude=Math.sqrt(x*x+y*y+z*z);
        return magnitude;
    }
}

链接:项目链接

  • 7
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值