我写了Gesture Detector class
并且没有尝试添加手势监听器,当我在onCreate()
视图的activity_main
函数中调用它时,它工作正常但我无法将其附加到通过按钮点击创建隐形视图..这是我的代码,
我没有收到错误,只是不听,我也可以在onTouchListener()
中设置invisibleView class
..
主
public class MainActivity extends Activity {
String TAG = "myTAG";
SocketClass mySocketClass;
View viewToWatch;
InvisibleView myInvisibleView;
Context myContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savedInstanceState = new Bundle();
myInvisibleView = new InvisibleView();
/*
-----------------------------------------------------
this works, when i add the listener to the main view:
-----------------------------------------------------
viewToWatch= (View) findViewById(R.id.mainViewId);
viewToWatch.setOnTouchListener(new ClassGesturesDetection() {
public void returnSingleTapUp() {
Log.d ("Gesture from main View", "single tab");
}
});
-----------------------------------------------------
*/
}
public void startApp(View activity_main_view){
myInvisibleView.onCreate(this.getApplicationContext());
putOnTochListenerToInvsibleView();
}
public void putOnTochListenerToInvsibleView( ) {
/*
-----------------------------------------------------
there i want to add the listener:
-----------------------------------------------------
*/
setContentView(R.layout.invisibleviewxml);
viewToWatch= (View) findViewById(R.id.invisibleViewId);
viewToWatch.setOnTouchListener(new ClassGesturesDetection() {
public void returnSingleTapUp() {
Log.d ("Gesture from transparent view", "single tab");
}
});
}
}
这里是我创建system overlay view
:
public class InvisibleView extends Service {
View myView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate(Context myContext) {
super.onCreate();
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = new View(myContext);
myView = inflater.inflate(R.layout.invisibleviewxml, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) myContext.getSystemService(WINDOW_SERVICE);
wm.addView(myView, params);
/*
-----------------------------------------------------
this also works,the system overlay catches clicks
-----------------------------------------------------
myView.setOnTouchListener( new OnTouchListener() {
@Override
public boolean onTouch(View inviView, MotionEvent event) {
Log.d("Gesture", "simple touch from InvisibleView Class");
return true;
}
});
-----------------------------------------------------
this does not work
-----------------------------------------------------
myView.setOnTouchListener(new ClassGesturesDetection() {
public void returnSingleTapUp() {
Log.d ("Gesture from transparent", "single tab");
}
});
----------------------------------------------------- */
}
@Override
public void onDestroy() {
super.onDestroy();
if(myView != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(myView);
myView = null;
}
}
}
这是我的手势系列:
public class ClassGesturesDetection implements OnTouchListener {
@SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
//Log.d("class Gesture", "on Down");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
returnOnLongPress();
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
returnSingleTapUp();
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
public void returnSingleTapUp() {
}
public void returnOnLongPress() {
}
}
答案 0 :(得分:0)
您必须添加此权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
您可以关注this 我已经回答了你的问题