磁场传感器

作者: 被罚站的树 | 来源:发表于2019-06-30 15:19 被阅读0次
  <com.lindingdu.app15.PointerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
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.view.View;

public class PointerView extends View implements SensorEventListener {
    private SensorManager sensorManager;//传感器管理器
    private Bitmap pointer=null;//定义指针的位图对象
    private float[] allvalue;//磁场传感器的值

    public PointerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获取传感器管理器
        sensorManager= (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        //为传感器注册监听器
        sensorManager.registerListener(this
                ,sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
                ,sensorManager.SENSOR_DELAY_GAME);

        pointer= BitmapFactory.decodeResource(super.getResources(),R.drawable.pointer);//指定要绘制的指针位图

    }

    @Override
    public void onSensorChanged(SensorEvent event) {//传感器值改变时触发

        if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){
            float[] value=event.values;//获取磁场感应器的值
            allvalue=value;
            super.postInvalidate();
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {//传感器精度改变时触发

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //根据xy轴磁场强度绘制指针
        if(allvalue!=null){
            float x=allvalue[0];
            float y=allvalue[1];
            canvas.save();
            canvas.restore();//重置绘图对象
            canvas.translate(super.getWidth()/2,super.getHeight()/2);//设置旋转的中心点为屏幕的中心点
            if(y==0&x>0){//正东方
                canvas.rotate(90);//旋转90度
            }else if(y==0&x<0){//正西方
                canvas.rotate(270);
            }else{
             if(y>=0){
                canvas.rotate((float) Math.tanh(x/y)*90);
             }else {
                 canvas.rotate(180+(float) Math.tanh(x/y)*90);
                 }
            }
        }
        canvas.drawBitmap(this.pointer,-this.pointer.getWidth()/2,-this.pointer.getHeight()/2
                ,new Paint());//绘制指针

    }
}

相关文章

  • Android 传感器开发详解

    Android 传感器开发详解 传感器 传感器的分类 方向传感器 陀螺仪传感器 磁场传感器 重力传感器 线性加速度...

  • 磁场传感器

  • 2017-12-22

    今天我们学习了霍尔传感器,霍尔传感器是根据霍尔效应制作的一种磁场传感器,通过霍尔效应实验测定的霍尔系数,能够判断半...

  • 更新记录(20200410)

    3.27.04101、增加会员组件“高德导航”,调用在线的高德路线规划API2、“传感器”增加磁场传感器组件,检测...

  • 2017年12月22日学习总结

    今天黄老师给大家讲了霍尔传感器 霍尔传感器是根据霍尔效应制作的一种磁场传感器。霍尔效应是磁电效应的一种,当电流...

  • 12.22

    今天黄老师给大家讲了霍尔传感器 霍尔传感器是根据霍尔效应制作的一种磁场传感器。霍尔效应是磁电效应的一种,当电流...

  • Android开发(27) 做个指南针应用

    概述 现在一般android手机里都有 磁场传感器,它能检测到方向。我们做个指南针应用玩玩。 思路 1.获得传感器...

  • Android简易指南针的实现(Kotlin)

    概述 1、现在的Android手机都会有加速度传感器和磁场传感器,指南针实际上就是利用这两个传感器计算出现在所指向...

  • 【开地电子】直线位移传感器在注塑机的应用

    电感式直线位移传感器采用新的检测原理,不但继承了磁致伸缩直线位移传感器的众多优势,而且在抗磁场干扰、抗振动性、外型...

  • 树莓派基础实验16:霍尔传感器实验

    一、介绍    霍尔传感器是根据霍尔效应制作的一种磁场传感器。霍尔效应是磁电效应的一种,这一现象是霍尔(A.H.H...

网友评论

    本文标题:磁场传感器

    本文链接:https://www.haomeiwen.com/subject/wnrbcctx.html