2015-04-30 9 views
7

Sto usando seguente codiceAumentare la visibilità di una vista sul Swipe utilizzando i gesti in Android

private GestureDetector.OnGestureListener mGestureListener = new GestureDetector.OnGestureListener() 
{ 
    private float lastDeltaValue; 
    private static final int MIN_DISTANCE = 15; 
    private static final int MIN_DISTANCE_Y = 25; 
    @Override 
    public boolean onDown(MotionEvent e) 
    { 
     lastDeltaValue = 0; 
     return false; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) { 

    } 

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

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 
    { 
     float startingX = (int)e1.getRawX(); 
     float startingY = (int)e1.getRawY(); 

     float endingX = (int)e2.getRawX(); 
     float endingY = (int)e2.getRawY(); 

     float deltaX = startingX - endingX; 
     float deltaY = startingY - endingY; 

     // swipe horizontal? 
     if(Math.abs(deltaX) > MIN_DISTANCE && Math.abs(deltaY) < MIN_DISTANCE_Y) 
     { 
      // left or right 
      if(deltaX > 0) 
      { 
       shouldCallBtn = SHOULD_CALL_WHERE; 
       if (gestureLayout.getVisibility() == View.GONE) 
       { 
        gestureLayout.setVisibility(View.VISIBLE); 
        gestureLayout.setAlpha(0.3f); 
       } 
       gestureText.setText(getString(R.string.option_where)); 
       if (deltaX > lastDeltaValue) 
       { 
        if (gestureLayout.getAlpha() > 0.29 && gestureLayout.getAlpha() < 0.80) 
         gestureLayout.setAlpha(gestureLayout.getAlpha() + 0.1f); 
       } 
       else 
       { 
        if (gestureLayout.getAlpha() < 0.81 && gestureLayout.getAlpha() > 0.29) 
         gestureLayout.setAlpha(gestureLayout.getAlpha() - 0.1f); 
       } 
       Log.d("DELTA VALUES", String.valueOf(deltaX) + " == " + String.valueOf(lastDeltaValue) + " " +String.valueOf(gestureLayout.getAlpha())); 

       lastDeltaValue = deltaX; 

      } 
      else if(deltaX < 0) 
      { 
       shouldCallBtn = SHOULD_CALL_ONMYWAY; 
       if (gestureLayout.getVisibility() == View.GONE) 
       { 
        gestureLayout.setVisibility(View.VISIBLE); 
        gestureLayout.setAlpha(0.3f); 
       } 
       gestureText.setText(getString(R.string.option_onway)); 
       if (deltaX > lastDeltaValue) 
       { 
        if (gestureLayout.getAlpha() > 0.29 && gestureLayout.getAlpha() < 0.80) 
         gestureLayout.setAlpha(gestureLayout.getAlpha() - 0.1f); 
       } 
       else 
       { 
        if (gestureLayout.getAlpha() < 0.81 && gestureLayout.getAlpha() > 0.29) 
         gestureLayout.setAlpha(gestureLayout.getAlpha() + 0.1f); 
       } 
       Log.d("DELTA VALUES", String.valueOf(deltaX) + " == " + String.valueOf(lastDeltaValue) + " " +String.valueOf(gestureLayout.getAlpha())); 

       lastDeltaValue = deltaX; 
      } 
     } 
     else 
     { 
     } 
     return false; 
    } 

    @Override 
    public void onLongPress(MotionEvent e) 
    { 
     gestureLayout.setVisibility(View.VISIBLE); 
     gestureLayout.setAlpha(0.8f); 
     gestureText.setText(getString(R.string.option_my_location)); 
     sendLocBtn.performClick(); 
    } 

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

Inizialmente GestureLayout visibilità è GONE. Dopo lo scorrimento la sua visibilità aumenterà da 0,3 a 0,8. Se provo a sinistra, scorri verso l'alto Aumenta l'alfa di uno View da qualche parte sullo schermo con un testo simile a (scorrimento a sinistra) e così fa sullo swipe destro.

Questo codice sembra funzionare ma l'animazione di alfa da basso ad alto non è conforme allo standard.

Qualsiasi aiuto sarebbe utile

NOTA: Io non ho bisogno di un'animazione. Voglio che sia basato sullo sfioramento del dito

+1

Ciao Muhammad. Quando provo a eseguire il codice, i seguenti simboli non sono definiti: "R.string.option_my_location'," R.string.option_onway', "R.string.option_where", "SHOULD_CALL_ONMYWAY'," SHOULD_CALL_WHERE', "gestureLayout', 'gestureText',' sendLocBtn' e 'shouldCallBtn'. Puoi fornire un esempio minimo, completo e verificabile (http://stackoverflow.com/help/mcve) per favore? Sarà quindi possibile riprodurre facilmente il problema che stai avendo, rendendo molto più facile fornire una buona risposta per te. –

+0

@MartinNordholts basta sostituire quelli con qualcosa o commentarli. Esegui una visualizzazione gestureLayout che è importante per aumentare o diminuire la sua visibilità –

+0

"Questo codice sembra funzionare ma l'animazione dell'alfa da bassa ad alta non è all'altezza dello standard." Qual è il problema? Vuoi che l'alfa sia animata mentre il colpo è in corso? – SharpEdge

risposta

4

Consente di chiamare il valore di 0.1fdeltaAlpha. deltaAlpha deve essere basato sul valore deltaX. È necessario definire un coefficiente per l'applicazione che rappresenti il ​​rapporto tra deltaX e deltaAlpha. Ad esempio, supponiamo che 10px equivalga a 0.01 alfa change. Ora sai che se il tuo dito percorre lo schermo 40px sullo schermo, il valore alfa sarà essere cambiato con 0.04 non importa della velocità del gesto. In questo modo l'animazione sarà liscia e si baserà sulla distanza che il dito ha spostato sullo schermo. Provate il seguente codice Java:

// 1 deltaX = 0.01 alpha 
// This is just an example coefficient. 
// Replace it with a value that fits your needs 
private static final float COEFFICIENT = 0.01; 

private float calculateDeltaAlpha(float deltaX) { 
    return deltaX * COEFICIENT; 
} 

private void incrementViewAlpha(View view, float distanceX) { 
    float oldAlpha = gestureLayout.getAlpha(); 
    if (oldAlpha > 0.29 && oldAlpha < 0.80) { 
    gestureLayout.setAlpha(oldAlpha + calculateAlphaDelta(distanceX)); 
    } 
} 

private void decrementViewAlpha(View view, float distanceX) { 
    float oldAlpha = gestureLayout.getAlpha(); 
    if (oldAlpha > 0.29 && oldAlpha < 0.80) { 
    gestureLayout.setAlpha(oldAlpha - calculateAlphaDelta(distanceX)); 
    } 
} 

chiamata incrementViewAlpha e decrementViewAlpha metodi quando si desidera aumentare o diminuire il valore alpha di vista.

+0

D'accordo con Kiril come la variazione alfa dovrebbe cambiare proporzionalmente con il movimento del dito per ottenere un effetto liscio. – Sameer

1

Ho migliorato il codice di Kiril ed evitato codice non necessario. È necessario modificare il valore COEFFICIENTE per controllare la variazione/uniformità in base alle dimensioni del layout, alle dimensioni dello schermo, ai parametri correlati, ecc. Fammi sapere se funziona per te. Sono state apportate anche modifiche per limitare l'alpha a UPPER_BOUND_ALPHA & LOWER_BOUND_ALPHA.

private static final float COEFFICIENT = 0.0001f; 
private static final float UPPER_BOUND_ALPHA = 0.8f; 
private static final float LOWER_BOUND_ALPHA = 0.3f; 

@Override 
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 
{ 
    float startingX = (int)e1.getRawX(); 
    float startingY = (int)e1.getRawY(); 

    float endingX = (int)e2.getRawX(); 
    float endingY = (int)e2.getRawY(); 

    float deltaX = startingX - endingX; 
    float deltaY = startingY - endingY; 

    // swipe horizontal? 
    if(Math.abs(deltaX) > MIN_DISTANCE && Math.abs(deltaY) < MIN_DISTANCE_Y) 
    { 
     if (gestureLayout.getVisibility() == View.GONE) 
     { 
      gestureLayout.setVisibility(View.VISIBLE); 
      gestureLayout.setAlpha(LOWER_BOUND_ALPHA); 
     } 

     // left or right 
     if(deltaX > 0) 
     { 
      shouldCallBtn = SHOULD_CALL_WHERE; 
      gestureText.setText(getString(R.string.option_where)); 
     } 
     else if(deltaX < 0) 
     { 
      shouldCallBtn = SHOULD_CALL_ONMYWAY; 
      gestureText.setText(getString(R.string.option_onway)); 
     } 

     float alphaValue = gestureLayout.getAlpha() + COEFFICIENT * deltaX; 
     if (alphaValue > UPPER_BOUND_ALPHA) 
     { 
      alphaValue = UPPER_BOUND_ALPHA; 
     } 
     else if (alphaValue < LOWER_BOUND_ALPHA) 
     { 
      alphaValue = LOWER_BOUND_ALPHA; 
     } 

     gestureLayout.setAlpha(alphaValue); 

     Log.d("DELTA VALUES", deltaX + " == " + lastDeltaValue + " " + gestureLayout.getAlpha()); 
     lastDeltaValue = deltaX; 
    } 

    return false; 
} 
Problemi correlati