2010-04-20 19 views
6

Mi chiedo se il supporto multi-touch per Android sia affidabile? Ho letto che soffre di alcuni problemi.Supporto multi-touch per Android

Mi chiedo anche, come posso definire i gesti multi-touch personalizzati? Come: 3 dita ruotano o 3 dita rimangono statiche e la quarta si muove.

Mi sono imbattuto in alcune risorse (Gestures o MotionEvent su developer.android.com) ma nulla lo afferma chiaramente.

saluti,

Zdenek

risposta

11

ho scavato in giro per l'API e trovato il modo di compiere gesti come pinch/pinch inversa, quindi credo che i gesti che descrivono sono possibili - ci vuole solo capire come codificarli. Di seguito ho incollato un esempio di un pizzico inverso che ho implementato. Volevo che il pizzico si registri solo se è orientato orizzontalmente. Non è un codice molto pulito o riutilizzabile, ma dovrebbe aiutarti ad andare avanti. Funziona su Android 2.0.x. Ho letto il multi-touch potrebbe avere problemi con le versioni precedenti. L'esempio è una classe che chiamo dall'interno di un'attività onTouchEvent, che inoltra l'evento alla classe.

public class HorizontalReversePinchDetector { 

    public boolean onTouchEvent(MotionEvent e) { 

     int pointerCount = e.getPointerCount(); 

     if(pointerCount != 2) { 
      Log.d(GESTURE, "not pinching - exactly 2 fingers are needed but have " + pointerCount); 
      clearPinch(); 
      return false; 
     } 

     int firstIndex = e.getX(0) < e.getX(1) ? 0: 1; 
     int secondIndex = e.getX(0) < e.getX(1) ? 1: 0; 

     Finger currentLeftFinger = new Finger(e.getX(firstIndex), e.getY(firstIndex)); 
     Finger currentRightFinger = new Finger(e.getX(secondIndex), e.getY(secondIndex)); 

     float yDifference = Math.abs(currentLeftFinger.getY() - currentRightFinger.getY()); 
     if(yDifference > 80) { 
      Log.d(GESTURE, "not pinching - fingers too vertically-oriented"); 
      clearPinch(); 
      return false; 
     } 

     if(initialLeftFinger == null) { 
      initialLeftFinger = currentLeftFinger; 
      initialRightFinger = currentRightFinger; 
      Log.d(GESTURE, "not pinching, but might be starting a pinch..."); 
      return false; 
     } 

     float leftFingerDistance = initialLeftFinger.getX() - currentLeftFinger.getX(); 
     float rightFingerDistance = currentRightFinger.getX() - initialRightFinger.getX(); 

     float xDistanceBetweenFingers = Math.abs(currentLeftFinger.getX() - currentRightFinger.getX()); 
     if(xDistanceBetweenFingers < minimumDistanceBetweenFingers) { 
      Log.d(GESTURE, "pinching, but fingers are not far enough apart..."); 
      return true; 
     } 

     if(leftFingerDistance < minimumDistanceForEachFinger) { 
      Log.d(GESTURE, "pinching, but left finger has not moved enough..."); 
      return true; 
     } 
     if(rightFingerDistance < minimumDistanceForEachFinger) { 
      Log.d(GESTURE, "pinching, but right finger has not moved enough..."); 
      return true; 
     } 

     pinchCompleted(); 
     return true; 
    } 

    private void pinchCompleted() { 
     Log.d(GESTURE, "pinch completed"); 
     if(pinchListener != null) pinchListener.onPinch(); 
     clearPinch(); 
    } 

    public static interface OnPinchListener { 
     void onPinch(); 
    } 

    private void clearPinch() { 
     initialLeftFinger = null; 
     initialRightFinger = null; 
    } 

    public void setPinchListener(OnPinchListener pinchListener) { 
     this.pinchListener = pinchListener; 
    } 

    private static class Finger { 

     private Finger(float x, float y) { 
      this.x = x; 
      this.y = y; 
     } 

     public float getX() { 
      return x; 
     } 

     public float getY() { 
      return y; 
     } 

     private float x; 
     private float y; 
    } 

    private Finger initialLeftFinger; 
    private Finger initialRightFinger; 
    private OnPinchListener pinchListener; 
    private static final float minimumDistanceForEachFinger = 30; 
    private static final float minimumDistanceBetweenFingers = 50; 
} 

Per quanto riguarda l'affidabilità, questo codice è stato completamente affidabile. Ancora una volta, solo su Android 2.0.x. Non l'ho testato su altre versioni della piattaforma.

+0

Ottimo! Grazie per la risposta, questa è proprio la cosa che stavo cercando. –