package com.hzhi.mouse_mb;
import java.text.DecimalFormat;
import android.os.Bundle;
...
public class MainActivity extends Activity{
...
// GestureDetector
public static GestureDetector gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = getApplicationContext();
// 获取SharedPreferences对象
sp = ctx.getSharedPreferences("SP", MODE_PRIVATE);
editor = sp.edit();
read_ip();
// 获得控件
get_con();
gd = new GestureDetector(ctx, new Mouse_GestureListener());
}
...
}
class Mouse_GestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
MainActivity.dis_x = e2.getX()-e1.getX();
MainActivity.dis_y = e2.getY()-e1.getY();
Log.i("**********************************", "onFling");
// 移动距离是否足够
if ((float)Math.pow((Math.pow(MainActivity.dis_x,2)+Math.pow(MainActivity.dis_y,2)),0.5)>MainActivity.dis_t){
MainActivity.dis_x *= MainActivity.move_times;
MainActivity.dis_y *= MainActivity.move_times;
MainActivity.send_thread.set_str(MainActivity.df.format(MainActivity.dis_x)+"/"+MainActivity.df.format(MainActivity.dis_y));
}
return false;
}
}
以上是代码,按理说手指在屏幕上移动时,应该Log.i("**********************************", "onFling");的,但是却没有反应,在public boolean onFling()中设置断点也根本执行不到断点去,说明根本就没有运行onFling(),这是怎么回事啊?
------解决方案--------------------
重写ontouch方法
return mGestureDetector.onTouchEvent(event);
------解决方案--------------------
1.重写ontouch方法
return gd.onTouchEvent(event);
2.如果有按钮,public boolean dispatchTouchEvent(MotionEvent ev) {