Android多点触控 - 不规则手指追踪

时间:2012-03-22 03:55:56

标签: java android multi-touch android-hardware

我设计了一个简单的多点触控系统,可以跟踪手指位置及其在屏幕上的状态。设计程序的方式,当两个手指放下时,根据放置顺序为它们分配一个数字。如果手指抬起,手指2仍称为手指2。

此代码在部分时间内完美运行。这意味着我将在测试中运行我的应用程序,有时每次都会进行多点触控,有时使用多个手指会使应用程序完全失败。它不规则可靠。

我很好奇这是硬件错误,还是我处理触摸输入的方式。来源可在下面找到。

感谢您提供的任何帮助!

-Nathan Tornquist

编辑:我认为问题可能与eventListeners的注册和取消注册方式有关。有时应用程序会崩溃,并在我重新打开它时再次完美运行。

EDIT2:问题在于程序的不规范程度。有时当我打开它时,它会完美地跟踪手指。放下的第一根手指保持1号,第二根手指保持2号等等。无论你是否在放置2后抬起手指1,数字都会保持分配状态。其他时候你会放两个手指,哪个手指是1,哪个是2个变化。应用程序似乎忘记了它们,当您将手指放在屏幕上时数字会切换。

EDIT3:我尝试过“触摸屏助推器”试图让屏幕始终正确响应。它没有解决问题。

EDIT4:经过更多测试后,这显然是代码错误。首次启动应用程序时,它始终完美运行。设备锁定后(当活动暂停时)然后解锁(当活动恢复时)多点触控停止工作,我得到一个适用于单点触控的应用程序,并且被多点触控困惑。

MultitouchGameFixActivity.java

package com.nathantornquist.multitouchgame;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MultiTouchGameFixActivity extends Activity{
    /** Called when the activity is first created. */
    MainGamePanel viewPanel;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Window state functions.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //This works without declaring a viewPanel instance here.
        //The instance declaration is needed to pass the 
        //onPause and onResume commands though.
        viewPanel = new MainGamePanel(this);
        setContentView(viewPanel);
    }

    //Restarts the accelerometer after onPause
    protected void onResume() {
        super.onResume();
        viewPanel.resume(this);

    }

    //Standard Method run when the Application loses focus.
    //This runs the pause() function in the viewPanel so that
    //the accelerometer can be paused.
    protected void onPause() {
        super.onPause();   
        viewPanel.pause();

    }

    protected void onDestroy() {
        super.onDestroy();
        viewPanel.destroy();
    }
}

MainThread.java

package com.nathantornquist.multitouchgame;

import com.nathantornquist.multitouchgame.MainGamePanel;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread {

private SurfaceHolder surfaceHolder;
private MainGamePanel gamePanel;
private boolean running;
public boolean pleaseWait = true;
public void setRunning(boolean running) {
    this.running = running;
}

public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
    super();
    this.surfaceHolder = surfaceHolder;
    this.gamePanel = gamePanel;
}

@Override
public void run() 
{
    Canvas canvas;
    while (running) {
        if(!pleaseWait) {
            canvas = null;
            // try locking the canvas for exclusive pixel editing on the surface
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    // update game state
                    this.gamePanel.update();

                    // draws the canvas on the panel
                    this.gamePanel.onDraw(canvas);
                }
            } finally {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   // end finally            
        }
        else {
            synchronized (this) {
                try {
                    wait();
                } catch (Exception e) { }
            }
        }
    }
}
}

MainGamePanel.java

package com.nathantornquist.multitouchgame;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainGamePanel extends SurfaceView implements SensorEventListener, SurfaceHolder.Callback 
{
    //Variable Declarations.
    private MainThread thread;

    public int screenWidth;
    public int screenHeight;

    private SensorManager mSensorManager;
    private Sensor mAccelerometer;    

    Paint paint;
    public int fingerOneDown;
    public int fingerTwoDown;
    public int fingerThreeDown;
    public int fingerFourDown;
    public float fingerOneX;
    public float fingerOneY;
    public float fingerTwoX;
    public float fingerTwoY;
    public float fingerThreeX;
    public float fingerThreeY;
    public float fingerFourX;
    public float fingerFourY;

    public MainGamePanel(Context context)
    {
        super(context);

        getHolder().addCallback(this);

        thread = new MainThread(getHolder(),this);

        paint = new Paint();
        paint.setAntiAlias(true);

        Display display = ((Activity) context).getWindowManager().getDefaultDisplay(); 
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();

        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);

        fingerOneDown = 0;
        fingerTwoDown = 0;
        fingerThreeDown = 0;
        fingerFourDown = 0;
        fingerOneX = 0;
        fingerOneY = 0;
        fingerTwoX = 0;
        fingerTwoY = 0;
        fingerThreeX = 0;
        fingerThreeY = 0;
        fingerFourX = 0;
        fingerFourY = 0;

        setFocusable(true);

        thread.setRunning(true);
        thread.start();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //continue the thread
        synchronized (thread) {
            thread.pleaseWait = false;
            thread.notifyAll();
        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        //pause the thread
        synchronized (thread) {
            thread.pleaseWait = true;
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        int pointerId = event.getPointerId(pointerIndex);
        switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:
            Log.d("pointer id - down",Integer.toString(pointerId));
            if (pointerId == 0)
            {
                fingerOneDown = 1;
                fingerOneX = event.getX(pointerIndex);
                fingerOneY = event.getY(pointerIndex);
            }
            if (pointerId == 1)
            {
                fingerTwoDown = 1;
                fingerTwoX = event.getX(pointerIndex);
                fingerTwoY = event.getY(pointerIndex);
            }
            if(pointerId == 2)
            {
                fingerThreeDown = 1;
                fingerThreeX = event.getX(pointerIndex);
                fingerThreeY = event.getY(pointerIndex);
            }
            if(pointerId == 3)
            {
                fingerFourDown = 1;
                fingerFourX = event.getX(pointerIndex);
                fingerFourY = event.getY(pointerIndex);
            }
            break;

        case MotionEvent.ACTION_UP:          
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL:
            Log.d("pointer id - cancel",Integer.toString(pointerId));
            if (pointerId == 0)
            {
                fingerOneDown = 0;
                fingerOneX = event.getX(pointerIndex);
                fingerOneY = event.getY(pointerIndex);
            }
            if (pointerId == 1)
            {
                fingerTwoDown = 0;
                fingerTwoX = event.getX(pointerIndex);
                fingerTwoY = event.getY(pointerIndex);
            }
            if(pointerId == 2)
            {
                fingerThreeDown = 0;
                fingerThreeX = event.getX(pointerIndex);
                fingerThreeY = event.getY(pointerIndex);
            }
            if(pointerId == 3)
            {
                fingerFourDown = 0;
                fingerFourX = event.getX(pointerIndex);
                fingerFourY = event.getY(pointerIndex);
            }
            break;

        case MotionEvent.ACTION_MOVE:

            int pointerCount = event.getPointerCount();
            for(int i = 0; i < pointerCount; ++i)
            {
                pointerIndex = i;
                pointerId = event.getPointerId(pointerIndex);
                Log.d("pointer id - move",Integer.toString(pointerId));
                if(pointerId == 0)
                {
                    fingerOneDown = 1;
                    fingerOneX = event.getX(pointerIndex);
                    fingerOneY = event.getY(pointerIndex);
                }
                if(pointerId == 1)
                {
                    fingerTwoDown = 1;
                    fingerTwoX = event.getX(pointerIndex);
                    fingerTwoY = event.getY(pointerIndex);
                }
                if(pointerId == 2)
                {
                    fingerThreeDown = 1;
                    fingerThreeX = event.getX(pointerIndex);
                    fingerThreeY = event.getY(pointerIndex);
                }
                if(pointerId == 3)
                {
                    fingerFourDown = 1;
                    fingerFourX = event.getX(pointerIndex);
                    fingerFourY = event.getY(pointerIndex);
                }
            }
            break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) 
    {        
        paint.setColor(Color.WHITE); 
        paint.setStyle(Style.FILL); 
        canvas.drawPaint(paint); 

        if (fingerOneDown == 1)
        {
            paint.setColor(Color.BLUE); 
            paint.setTextSize(20); 
            canvas.drawText("1", fingerOneX, fingerOneY - 30, paint); 
        }

        if (fingerTwoDown == 1)
        {
            paint.setColor(Color.RED); 
            paint.setTextSize(20); 
            canvas.drawText("2", fingerTwoX, fingerTwoY - 30, paint); 
        }
        if (fingerThreeDown == 1)
        {
            paint.setColor(Color.GREEN); 
            paint.setTextSize(20); 
            canvas.drawText("3", fingerThreeX, fingerThreeY - 30, paint); 
        }
        if (fingerFourDown == 1)
        {
            paint.setColor(Color.BLACK); 
            paint.setTextSize(20); 
            canvas.drawText("4", fingerFourX, fingerFourY - 30, paint); 
        }
    }

    public void update() {
    }

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

    @Override
    public void onSensorChanged(SensorEvent event) {

    }

    public void pause() {
        mSensorManager.unregisterListener(this);
    }

    public void resume(Context context) {
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);      
    }

    public void destroy() {
        thread.setRunning(false);

        if (thread != null)
        {
            Thread killThread = thread;
            thread = null;
            killThread.interrupt();
        }   

    }


}

2 个答案:

答案 0 :(得分:0)

我会抛出一些想法,即使我的安卓技能有点缺乏......

你是如何测试的?在一个真实的设备上我猜?可以在模拟器上测试吗?看起来它很容易出现硬件问题。

如果有一个小故障,屏幕即使只是一个毫秒也会失去手指的轨迹,不会因为手指的索引而重新开始吗?如果是这种情况,它会非常随机地对手指进行编号,从而导致您遇到的问题。

一些想法:

  • 不要跟踪手指的索引,只需保留手指列表及其相对位置。但我想部分游戏需要你保留每个手指的索引。
  • 如果您松开与手指的连接,请记录,这将显示这是否是一个isse。我想MotionEvent.ACTION_POINTER_UP是您应该记录的事件。事实上,我为每个可能的事件添加一个日志,只是为了看看发生了什么。
  • 添加一种方法,试图确定哪个手指已放置在屏幕上,即它将手指的当前位置与先前手指的已知值进行比较,如果它在阈值内,则假设其手指相同。可能想要添加一个计时器来忘记&#34;以前的手指也是如此。

我想您一直在查看Android developer blog上的多点触控示例,以确保您正确使用这些API?

希望它可以给你一些帮助。

答案 1 :(得分:0)

通过禁用WidgetLocker解决了该问题。该应用程序似乎偶尔搞乱一些程序。当我在玩游戏时锁定屏幕时,我也注意到了其他应用程序中的问题。