2011-12-26 72 views
2

我有以下代碼,基本上在窗口上繪製一個透明的矩形。 我想攔截這個矩形的觸摸事件,而不是將事件發送到下面的窗口。 我嘗試過onInterceptTouchEvent,onTouchEvent,但是當我在矩形內部單擊時,矩形後面的窗口接收到了觸摸。如何在覆蓋窗口上獲得觸摸事件?

public class TestService extends Service { 
    HUDView mView; 
    int ScreenHeight; 
    int ScreenWidth; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     try { 
     super.onCreate(); 
     WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
     ScreenHeight=wm.getDefaultDisplay().getHeight(); 
     ScreenWidth=wm.getDefaultDisplay().getWidth(); 

     mView = new HUDView(this,ScreenHeight,ScreenWidth); 
     WindowManager.LayoutParams params = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 

       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
      // | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 
       | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 
         | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 
         | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR 
         , 
       PixelFormat.TRANSLUCENT); 
     //params.gravity = Gravity.RIGHT | Gravity.TOP; 
     wm.addView(mView, params); 

     } 
     catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 



    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show(); 
     if(mView != null) 
     { 
      ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView); 
      mView = null; 
     } 
    } 
} 

class HUDView extends ViewGroup { 
    private Paint mLoadPaint; 
    private int ScreenHeight; 
    private int ScreenWidth; 


    public HUDView(Context context, int WH, int WW) { 
     super(context); 
     ScreenHeight=WH; 
     ScreenWidth=WW; 
     // Toast.makeText(getContext(),"HUDView", Toast.LENGTH_LONG).show(); 

     mLoadPaint = new Paint(); 
     mLoadPaint.setARGB(100, 0, 0, 0); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     canvas.drawRect(0,0, 100, 100, mLoadPaint); 
     //canvas.drawText("Hello World", 5, 15, mLoadPaint); 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
//if (event.getAction() == MotionEvent.ACTION_DOWN) { 
    // Toast.makeText(getContext(), "onInterceptTouchEvent : " + event.getAction(),Toast.LENGTH_LONG); 
//} 
     Log.d("bThere", "X: " + (int)event.getX() + " Y: " + (int)event.getY()); 
     return true; 

    } 


    @Override 
    public boolean onInterceptTouchEvent(MotionEvent event) { 
     //super.onInterceptTouchEvent(event); 
     Toast.makeText(getContext(), "onInterceptTouchEvent : " + event.getAction(),Toast.LENGTH_LONG); 
     return true; 
    } 


    @Override 
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) { 
    } 

} 
+0

你最後做了什麼? – Anand 2015-09-16 18:01:28

回答

0

嘗試設置focus到您要處理的觸摸view

+0

這沒有幫助,還有什麼建議嗎? – 2013-09-01 10:01:19

0

這裏是AOSP的PhoneWindowManager.java

@Override 
public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) { 
    switch (attrs.type) { 
     case TYPE_SYSTEM_OVERLAY: 
     case TYPE_SECURE_SYSTEM_OVERLAY: 
      // These types of windows can't receive input events. 
      attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; 
      attrs.flags &= ~WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; 
      break; 
.... 
} 

代碼所以基本上,如果你的窗口的類型是TYPE_SYSTEM_OVERLAY,系統將自動添加標誌WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE。因此,您的窗口將不會收到任何觸摸事件。

相關問題