2013-02-18 11 views
8

Mi piace sviluppare un'applicazione simile a Swapps. Sto cercando di visualizzare un pulsante in cima a qualsiasi schermata e ciò dovrebbe verificarsi solo con un'azione di scorrimento. Ho provato con il codice seguenteImpossibile ottenere l'evento di tocco per TYPE_SYSTEM_OVERLAY

WindowManager.LayoutParams params = new WindowManager.LayoutParams(); 
    params.type =WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; 
    params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; 
    params.format =PixelFormat.TRANSLUCENT; 
    params.height = WindowManager.LayoutParams.WRAP_CONTENT; 
    params.width = WindowManager.LayoutParams.WRAP_CONTENT; 
    parentlay = new LinearLayout(this); 
    mybt = new Button(getApplicationContext()); 
    bt.setText("Click to Go"); 

    parentlay.setBackgroundColor(Color.TRANSPARENT); 
    parentlay.setHapticFeedbackEnabled(true); 
    parentlay.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
       // event.getAction() prints 4 and not able to get up or down event 
      } 
     }); 
    windowmanager.addView(parentlay, params); 

sto cercando di visualizzare il pulsante quando l'utente fa scorrere da sinistra a destra catturando quell'evento e facendo

parentlay.addView(bt); 

e togliere la vista quando l'utente fa scorrere da destra a sinistra per

Come accennato, non ho ricevuto gli eventi di movimento a meno che non avessi cambiato il tipo come TYPE_SYSTEM_ALERT, ma che l'animazione blocca tutto lo schermo e non è possibile fare clic sul pulsante Indietro o su Home, c'è un modo alternativo per farlo.

Ho appena provato sotto la modifica

params.type =WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; 
    params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
    |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH 
    |WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; 

    params.height = WindowManager.LayoutParams.MATCH_PARENT; 
    params.width = WindowManager.LayoutParams.MATCH_PARENT; 

Ma non ho potuto fare clic sul pulsante sottostante, può essere la sua causa di MATCH_PARTENT ma se ho impostato a wrap_content allora non posso ottenere cliccare eventi.

risposta

8

Per nascondere la vista è necessario chiamare mWindowManager.removeView (mOverlay);

private RelativeLayout mOverlay; 
     private WindowManager mWindowManager; 
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
         WindowManager.LayoutParams.WRAP_CONTENT, 
         WindowManager.LayoutParams.WRAP_CONTENT, 
         WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 
         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
           | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 
         PixelFormat.TRANSLUCENT); 
       params.gravity = Gravity.TOP; 
       LayoutInflater inflater = (LayoutInflater) context 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       mOverlay = (RelativeLayout) inflater 
         .inflate(R.layout.phasechange, null); 

       mWindowManager = (WindowManager) context 
         .getSystemService(Context.WINDOW_SERVICE); 
       mWindowManager.addView(mOverlay, params); 
       Button b = (Button) mOverlay.findViewById(R.id.button1); 
       b.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         mWindowManager.removeView(mOverlay); 
        } 
       }); 

      } 
+0

Grazie per la tua risposta, ho bisogno di aggiungere la vista con l'azione di scorrimento o qualsiasi evento di azione, come quello che ha fatto swapps. Non ho avviare la mia app.e rimuovere la vista da un evento di azione – ganesh

+0

ok per ora io può accettare la risposta di DjHacktor quindi è abbastanza vicino alle mie esigenze – ganesh

+0

Grazie mille! – kord

1

L'approccio a questo compito è molto insolito. Semplicemente aggiungendo una nuova vista si gestiva bene l'attività.

aggiungere questo codice:

RelativeLayout layout = new RelativeLayout(this); 
Button b = new Button(this); 
b.setBackgroundColor(Color.LTGRAY); 
b.setText("Click to Go"); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.CENTER_IN_PARENT); 
b.setLayoutParams(params); 
layout.addView(b); 

//get a reference to your main layout: 
ViewGroup myLayout = findViewById(R.id.whateverMayLayoutIdIs); 
layout.addView(layout); 

Per nascondere la vista, si può semplicemente rimuovere dal layout, o cambia la sua visibilità.

L'approccio su cui stavate lavorando è molto complicato e altri hanno avuto problemi con la gestione dei tocchi. Gli esempi includono Android : Multi touch and TYPE_SYSTEM_OVERLAY e GestureDetector within a TYPE_SYSTEM_OVERLAY. Prova this google search per altro.

1

Fare vista dal Codice è sempre frenetica e un po 'difficile da gestire. in alternativa, puoi aggiungere il pulsante nel layout e renderlo visibile e invisibile in base ai tuoi scatti.

e utilizzare sotto codice per ottenere i gesti

public class MyGestureDetector implements OnGestureListener { 
     @Override 
     public boolean onDown(MotionEvent arg0) { 
      return false; 
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
       float arg3) { 
      return setCurrentSwipeTab(e1, e2, velocityX, arg3); 
     } 

     @Override 
     public void onLongPress(MotionEvent arg0) { 
     } 

     @Override 
     public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, 
       float arg3) { 
      return false; 
     } 

     @Override 
     public void onShowPress(MotionEvent e) { 
     } 

     @Override 
     public boolean onSingleTapUp(MotionEvent e) { 
      return false; 
     } 
    } 


public boolean setCurrentSwipeTab(MotionEvent e1, MotionEvent e2, 
      float velocityX, float arg3) { 
     try { 
      if (Math.abs(e1.getY() - e2.getY()) > Util.SWIPE_MAX_OFF_PATH) 
       return false; 
      // Left to right swipe of the user detected 
      if (e1.getX() - e2.getX() > Util.SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > Util.SWIPE_THRESHOLD_VELOCITY) { 

       } 
        // Nothing to do since you are at the Last tab!! it's task 
        // of the left swipe now 
       } 
      } else if (e2.getX() - e1.getX() > Util.SWIPE_MIN_DISTANCE 
        && Math.abs(velocityX) > Util.SWIPE_THRESHOLD_VELOCITY) { 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return false; 
    } 
1

Usa finestra di tipo TYPE_PHONE al posto di TYPE_SYSTEM_OVERLAY, insieme con la bandiera FLAG_NOT_FOCUSABLE.

Problemi correlati