2010-11-04 25 views
41

OK Tôi đã tham chiếu mã ở đây: Fling gesture detection on grid layoutThêm Cử chỉ Fling vào chế độ xem hình ảnh - Android

nhưng không thể làm việc đó. Trong hoạt động chính của tôi, tôi đã xác định một hình ảnh đơn giản. Tôi muốn phát hiện một cú ném trên ảnh. Đây là mã của tôi dưới đây. Phương pháp onclick ở phía dưới trống. Có phải vì điều này? Tôi để trống vì trong mẫu mã khác không phải là thứ tôi muốn. Tôi chỉ muốn một bánh mì nướng đơn giản bật lên nói rằng quăng sang phải hoặc chạy sang trái.

public class GestureRightLeft extends Activity implements OnClickListener { 

    ImageView peek; 

    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_MAX_OFF_PATH = 250; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
    private GestureDetector gestureDetector; 
    View.OnTouchListener gestureListener; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     peek =(ImageView) findViewById(R.id.peek); 
     peek.setImageResource(R.drawable.bluestrip); 

     gestureDetector = new GestureDetector(new MyGestureDetector()); 
     gestureListener = new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       if (gestureDetector.onTouchEvent(event)) { 
        return true; 
       } 
       return false; 
      } 
     }; 
    } 

    class MyGestureDetector extends SimpleOnGestureListener { 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      try { 
       if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
        return false; 
       // right to left swipe 
       if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
        Toast.makeText(GestureRightLeft.this, "Left Swipe", Toast.LENGTH_SHORT).show(); 
       } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
        Toast.makeText(GestureRightLeft.this, "Right Swipe", Toast.LENGTH_SHORT).show(); 
       } 
      } catch (Exception e) { 
       // nothing 
      } 
      return false; 
     } 
    } 

    @Override 
    public void onClick(View v) {} 
} 
+0

Xem thêm [Làm thế nào để thêm một máy dò cử chỉ để một cái nhìn trong Android] (https://stackoverflow.com/câu hỏi/45054908/how-to- add-a-gesture-detector-to-a-view-in-android) – Suragch

Trả lời

83

Đây là phiên bản làm việc đơn giản nhất của bộ rung mà tôi có thể nghĩ đến. Bạn thực sự có thể kết nối nó với bất kỳ thành phần nào, không chỉ là ImageView.

public class MyActivity extends Activity { 
    private void onCreate() { 
     final GestureDetector gdt = new GestureDetector(new GestureListener()); 
     final ImageView imageView = (ImageView) findViewById(R.id.image_view); 
     imageView.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(final View view, final MotionEvent event) { 
       gdt.onTouchEvent(event); 
       return true; 
      } 
     }); 
    }    

    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

    private class GestureListener extends SimpleOnGestureListener { 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
      if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       return false; // Right to left 
      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       return false; // Left to right 
      } 

      if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { 
       return false; // Bottom to top 
      } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { 
       return false; // Top to bottom 
      } 
      return false; 
     } 
    } 
} 

Không có hoạt động OnClickListener dù (nếu bạn không cần phải bắt bất cứ hành động onclick) Nó nắm bắt không chỉ ngang, nhưng dọc cũng (chỉ cần xóa phần dọc nếu bạn không cần đến nó), và ngang swipes có ưu tiên như bạn có thể nhìn thấy. Ở những nơi mà phương pháp lợi nhuận (nad nơi bình luận của tôi là) chỉ cần gọi phương pháp của bạn hoặc bất cứ điều gì :)

+0

Giúp tôi hiểu được GestureDetector và flings ...... – Gopinath

+3

Sử dụng 'final GestureDetector gdt = GestureDetector (điều này, new GestureListener());' thay vào đó, mỗi http://developer.android.com/reference/android/view/GestureDetector.html#GestureDetector(android.view.GestureDetector.OnGestureListener) – Richard

+0

Điều này giống như phân trang hơn. Bạn có thể tìm thấy ví dụ điển hình tốt tại đây: http://developer.android.com/training/custom-views/making-interactive.html – Snicolas

13

Hãy thử điều này

imageView.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (gestureDetector.onTouchEvent(event)) { 
       return false; 
      } 
      return true; 
     } 
    }); 
+2

** + 1 ** cảm ơn dude nó làm việc cho tôi .. tôi trở về đúng thay vì sai. bây giờ vấn đề của tôi đã được giải quyết –

+1

@Ganapathy có thể cập nhật câu trả lời với mã đang hoạt động plz –

+0

@contactmeandroid bên trong nếu điều kiện tôi cũng trả về đúng; nó làm việc cho tôi. –

9
imageView.setOnTouchListener(new View.OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return !gestureDetector.onTouchEvent(event); 
    } 
}); 
3

một số trong những điều kiện tiên quyết

1)setonClick method 
    image.setOnClickListener(this); 

2)set your gesture detection in onTouch() 
    image.setOnTouchListener(new OnTouchListener() { 
     GestureDetector gestureDetector = new GestureDetector(new SwingGestureDetection((mContext),image,a)); 
     @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       return gestureDetector.onTouchEvent(event); 
     } 
    }); 

3)create SwingGestureDetection class and implement all method 
@Override 
public boolean onFling(MotionEvent start, MotionEvent finish, float arg2,float arg3) { 
    if(start.getRawX()<finish.getRawX()){ 
     System.out.println("next...swing"); 
    }else{ 
    System.out.println("previois...swing"); 


} 

4)pass your imageview in constructor 
public SwingGestureDetection(Context con,ImageView image,int pos) { 
    mContext=con; 
    this.image=image; 
    this.position=pos; 
} 

Đây là công việc hoàn hảo cho me.if bất kỳ truy vấn sau đó đưa nhận xét.

0

Nếu bạn đang tìm kiếm một phương pháp như ImageView.setOnGestureListener, thì không có gì. Bạn có thể xem mã nguồn Android. Lựa chọn tốt nhất của bạn là xử lý nó trong onTouch() của đối tượng View.

Đây là GestureDetector đơn giản nhất mà tôi có thể thực hiện.

Tôi có một lớp được gọi là GenesMotionDetector.java. Đây là mã cho nó:

package gene.com.motioneventssample; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class GenesMotionDetector extends Activity implements GestureDetector.OnGestureListener { 
    private GestureDetector gestureScanner; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nothing); 
     gestureScanner= new GestureDetector(getBaseContext(),this); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent me) { 
     System.out.println("Inside onTouchEvent() of GenesMotionDetector.java"); 
     return gestureScanner.onTouchEvent(me); 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     System.out.println("Inside onDown() of GenesMotionDetector.java"); 
     return true; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     System.out.println("Inside onFling() of GenesMotionDetector.java"); 
     return true; 
    } 

    @Override 
    public void onLongPress(MotionEvent e) { 
     System.out.println("Inside onLongPress() of GenesMotionDetector.java"); 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
     System.out.println("Inside onScroll() of GenesMotionDetector.java"); 
     return true; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) { 
     System.out.println("Inside onShowPress() of GenesMotionDetector.java"); 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     System.out.println("Inside onSingleTapUp() of GenesMotionDetector.java"); 
     return true; 
    } 
} 

Tệp bố cục XML tương ứng cho lớp đó là nothing.xml. Dưới đây là đoạn code cho nó:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/screen" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text" 
     android:background="#17528c" 
     android:text="testing" 
     android:layout_width="100dp" 
     android:layout_height="200dp" /> 

    <ImageView 
     android:id="@+id/image" 
     android:background="#f8af20" 
     android:layout_width="100dp" 
     android:layout_height="200dp" /> 
</LinearLayout> 

Bây giờ, lấy GestureDetector đơn giản nhất tôi có thể làm (từ trên cao) và sửa đổi nó cho những gì bạn muốn, tôi có được điều này:

package gene.com.motioneventssample; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.VelocityTracker; 
import android.view.View; 
import android.widget.ImageView; 

public class GenesMotionDetector3 extends Activity implements GestureDetector.OnGestureListener { 
    private GestureDetector gestureScanner; 
    ImageView mView3; 

    View.OnTouchListener gestureListener; 
    MotionEvent initialME, finalME; 
    private VelocityTracker mVelocityTracker = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nothing); 


     mView3 = (ImageView) findViewById(R.id.image); 

     // Gesture detection 
     gestureScanner = new GestureDetector(getBaseContext(), this); 

     gestureListener = new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch(event.getAction()){ 
        case MotionEvent.ACTION_DOWN: 
         initialME= event; 

         if(mVelocityTracker == null) { 
          // Retrieve a new VelocityTracker object to watch the velocity of a motion. 
          mVelocityTracker = VelocityTracker.obtain(); 
         } 
         else { 
          // Reset the velocity tracker back to its initial state. 
          mVelocityTracker.clear(); 
         } 
         // Add a user's movement to the tracker. 
         mVelocityTracker.addMovement(event); 

         break; 
        case MotionEvent.ACTION_MOVE: 
         mVelocityTracker.addMovement(event); 
         // When you want to determine the velocity, call 
         // computeCurrentVelocity(). Then call getXVelocity() 
         // and getYVelocity() to retrieve the velocity for each pointer ID. 
         mVelocityTracker.computeCurrentVelocity(1000); 
         break; 
        case MotionEvent.ACTION_UP: 
         finalME=event; 
         break; 
        case MotionEvent.ACTION_CANCEL: 
         // Return a VelocityTracker object back to be re-used by others. 
         mVelocityTracker.recycle(); 
         break; 
       } 
       return onFling(initialME, finalME, mVelocityTracker.getXVelocity(), mVelocityTracker.getYVelocity()); 
       //return false; 
      } 
     }; 

     mView3.setOnTouchListener(gestureListener); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent me) { 
     System.out.println("Inside onTouchEvent() of GenesMotionDetector.java"); 

     return gestureScanner.onTouchEvent(me); 
    } 

    @Override 
    public boolean onDown(MotionEvent e) { 
     System.out.println("Inside onDown() of GenesMotionDetector.java"); 
     return true; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     System.out.println("Inside onFling() of GenesMotionDetector.java"); 
     System.out.println("e1= "+ e1); 
     System.out.println("e2= "+ e2); 
     System.out.println("velocityX= "+ velocityX); 
     System.out.println("velocityY= "+ velocityY); 
     return true; 
    } 

    @Override 
    public void onLongPress(MotionEvent e) { 
     System.out.println("Inside onLongPress() of GenesMotionDetector.java"); 
    } 

    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
     System.out.println("Inside onScroll() of GenesMotionDetector.java"); 
     return true; 
    } 

    @Override 
    public void onShowPress(MotionEvent e) { 
     System.out.println("Inside onShowPress() of GenesMotionDetector.java"); 
    } 

    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
     System.out.println("Inside onSingleTapUp() of GenesMotionDetector.java"); 
     return true; 
    } 
} 

bạn cũng có thể chỉ xử lý mọi thứ trong onTouch(). Tôi về cơ bản nhận được các cử chỉ trong onTouch và chuyển nó đến onFling().

0

Thiết kế bố cục của bạn bằng ImageView. Thêm setOnTouchListener vào imageView. imageview.setOnTouchListener(this) .thêm phương pháp chưa được thực hiện View.OnTouchListener. Sau đó, onTouch phương pháp bạn có thể thực hiện logic khách mời của mình.Mẫu này sẽ cho bạn biết trái sang phải và quyền guesture trái,

float x1,x2; 
float y1, y2; 

@Override 
public boolean onTouch(View view, MotionEvent event) { 
    switch (event.getAction()){ 

     case MotionEvent.ACTION_DOWN:{ 
      x1 = event.getX(); 
      y1 = event.getY(); 
      break; 
     } 

     case MotionEvent.ACTION_UP:{ 
      x2 = event.getX(); 
      y2 = event.getY(); 

      if (x1<x2) { 
       //implement what you need to do here 
      } 
      if (x1>x2) { 
       //implement what you need to do here 
      } 
      break; 
     } 
    } 
    return false; 
} 
+1

Câu trả lời không cung cấp logic ngắt, OP đang tìm kiếm. – Sipty

0

Nếu bạn không muốn tạo ra một lớp riêng biệt hoặc làm cho mã phức tạp,
Bạn chỉ có thể tạo ra một biến GestureDetector bên OnTouchListener và làm cho mã của bạn nhiều hơn dễ dàng hơn

namVyuVar thể được bất kỳ tên của Xem trên mà bạn cần phải thiết lập các listner

namVyuVar.setOnTouchListener(new View.OnTouchListener() 
{ 
    @Override 
    public boolean onTouch(View view, MotionEvent MsnEvtPsgVal) 
    { 
     flingActionVar.onTouchEvent(MsnEvtPsgVal); 
     return true; 
    } 

    GestureDetector flingActionVar = new GestureDetector(getApplicationContext(), new GestureDetector.SimpleOnGestureListener() 
    { 
     private static final int flingActionMinDstVac = 120; 
     private static final int flingActionMinSpdVac = 200; 

     @Override 
     public boolean onFling(MotionEvent fstMsnEvtPsgVal, MotionEvent lstMsnEvtPsgVal, float flingActionXcoSpdPsgVal, float flingActionYcoSpdPsgVal) 
     { 
      if(fstMsnEvtPsgVal.getX() - lstMsnEvtPsgVal.getX() > flingActionMinDstVac && Math.abs(flingActionXcoSpdPsgVal) > flingActionMinSpdVac) 
      { 
       // TskTdo :=> On Right to Left fling 

       return false; 
      } 
      else if (lstMsnEvtPsgVal.getX() - fstMsnEvtPsgVal.getX() > flingActionMinDstVac && Math.abs(flingActionXcoSpdPsgVal) > flingActionMinSpdVac) 
      { 
       // TskTdo :=> On Left to Right fling 

       return false; 
      } 

      if(fstMsnEvtPsgVal.getY() - lstMsnEvtPsgVal.getY() > flingActionMinDstVac && Math.abs(flingActionYcoSpdPsgVal) > flingActionMinSpdVac) 
      { 
       // TskTdo :=> On Bottom to Top fling 

       return false; 
      } 
      else if (lstMsnEvtPsgVal.getY() - fstMsnEvtPsgVal.getY() > flingActionMinDstVac && Math.abs(flingActionYcoSpdPsgVal) > flingActionMinSpdVac) 
      { 
       // TskTdo :=> On Top to Bottom fling 

       return false; 
      } 
      return false; 
     } 
    }); 
}); 
Các vấn đề liên quan