我实现了计算黑色画布(画布)上的坐标、压力、方向、手指大小等的程序。但我需要在每个应用程序之上计算这些。那是在 PDF、网络浏览器等之上,即它应该作为后台进程运行。它必须捕获屏幕的每个触摸实例。我应该将屏幕绑定到画布。
公共类 CaptureEventsActivity 扩展 Activity 实现 OnTouchListener {
String root = android.os.Environment.getExternalStorageDirectory().toString();
File mydir = new File(root + "/saved_texts");
File file1 = new File(mydir,"Research.xls");
FileOutputStream out_stream;
FileOutputStream out_stream1;
Float azimut;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.BOTTOM;
WindowManager wm1 = (WindowManager) getSystemService(WINDOW_SERVICE);
BlankCanvas bcan = new BlankCanvas(this,null);
this.setContentView(bcan);
bcan.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("NewApi")
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
BlankCanvas bcn = (BlankCanvas) arg0;
int pointerIndex = event.getActionIndex();
int pointerId = event.getPointerId(pointerIndex);
int maskedAction = event.getActionMasked();
switch(maskedAction) {
case MotionEvent.ACTION_DOWN:
{
//EventData eventData = new EventData();
handler.postDelayed(CaptureEventsActivity.this.mLongPressed, 1000);
GesturePoint gp = new GesturePoint(event.getX(),event.getY(),System.currentTimeMillis());
aa.add(gp);
//bcn.path.moveTo(event.getX(), event.getY());
touchDirection = getTouchDirection(event.getX(), event.getY());
String strD = String.valueOf((int)event.getX()) +'\t'+ String.valueOf((int)event.getY()) +'\t'+ String.valueOf(event.getOrientation())+'\t' +event.getPressure()+'\t'+event.getEventTime()+'\t'+"ACTION_DOWN"+'\t'+event.getSize()+'\t'+ touchDirection+'\t'+'\n';
try {
out_stream.write(strD.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;}
case MotionEvent.ACTION_MOVE:
{
if(event.getPointerCount()>1)
{
for (int size = event.getPointerCount(), i = 0; i < size; i++) {
PointF point = mActivePointers.get(event.getPointerId(i));
if (point != null) {
//bcn.path.lineTo(event.getX(i), event.getY(i));
String x="";
//if(i==1)
x="finger:"+i;
//touchDirection = getTouchDirection(event.getX(i), event.getY(i));
String strM = String.valueOf((int)event.getX(i)) +'\t'+ String.valueOf((int)event.getY(i)) +'\t'+ String.valueOf(event.getOrientation(i))+'\t' +event.getPressure(i)+'\t'+event.getEventTime()+'\t'+"ACTION_MOVE"+'\t'+event.getSize(i)+'\t'+ touchDirection+'\t'+x+'\n';
try {
out_stream.write(strM.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
else
{
handler.removeCallbacks(CaptureEventsActivity.this.mLongPressed);
//bcn.path.lineTo(event.getX(), event.getY());
//touchDirection = getTouchDirection(event.getX(i), event.getY(i));
String strM = String.valueOf((int)event.getX()) +'\t'+ String.valueOf((int)event.getY()) +'\t'+ String.valueOf(event.getOrientation())+'\t' +event.getPressure()+'\t'+event.getEventTime()+'\t'+"ACTION_MOVE"+'\t'+event.getSize()+'\t'+ touchDirection+'\t'+'\n';
try {
out_stream.write(strM.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
case MotionEvent.ACTION_POINTER_DOWN:
{
PointF f = new PointF();
handler.postDelayed(CaptureEventsActivity.this.mLongPressed, 1000);
GesturePoint gp = new GesturePoint(event.getX(),event.getY(),System.currentTimeMillis());
aa.add(gp);
f.x = event.getX(pointerIndex);
f.y = event.getY(pointerIndex);
// bcn.path.moveTo(event.getX(), event.getY());
touchDirection = getTouchDirection(event.getX(), event.getY());
String strA = String.valueOf((int)f.x) +'\t'+ String.valueOf((int)f.y) +'\t'+ String.valueOf(event.getOrientation())+'\t' +event.getPressure()+'\t'+event.getEventTime()+'\t'+"ACTION_POINTER_DOWN"+'\t'+event.getSize()+'\t'+ touchDirection+'\t'+'\n';
mActivePointers.put(pointerId, f);
try {
out_stream.write(strA.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case MotionEvent.ACTION_POINTER_UP:
{
PointF f = new PointF();
handler.postDelayed(CaptureEventsActivity.this.mLongPressed, 1000);
GesturePoint gp = new GesturePoint(event.getX(),event.getY(),System.currentTimeMillis());
aa.add(gp);
f.x = event.getX(pointerIndex);
f.y = event.getY(pointerIndex);
mActivePointers.put(pointerId, f);
// bcn.path.moveTo(event.getX(), event.getY());
touchDirection = getTouchDirection(event.getX(), event.getY());
String strU = String.valueOf((int)event.getX()) +'\t'+ String.valueOf((int)event.getY()) +'\t'+ String.valueOf(event.getOrientation())+'\t' +event.getPressure()+'\t'+event.getEventTime()+'\t'+"ACTION_POINTER_UP"+'\t'+event.getSize()+'\t'+ touchDirection+'\t'+'\n';
try {
out_stream.write(strU.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case MotionEvent.ACTION_UP:
{ handler.removeCallbacks(CaptureEventsActivity.this.mLongPressed);
GesturePoint gp = new GesturePoint(event.getX(),event.getY(),System.currentTimeMillis());
aa.add(gp);
GesturePoint gp1= new GesturePoint(event.getOrientation(),event.getPressure(),event.getAction());
aa.add(gp1);
//GestureStroke gs = new GestureStroke(aa);
touchDirection = getTouchDirection(event.getX(), event.getY());
String strU = String.valueOf((int)event.getX()) +'\t'+ String.valueOf((int)event.getY()) +'\t'+ String.valueOf(event.getOrientation())+'\t' +event.getPressure()+'\t'+event.getEventTime()+'\t'+"ACTION_UP"+'\t'+event.getSize()+'\t'+ touchDirection+'\t'+'\n';
try {
out_stream.write(strU.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
aa.clear();
}
break;
default:
/*String x =String.valueOf(bcn.getX())+'\n';
try {
out_stream.write(x.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
return false;
}
bcn.invalidate();
return true;
}
});
//}
//wm1.addView(bcan, params);
bcan.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Log.e("CaptureEvent", "in long preess...");
long_clicks++;
return true;
}
});
}
//@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
try {
//String w_s = "No.of strikes..." + String.valueOf(no_of_strikes) + '\n';
//out_stream.write(w_s.getBytes());
//w_s = "No.of Long Clicks..."+String.valueOf(long_clicks) + '\n';
//out_stream.write(w_s.getBytes());
out_stream.flush();
out_stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getTouchDirection (float eventX, float eventY) {
String direction = "";
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics( metrics );
float centreX = 800; /// metrics.widthPixels * eventX;
float centreY = 480; /// metrics.heightPixels * eventY;
// float centreX = 400/2, centreY = 700/2;
float tx = (float) (eventX - centreX), ty = (float) (eventY - centreY);
float radius = (float) Math.sqrt(tx*tx + ty*ty);
float offsetX = 0, offsetY = radius, adjEventX = eventX - centreX, adjEventY = centreY - eventY;
double cosaU = ((offsetX * adjEventX) + (offsetY * adjEventY));
double cosaD = ( Math.sqrt((offsetX * offsetX) + (offsetY * offsetY)) * Math.sqrt((adjEventX * adjEventX) + (adjEventY * adjEventY)));
double cosa = cosaU / cosaD;
double degr = ( Math.acos(cosa) * (180 / Math.PI));
if (adjEventX < 0)
degr = 360 - degr;
float offsetDegrees = (float) (degrees + degr);
if (offsetDegrees > 360)
offsetDegrees = offsetDegrees - 360;
if (offsetDegrees < 22.5 || offsetDegrees > 336.5)
direction = "NORTH";
else if (offsetDegrees > 22.5 && offsetDegrees < 67.5)
direction = "NORTHEAST";
else if (offsetDegrees > 67.5 && offsetDegrees < 112.5)
direction = "EAST";
else if (offsetDegrees > 112.5 && offsetDegrees < 156.5)
direction = "SOUTHEAST";
else if (offsetDegrees > 156.5 && offsetDegrees < 201.5)
direction = "SOUTH";
else if (offsetDegrees > 201.5 && offsetDegrees < 246.5)
direction = "SOUTHWEST";
else if (offsetDegrees > 246.5 && offsetDegrees < 291.5)
direction = "WEST";
else if (offsetDegrees > 291.5 && offsetDegrees < 336.5)
direction = "NORTHWEST";
return direction;
}
}
所以当我调用 wm1.addView(bcan, params); 时使用 WindowManager.LayoutParams; 它抛出一个错误