[Basics of Android development] Sensors (direction sensor, acceleration sensor)

I. Introduction

  • Description: Sensors are an important component of machine products. Only a product with sensors can be more "emotional". For example, map navigation products require the use of a variety of sensors (direction sensors, magnetic field sensors, acceleration sensors, etc.). The existence of sensors can better express users' needs that cannot be described in detail in words.
  • Difficulty: Intermediate
  • Knowledge points:
    1. Direction sensor
    2. Acceleration sensor
    3. Animation animation
  • Examples: compass, shake

2. Design

The virtual machine does not have sensors, so the sensor functions need to be simulated using a real machine.

1. Compass (direction sensor)

(1) Effect

Insert image description here

(2)UI design

Suggestion: Regarding the src picture path in ImageView, it is best to choose a picture with a sense of direction (such as arrows, isosceles triangles).

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image_znz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/znz"/>

</RelativeLayout>
(3) Functional design

Register sensor listener

    SensorManager mSensorManager; //管理器
    
    @Override
    protected void onResume(){
    
    
        super.onResume();
        mSensorManager.registerListener(mylistenner
                , mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
    }

Sensor event listener

    ImageView image;  //指南针图片
    float currentDegree = 0f; //指南针图片转过的角度
    Mylistenner  mylistenner =new Mylistenner();

	public  class Mylistenner implements SensorEventListener{
    
    
        
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
      //传感器值改变
            // TODO Auto-generated method stub

        }
        
        @Override
        public void onSensorChanged(SensorEvent event) {
    
      //精度改变
            // TODO Auto-generated method stub
            //获取触发event的传感器类型
            int sensorType = event.sensor.getType();

            switch(sensorType){
    
    
                case Sensor.TYPE_ORIENTATION:
                    float degree = event.values[0]; //获取z转过的角度
                    //穿件旋转动画
                    RotateAnimation ra = new RotateAnimation(currentDegree,-degree,Animation.RELATIVE_TO_SELF,0.5f
                            ,Animation.RELATIVE_TO_SELF,0.5f);
                    ra.setDuration(100);//动画持续时间
                    image.startAnimation(ra);
                    currentDegree = -degree;
                    break;

            }
        }
    }

Initialization code

        image = (ImageView)findViewById(R.id.image_znz);
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); //获取管理服务

2. Shake (acceleration sensor)

(1) Effect

Insert image description here

(2)UI design

Suggestion: It is best to divide a complete picture into two parts.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#404445"
    android:gravity="center"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image_sha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/sha"/>

    <ImageView
        android:id="@+id/image_xia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/xia"
        android:layout_below="@+id/image_sha"/>

</RelativeLayout>

Animation one translate_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0.0"
        android:fromYDelta="0.0"
        android:toXDelta="0.0"
        android:toYDelta="100"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:duration="1000"/>
</set>

Animation 2 translatex_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0.0"
        android:fromYDelta="0.0"
        android:toXDelta="0.0"
        android:toYDelta="-100"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:duration="1000" />
</set>
(3) Functional design

AndroidManifest.xml registration permissions

<uses-permission android:name="android.permission.VIBRATE" />

Acceleration sensor registration listener

	private SensorManager sensorManager;  //定义传感器管理器
	
    @Override
    protected void onResume() {
    
    
        super.onResume();
        sensorManager.registerListener(this,sensor , SensorManager.SENSOR_DELAY_GAME);
    }

Sensor event listener

    @Override
    public void onSensorChanged(SensorEvent event) {
    
    

        float[] values = event.values;  //获取传感器X、Y、Z三个轴的输出信息
        int sensorType = event.sensor.getType();  // 获取传感器类型
        if (sensorType == Sensor.TYPE_ACCELEROMETER) {
    
      //如果是加速度传感器
            //X轴输出信息>15,Y轴输出信息>15,Z轴输出信息>20
            if (values[0] > 15 || values[1] > 15 || values[2] > 20) {
    
    
                //动画向上
                Animation translate_sha = AnimationUtils.loadAnimation(this,R.anim.translatex_anim);
                sha.startAnimation(translate_sha);
                //动画向下
                Animation translate_xia = AnimationUtils.loadAnimation(this,R.anim.translate_anim);
                xia.startAnimation(translate_xia);
                Toast.makeText(MainActivity.this, "不好意思,摇晚了!!", Toast.LENGTH_SHORT).show();
                vibrator.vibrate(500);                    //设置振动器频率
                sensorManager.unregisterListener(this);  //取消注册监听器
            }
        }

    }

3. Accessories

(CSDN download address)
 
Orientation sensor: https://download.csdn.net/download/weixin_48916759/87916890
 
Acceleration sensor: https://download.csdn.net/download/weixin_48916759/87916891
 
(Gitee download address)
 
Orientation sensor: https ://gitee.com/xu-pq/android-demo/tree/master/zhinanzhenAcceleration
 
sensor: https://gitee.com/xu-pq/android-demo/tree/master/yaoyiyao

Guess you like

Origin blog.csdn.net/weixin_48916759/article/details/131247767