如何捕获android中的触摸屏上的压力和区域

时间:2012-08-16 14:01:12

标签: android touchscreen touch-event motionevent

我正在使用Acer A500(电容屏)10英寸平板电脑和Android 3.2.1。我试图捕捉触摸屏幕的手指区域以及屏幕上的压力。我尝试过Motionevent。 getsize(),我总是得到一个值0.此外,Motionevent.getpressure()总是给我一个值1.

getX(),getY()和getEventTime()等其他函数似乎工作正常。

我错过了什么吗?还有什么我需要做的吗?

非常感谢任何帮助。

以下是代码:

package com.mygroup.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.util.Log;
import android.view.MotionEvent; 
import android.view.View;
import android.view.View.OnTouchListener;

public class MultiTouchTest extends Activity implements OnTouchListener{
StringBuilder builder = new StringBuilder();
TextView textView;
float[] x = new float[10];
float[] y = new float[10];
double[] p = new double[10];
float[] s = new float[10];
long[] t = new long[10];
boolean[] touched = new boolean[10];


private void updateTextView(){
    builder.setLength(0);
    for (int i = 0; i < 10; i++){
        builder.append(touched[i]);
        builder.append(", ");
        builder.append(x[i]);
        builder.append(", ");
        builder.append(y[i]);
        builder.append(", pressure = ");
        builder.append(p[i]);
        builder.append(", size = ");
        builder.append(s[i]);
        builder.append(", time = ");
        builder.append(t[i]);
        builder.append("\n");
    }
    textView.setText(builder.toString());
}

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    textView = new TextView(this);
    textView.setText("touch:");
    textView.setOnTouchListener(this);
    setContentView(textView);
}
//@Override

public boolean onTouch(View v, MotionEvent event){
    int action = event.getAction() & MotionEvent.ACTION_MASK;

    int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
    MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    int pointerId = event.getPointerId(pointerIndex);

    switch(action){
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
        touched[pointerId] = true;
        x[pointerId] = (int)event.getX(pointerIndex);
        y[pointerId] = (int)event.getY(pointerIndex);
        p[pointerId] = event.getPressure(pointerIndex);
        s[pointerId] = event.getSize(pointerIndex);
        t[pointerId] = event.getEventTime();
        break;
               ...
    }
    updateTextView();
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

可能未启用这些输入报告类型。您可以尝试在adb shell下运行此命令,它将打印触摸控制器支持的可能事件类型:

getevent -pl