Android 传感器管理器将球移动得太快

Android 传感器管理器将球移动得太快,android,bitmap,sensors,Android,Bitmap,Sensors,当传感器改变时,我在画布上移动一个位图球,但问题是,即使手机稳定,它也移动得太快。即使手机稳定,它也会在同一位置晃动。 我该如何降低传感器的速度来识别设备的运动呢?它甚至会给手机带来轻微的变化,所以有人能帮我吗?这将是一个很大的帮助 这是我的密码 public class Can extends SurfaceView implements Runnable, SensorEventListener { public Can(Context context) {

当传感器改变时,我在画布上移动一个位图球,但问题是,即使手机稳定,它也移动得太快。即使手机稳定,它也会在同一位置晃动。 我该如何降低传感器的速度来识别设备的运动呢?它甚至会给手机带来轻微的变化,所以有人能帮我吗?这将是一个很大的帮助 这是我的密码

public class Can extends SurfaceView implements Runnable,
        SensorEventListener {


    public Can(Context context) {
        // TODO Auto-generated constructor stub
        super(context);

        ourHolder = getHolder();
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
            Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
            Toast.makeText(getApplicationContext(), "registered", 500).show();
            sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
        }
        ball = BitmapFactory
                .decodeResource(getResources(), R.drawable.ball);
    }

    public void resume() {
        // TODO Auto-generated method stub
        isRunning = true;
        ourThread = new Thread(this);
        ourThread.start();
        w.acquire();
    }

    public void pause() {
        // TODO Auto-generated method stub
        isRunning = false;
        while (true) {
            try {
                ourThread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        ourThread = null;
        w.release();
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (isRunning) {
            if (!ourHolder.getSurface().isValid()) {
                continue;
            }
            Canvas canvas = ourHolder.lockCanvas();
            canvas.drawColor(Color.WHITE);
            Rect red = new Rect(0, canvas.getHeight() / 2,canvas.getWidth(), canvas.getHeight());
            Paint Pred = new Paint();
            Pred.setColor(Color.RED);
            canvas.drawRect(red, Pred);
            ballY = ((canvas.getHeight() / 2) - (ball.getHeight() / 2))+ sensorY*70  ;
            ballX = ((canvas.getWidth() / 2) - (ball.getWidth() / 2))+ sensorX*70 ;
            if(ballX>(canvas.getWidth()-ball.getWidth())){
                ballX=canvas.getWidth()-ball.getWidth();
            }
            if(ballX<0){
                ballX=0;
            }
            if(ballY<0){
                ballY=0;
            }
            if(ballY>(canvas.getHeight()-ball.getHeight())){
                ballY=canvas.getHeight()-ball.getHeight();
            }
            canvas.drawBitmap(ball, ballX, ballY, null);
            Paint Pblack = new Paint();
            Pblack.setColor(Color.BLACK);
            Paint Pwhite = new Paint();
            Pwhite.setColor(Color.WHITE);
            canvas.drawCircle(canvas.getWidth()/2, 0, ball.getWidth(), Pblack);
            canvas.drawCircle(canvas.getWidth()/2,canvas.getHeight(),ball.getWidth(), Pwhite);
            ourHolder.unlockCanvasAndPost(canvas);
        }
    }



    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        sensorY = event.values[0];
        sensorX = event.values[1];
        try {
            Thread.sleep(16);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
public类可以扩展SurfaceView实现Runnable,
SensorEventListener{
公共Can(上下文){
//TODO自动生成的构造函数存根
超级(上下文);
ourHolder=getHolder();
sm=(SensorManager)getSystemService(Context.SENSOR\u服务);
if(sm.getSensorList(Sensor.TYPE_Accelerator).size()!=0){
传感器s=sm.getSensorList(传感器类型\加速计).get(0);
Toast.makeText(getApplicationContext(),“registered”,500).show();
sm.registerListener(这个,s,SensorManager.SENSOR\u DELAY\u游戏);
}
ball=位图工厂
.decodeResource(getResources(),R.drawable.ball);
}
公众简历(){
//TODO自动生成的方法存根
isRunning=true;
ourThread=新线程(此线程);
ourThread.start();
w、 获得();
}
公共空间暂停(){
//TODO自动生成的方法存根
isRunning=false;
while(true){
试一试{
ourThread.join();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
打破
}
ourThread=null;
w、 释放();
}
@凌驾
公开募捐{
//TODO自动生成的方法存根
同时(正在运行){
如果(!ourHolder.getSurface().isValid()){
持续
}
Canvas Canvas=ourHolder.lockCanvas();
画布。drawColor(颜色。白色);
Rect red=new Rect(0,canvas.getHeight()/2,canvas.getWidth(),canvas.getHeight());
Paint Pred=新油漆();
前设置颜色(颜色为红色);
canvas.drawRect(红色,Pred);
ballY=((canvas.getHeight()/2)-(ball.getHeight()/2))+sensorical*70;
ballX=((canvas.getWidth()/2)-(ball.getWidth()/2))+sensorX*70;
if(ballX>(canvas.getWidth()-ball.getWidth()){
ballX=canvas.getWidth()-ball.getWidth();
}
如果(第1部分)
降低增益
目前,传感器值的灵敏度设置过多

ballY = ((canvas.getHeight() / 2) - (ball.getHeight() / 2))+ sensorY*70  ;
ballX = ((canvas.getWidth() / 2) - (ball.getWidth() / 2))+ sensorX*70 ;
这里的灵敏度是70,应该降低它以使其更稳定。最佳值应该通过使用数据的极值来确定。尝试从1缓慢增加它


修正2 稳定性过滤器
加速度计数据通常波动很大。使用低通滤波器稳定它可能是一个好主意

当你得到新的传感器数据时

// gain has some value between 0 and 1. For start, try gain = 0.5 
sensorX = gain * newSensorXValue + (1 - gain) * sensorX;
Y轴也是如此

希望这有帮助。
祝你好运