水平傳感器(磁場+加速度)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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"
    tools:context=".MainActivity">
    <com.lindingdu.app17.SpiritlevelView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.util.Printer;
import android.view.View;

public class SpiritlevelView extends View implements SensorEventListener {
    private Bitmap bubble;//小籃球位圖對象
    float[]accV=new float[3];//加速度傳感器的值
    float[]magV=new float[3];//磁場傳感器的值
    private int Max=30;//手機(jī)最大傾斜角度
    private int bx,by;//籃球的位置坐標(biāo)

    public SpiritlevelView(Context context,AttributeSet attrs) {
        super(context, attrs);
        SensorManager sensorManager = (SensorManager) context
                .getSystemService(Context.SENSOR_SERVICE);//獲取傳感器管理器

        sensorManager.registerListener(this
                ,sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
                ,SensorManager.SENSOR_DELAY_GAME);//為磁場傳感器注冊監(jiān)聽器
        sensorManager.registerListener(this
                ,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
                ,SensorManager.SENSOR_DELAY_GAME);//為加速度傳感器注冊監(jiān)聽器
        //加載籃球圖片
        bubble= BitmapFactory.decodeResource(getResources(),R.drawable.bubble);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {//值改變時觸發(fā)

        if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
            accV=event.values.clone();

        }else if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){
            magV=event.values.clone();
        }

        float R[]=new float[9];//保存旋轉(zhuǎn)數(shù)據(jù)的數(shù)組
        float values[]=new float[3];//保存方向數(shù)據(jù)的數(shù)組

        SensorManager.getRotationMatrix(R,null,accV,magV);//獲得一個包含旋轉(zhuǎn)矩陣的R數(shù)組
        SensorManager.getOrientation(R,values);//獲取方向值

        float x=(float)Math.toDegrees(values[1]);//x軸旋轉(zhuǎn)角度
        float y=(float)Math.toDegrees(values[1]);//y軸旋轉(zhuǎn)角度

        getPosition(x,y);
        super.postInvalidate();//刷新界面

    }

    public void getPosition(float X,float Y){

        //籃球位于水平中心是,xy的坐標(biāo)
        int x=(super.getWidth()-bubble.getWidth())/2;
        int y=(super.getHeight()-bubble.getHeight())/2;

        //控制球X軸的位置
        if(Math.abs(Y)<=Max){//如果Y軸的最大傾斜角度在最大角度之內(nèi)
            //根據(jù)Y軸的傾斜角度計(jì)算X坐標(biāo)的變化值(傾斜角度越大,X坐標(biāo)變化越大)
            int deltaX=(int)((super.getWidth()-bubble.getWidth())/2*Y/Max);
            x-=deltaX;
        }else if(Y>Max){//Y軸傾斜角度大于最大傾斜角度,小球在最左邊
            x=0;
        }else{//Y軸傾斜角度小于負(fù)的最大傾斜角度,小球在最右邊
            x=super.getWidth()-bubble.getWidth();
        }

        //控制球Y軸的位置

        if(Math.abs(X)<=Max){
            int deltaY=(int)((super.getHeight()-bubble.getHeight())/2*X/Max);
            y+=deltaY;
        }else if(X>Max){//x軸傾斜角度大于max籃球在最下邊
            y=super.getHeight()-bubble.getHeight();
        }else{//x軸的傾斜角度小于負(fù)的max,籃球在最上邊
         y=0;
        }
        //更新小球坐標(biāo)
        bx=x;
        by=y;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bubble,bx,by,new Paint());//繪制籃球
    }

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

    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容