2011-07-16 10 views
4

私はイメージビューを持っています。私はダブルタップとシングルレットで異なる速度でアニメーションを開始する必要があります。ダブルタップのスピードは速く、シングルレットのスピードは通常のスピードに戻ります。imageviewのonclickでダブルタップを実装する方法は?

イメージビューのonclickでジェスチャを実装するにはどうすればよいですか?

答えて

0

アクティビティでGestureDetector.SimpleOnGestureListenerを拡張する必要があります。次に、onDoubleTapイベントを取得します。詳細についてはこちらのページの上に同じ記事を参照できます。

DoubleTap in android

Android: How to detect double-tap?

5

あなたはジェスチャーリスナーを実装することが可能のように例えばダブルタップをキャッチするために:

public class MyView extends ImageView { 

    GestureDetector gestureDetector; 

    public MyView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // creating new gesture detector 
     gestureDetector = new GestureDetector(context, new GestureListener()); 
    } 

    // skipping measure calculation and drawing 

    // delegate the event to the gesture detector 
    @Override 
    public boolean onTouchEvent(MotionEvent e) { 
     return gestureDetector.onTouchEvent(e); 
    } 


    private class GestureListener extends GestureDetector.SimpleOnGestureListener { 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 
     // event when double tap occurs 
     @Override 
     public boolean onDoubleTap(MotionEvent e) { 
      float x = e.getX(); 
      float y = e.getY(); 

      Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")"); 

      return true; 
     } 
    } 
} 
9

あなたの場合カスタム画像のビューを希望しない場合は、次の方法を使用できます。

// class level 

GestureDetector gestureDetector; 
boolean tapped; 
ImageView imageView; 

// inside onCreate of Activity or Fragment 
gestureDetector = new GestureDetector(getActivity(),new GestureListener()); 

// --------------------------------------------- -----------------------------------

public class GestureListener extends 
      GestureDetector.SimpleOnGestureListener { 

     @Override 
     public boolean onDown(MotionEvent e) { 

      return true; 
     } 

     // event when double tap occurs 
     @Override 
     public boolean onDoubleTap(MotionEvent e) { 

      tapped = !tapped; 

      if (tapped) { 



      } else { 



      } 

      return true; 
     } 
    } 

// ------ -------------------------------------------------- ------------------------ ImageViewの

ため

imageView.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       return gestureDetector.onTouchEvent(event); 
      } 

     }); 
+0

あなたのために働いたのですか? –

+0

ああ、私は 'onDown(MotionEvent e)'に真を返していませんでした –

+0

タップされた変数は必要ありません。 onDoubleTapメソッドですでにダブルタップを検出します。 –

2

gesturelistenerを使用する必要はありませんし、オーバーライドを独自のカスタムImageViewのを作成ontouchEventは次のようになります:

long t1 = 0, t2 = 0; 
int count = 0; 
@Override 
public boolean onTouchEvent(MotionEvent e) { 

    int action = e.getAction(); 
    switch (action){ 
     case MotionEvent.ACTION_DOWN: 
      if(count == 0) { 
       t1 = System.currentTimeMillis(); 
      } 
      if(count == 1){ 
       t2 = System.currentTimeMillis(); 
      } 
      count++; 
      if(count > 1) count = 0; 
      Log.d("DoubleTapImageView", "down t1: " + String.valueOf(t1) + " t2: " + String.valueOf(t2) + "," + String.valueOf(t2-t1)); 
      if(Math.abs(t2 - t1) < 300){ 
       t1 = t2 = 0; count = 0; 
       // On double tap here. Do stuff 
      } 

      break; 

    } 
    return super.onTouchEvent(e); 
} 

このようにして、継続時間を制御することができます

+0

システムダブルタップタイムアウトは 'ViewConfiguration.getDoubleTapTimeout()'です。 – Suragch

+0

この回答もご覧ください:https://stackoverflow.com/a/21764444 – Suragch

関連する問題