Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

android - 网格布局上的翻转手势检测(Fling gesture detection on grid layout)

I want to get fling gesture detection working in my Android application.

(我想fling手势检测工作在我的Android应用程序。)

What I have is a GridLayout that contains 9 ImageView s.

(我所拥有的是一个GridLayout ,其中包含9个ImageView 。)

The source can be found here: Romain Guys's Grid Layout .

(可以在这里找到源: Romain Guys的Grid Layout 。)

That file I take is from Romain Guy's Photostream application and has only been slightly adapted.

(我带的那个文件来自Romain Guy的Photostream应用程序 ,只是稍作修改。)

For the simple click situation I need only set the onClickListener for each ImageView I add to be the main activity which implements View.OnClickListener .

(对于简单的点击情况我只需要设置onClickListener每个ImageView我添加是主要的activity ,它实现View.OnClickListener 。)

It seems infinitely more complicated to implement something that recognizes a fling .

(实现识别出fling东西似乎无限复杂。)

I presume this is because it may span views ?

(我认为这是因为它可能跨越views ?)

  • If my activity implements OnGestureListener I don't know how to set that as the gesture listener for the Grid or the Image views that I add.

    (如果我的活动实现了OnGestureListener我将不知道如何将其设置为我添加的GridImage视图的手势侦听器。)

     public class SelectFilterActivity extends Activity implements View.OnClickListener, OnGestureListener { ... 
  • If my activity implements OnTouchListener then I have no onFling method to override (it has two events as parameters allowing me to determine if the fling was noteworthy).

    (如果我的活动实现了OnTouchListener那么我就没有要override onFling方法(它有两个事件作为参数,使我可以确定onFling是否值得关注)。)

     public class SelectFilterActivity extends Activity implements View.OnClickListener, OnTouchListener { ... 
  • If I make a custom View , like GestureImageView that extends ImageView I don't know how to tell the activity that a fling has occurred from the view.

    (如果我做一个自定义的View ,如GestureImageView扩展ImageView ,我不知道怎么跟一个活动fling已经从视图中发生。)

    In any case, I tried this and the methods weren't called when I touched the screen.

    (无论如何,我都尝试过这种方法,并且触摸屏幕时不会调用这些方法。)

I really just need a concrete example of this working across views.

(我真的只需要一个跨视图工作的具体示例。)

What, when and how should I attach this listener ?

(什么,何时以及如何附加此listener ?)

I need to be able to detect single clicks also.

(我还需要能够检测到单击。)

// Gesture detection
mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        int dx = (int) (e2.getX() - e1.getX());
        // don't accept the fling if it's too short
        // as it may conflict with a button push
        if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.absvelocityY)) {
            if (velocityX > 0) {
                moveRight();
            } else {
                moveLeft();
            }
            return true;
        } else {
            return false;
        }
    }
});

Is it possible to lay a transparent view over the top of my screen to capture flings?

(是否可以在屏幕上方放置透明视图以捕获抓拍?)

If I choose not to inflate my child image views from XML can I pass the GestureDetector as a constructor parameter to a new subclass of ImageView that I create?

(如果我选择不inflate从XML我的孩子形象的意见我可以传递GestureDetector作为构造函数参数的一个新的子类ImageView ,我创造?)

This is the very simple activity that I'm trying to get the fling detection to work for: SelectFilterActivity (Adapted from photostream) .

(这是一个非常简单的活动,我正在尝试使fling检测起作用: SelectFilterActivity(从photostream改编而成) 。)

I've been looking at these sources:

(我一直在寻找这些来源:)

Nothing has worked for me so far and I was hoping for some pointers.

(到目前为止,对我来说什么都没有奏效,我一直希望有一些指点。)

  ask by gav translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks to Code Shogun , whose code I adapted to my situation.

(感谢Code Shogun ,我适应了我的情况。)

Let your activity implement OnClickListener as usual:

(让您的活动照常实现OnClickListener :)

public class SelectFilterActivity extends Activity implements OnClickListener {

  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
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* ... */

    // Gesture detection
    gestureDetector = new GestureDetector(this, new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
      public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
      }
    };

  }

  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(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
          Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
        }
      } catch (Exception e) {
         // nothing
      }
      return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
      return true;
    }
  }
}

Attach your gesture listener to all the views you add to the main layout;

(将手势侦听器附加到您添加到主布局中的所有视图;)

// Do this for each view added to the grid
imageView.setOnClickListener(SelectFilterActivity.this); 
imageView.setOnTouchListener(gestureListener);

Watch in awe as your overridden methods are hit, both the onClick(View v) of the activity and the onFling of the gesture listener.

(敬畏地看着您的重写方法被击中,包括活动的onClick(View v)和手势侦听器的onFling 。)

public void onClick(View v) {
  Filter f = (Filter) v.getTag();
  FilterFullscreenActivity.show(this, input, f);
}

The post 'fling' dance is optional but encouraged.

(后期的“跳”舞是可选的,但值得鼓励。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...