我在从 View 扩展的类中实现了以下 onTouchEvent() 函数:
public boolean onTouchEvent(MotionEvent event){
// Log.d("MYLOG","TOUCH EVENT OCCURED");
int i, pointToTrack = 0;
boolean trackAPoint = false;
float temp = 0.0f;
float x = event.getX();
float y = event.getY();
// Log.d("MYLOG","TOUCH AT ("+Float.toString(x)+","+Float.toString(y)+")");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Toast.makeText(getApplicationContext(), "ACT_DOWN", Toast.LENGTH_SHORT).show();
// Log.d("MYLOG", "ACT_DOWN");
for(i=0; i<NUM_OF_GRAPH_POINTS; i++){
// if(graphPoints[i][0] == y){
if(Math.abs(graphPoints[i][0] - y)<10.0f ){
pointToTrack = i;
trackAPoint = true;
Log.d("MYLOG", "PRESSED PNT = "+Float.toString(y));
}
}
break;
case MotionEvent.ACTION_MOVE:
// Toast.makeText(getApplicationContext(), "ACT_MOVE", Toast.LENGTH_SHORT).show();
// Log.d("MYLOG", "ACT_MOVE");
/*
if(trackAPoint == true){
graphPoints[pointToTrack][1] = y;
}
*/
temp = temp + 10.0f;
Log.d("MYLOG", "temp="+Float.toString(temp));
receiveGainValues(temp);
break;
case MotionEvent.ACTION_UP:
// Toast.makeText(getApplicationContext(), "ACT_UP", Toast.LENGTH_SHORT).show();
// Log.d("MYLOG", "ACT_UP");
trackAPoint = false;
break;
}
// invalidate();
postInvalidate();
return true;
}
请特别查看此部分:
case MotionEvent.ACTION_MOVE:
// Toast.makeText(getApplicationContext(), "ACT_MOVE", Toast.LENGTH_SHORT).show();
// Log.d("MYLOG", "ACT_MOVE");
/*
if(trackAPoint == true){
graphPoints[pointToTrack][1] = y;
}
*/
temp = temp + 10.0f;
Log.d("MYLOG", "temp="+Float.toString(temp));
receiveGainValues(temp);
break;
当我将手指移到平板电脑表面时,logcat 多次显示 temp = 10。但是,既然它正在检测运动,那么只要检测到运动,温度不应该继续增加吗?为什么它会增加一次,然后停止,而它下面的 Log.d() 却一直被调用?
如果相关,平板电脑是带有 Android 4.3 和 API 级别 18 的 nexus 7