Fling gesture detection on grid layout
我想让
我有一个包含9个
我从RomainGuy的photostream应用程序中获取的那个文件只是稍微修改过。
对于简单的点击情况,我只需要为每个
如果我的活动实现我不知道怎么做将其设置为
Grid 或Image 认为添加。1
2public class SelectFilterActivity extends Activity implements
View.OnClickListener, OnGestureListener { ...如果我的活动实现那么我没有
onFling 方法到override 方法(它有两个事件作为参数允许我以确定是否值得注意的。1
2public class SelectFilterActivity extends Activity implements
View.OnClickListener, OnTouchListener { ...如果我做一个自定义的
View ,像GestureImageView ,扩展ImageView ,我不知道如何告诉活动,一个fling 已经发生的观点。无论如何,我尝试过这个方法,当我触摸屏幕时,方法就没有被调用。
我真的需要一个跨视图工作的具体例子。什么,什么时候以及如何连接这个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // 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; } } }); |
有没有可能在我的屏幕顶部放一个透明的视图来捕捉打火?
如果我选择不使用
这是一个非常简单的活动,我正试图让
我一直在研究这些来源:
检测手势-教程
SDK文档
计算器代码
到目前为止我什么都没用,我希望能得到一些指点。
多亏了代码Shogun,它的代码我适应了我的情况。
让您的活动照常执行cx1〔0〕:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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; } } } |
将手势侦听器附加到添加到主布局的所有视图上;
1 2 3 | // Do this for each view added to the grid imageView.setOnClickListener(SelectFilterActivity.this); imageView.setOnTouchListener(gestureListener); |
当您的重写方法被击中时,请敬畏地观看,活动的
1 2 3 4 | public void onClick(View v) { Filter f = (Filter) v.getTag(); FilterFullscreenActivity.show(this, input, f); } |
后"放荡"舞蹈是可选的,但鼓励。
上面的一个答案提到处理不同的像素密度,但建议用手计算滑动参数。值得注意的是,您可以使用
1 2 3 4 5 | final ViewConfiguration vc = ViewConfiguration.get(getContext()); final int swipeMinDistance = vc.getScaledPagingTouchSlop(); final int swipeThresholdVelocity = vc.getScaledMinimumFlingVelocity(); final int swipeMaxOffPath = vc.getScaledTouchSlop(); // (there is also vc.getScaledMaximumFlingVelocity() one could check against) |
我注意到使用这些值会使Fling的"感觉"在应用程序和系统的其他部分之间更加一致。
我做的有点不同,并编写了一个额外的检测器类来实现
1 2 3 | ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this); lowestLayout = (RelativeLayout)this.findViewById(R.id.lowestLayout); lowestLayout.setOnTouchListener(activitySwipeDetector); |
其中id.lowest layout是布局层次结构中最低视图的id.xxx,lowest layout声明为relativelayout
然后是实际的活动刷卡检测器类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | public class ActivitySwipeDetector implements View.OnTouchListener { static final String logTag ="ActivitySwipeDetector"; private Activity activity; static final int MIN_DISTANCE = 100; private float downX, downY, upX, upY; public ActivitySwipeDetector(Activity activity){ this.activity = activity; } public void onRightSwipe(){ Log.i(logTag,"RightToLeftSwipe!"); activity.doSomething(); } public void onLeftSwipe(){ Log.i(logTag,"LeftToRightSwipe!"); activity.doSomething(); } public void onDownSwipe(){ Log.i(logTag,"onTopToBottomSwipe!"); activity.doSomething(); } public void onUpSwipe(){ Log.i(logTag,"onBottomToTopSwipe!"); activity.doSomething(); } public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: { downX = event.getX(); downY = event.getY(); return true; } case MotionEvent.ACTION_UP: { upX = event.getX(); upY = event.getY(); float deltaX = downX - upX; float deltaY = downY - upY; // swipe horizontal? if(Math.abs(deltaX) > Math.abs(deltaY)) { if(Math.abs(deltaX) > MIN_DISTANCE){ // left or right if(deltaX > 0) { this.onRightSwipe(); return true; } if(deltaX < 0) { this.onLeftSwipe(); return true; } } else { Log.i(logTag,"Horizontal Swipe was only" + Math.abs(deltaX) +" long, need at least" + MIN_DISTANCE); return false; // We don't consume the event } } // swipe vertical? else { if(Math.abs(deltaY) > MIN_DISTANCE){ // top or down if(deltaY < 0) { this.onDownSwipe(); return true; } if(deltaY > 0) { this.onUpSwipe(); return true; } } else { Log.i(logTag,"Vertical Swipe was only" + Math.abs(deltaX) +" long, need at least" + MIN_DISTANCE); return false; // We don't consume the event } } return true; } } return false; } } |
对我来说真的很好!
我对Thomas Fankhauser的解决方案进行了轻微修改和修复
整个系统由两个文件组成:Swipeinterface和ActivitySwipedetector
滑动Interface.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | import android.view.View; public interface SwipeInterface { public void bottom2top(View v); public void left2right(View v); public void right2left(View v); public void top2bottom(View v); } |
检测器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import android.util.Log; import android.view.MotionEvent; import android.view.View; public class ActivitySwipeDetector implements View.OnTouchListener { static final String logTag ="ActivitySwipeDetector"; private SwipeInterface activity; static final int MIN_DISTANCE = 100; private float downX, downY, upX, upY; public ActivitySwipeDetector(SwipeInterface activity){ this.activity = activity; } public void onRightToLeftSwipe(View v){ Log.i(logTag,"RightToLeftSwipe!"); activity.right2left(v); } public void onLeftToRightSwipe(View v){ Log.i(logTag,"LeftToRightSwipe!"); activity.left2right(v); } public void onTopToBottomSwipe(View v){ Log.i(logTag,"onTopToBottomSwipe!"); activity.top2bottom(v); } public void onBottomToTopSwipe(View v){ Log.i(logTag,"onBottomToTopSwipe!"); activity.bottom2top(v); } public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: { downX = event.getX(); downY = event.getY(); return true; } case MotionEvent.ACTION_UP: { upX = event.getX(); upY = event.getY(); float deltaX = downX - upX; float deltaY = downY - upY; // swipe horizontal? if(Math.abs(deltaX) > MIN_DISTANCE){ // left or right if(deltaX < 0) { this.onLeftToRightSwipe(v); return true; } if(deltaX > 0) { this.onRightToLeftSwipe(v); return true; } } else { Log.i(logTag,"Swipe was only" + Math.abs(deltaX) +" long, need at least" + MIN_DISTANCE); } // swipe vertical? if(Math.abs(deltaY) > MIN_DISTANCE){ // top or down if(deltaY < 0) { this.onTopToBottomSwipe(v); return true; } if(deltaY > 0) { this.onBottomToTopSwipe(v); return true; } } else { Log.i(logTag,"Swipe was only" + Math.abs(deltaX) +" long, need at least" + MIN_DISTANCE); v.performClick(); } } } return false; } } |
使用方法如下:
1 2 3 | ActivitySwipeDetector swipe = new ActivitySwipeDetector(this); LinearLayout swipe_layout = (LinearLayout) findViewById(R.id.swipe_layout); swipe_layout.setOnTouchListener(swipe); |
在实现
1 2 3 4 5 6 7 8 | @Override public void left2right(View v) { switch(v.getId()){ case R.id.swipe_layout: // do your stuff here break; } } |
上面的滑动手势检测器代码非常有用!但是,您可能希望使用以下相对值
1 2 3 4 5 | DisplayMetrics dm = getResources().getDisplayMetrics(); int REL_SWIPE_MIN_DISTANCE = (int)(SWIPE_MIN_DISTANCE * dm.densityDpi / 160.0f); int REL_SWIPE_MAX_OFF_PATH = (int)(SWIPE_MAX_OFF_PATH * dm.densityDpi / 160.0f); int REL_SWIPE_THRESHOLD_VELOCITY = (int)(SWIPE_THRESHOLD_VELOCITY * dm.densityDpi / 160.0f); |
我的解决方案版本由Thomas Fankhauser和Marek Sebera提出(不处理垂直滑动):
滑动Interface.java
1 2 3 4 5 6 7 8 | import android.view.View; public interface SwipeInterface { public void onLeftToRight(View v); public void onRightToLeft(View v); } |
活动Swipedetector.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import android.content.Context; import android.util.DisplayMetrics; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; public class ActivitySwipeDetector implements View.OnTouchListener { static final String logTag ="ActivitySwipeDetector"; private SwipeInterface activity; private float downX, downY; private long timeDown; private final float MIN_DISTANCE; private final int VELOCITY; private final float MAX_OFF_PATH; public ActivitySwipeDetector(Context context, SwipeInterface activity){ this.activity = activity; final ViewConfiguration vc = ViewConfiguration.get(context); DisplayMetrics dm = context.getResources().getDisplayMetrics(); MIN_DISTANCE = vc.getScaledPagingTouchSlop() * dm.density; VELOCITY = vc.getScaledMinimumFlingVelocity(); MAX_OFF_PATH = MIN_DISTANCE * 2; } public void onRightToLeftSwipe(View v){ Log.i(logTag,"RightToLeftSwipe!"); activity.onRightToLeft(v); } public void onLeftToRightSwipe(View v){ Log.i(logTag,"LeftToRightSwipe!"); activity.onLeftToRight(v); } public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: { Log.d("onTouch","ACTION_DOWN"); timeDown = System.currentTimeMillis(); downX = event.getX(); downY = event.getY(); return true; } case MotionEvent.ACTION_UP: { Log.d("onTouch","ACTION_UP"); long timeUp = System.currentTimeMillis(); float upX = event.getX(); float upY = event.getY(); float deltaX = downX - upX; float absDeltaX = Math.abs(deltaX); float deltaY = downY - upY; float absDeltaY = Math.abs(deltaY); long time = timeUp - timeDown; if (absDeltaY > MAX_OFF_PATH) { Log.i(logTag, String.format("absDeltaY=%.2f, MAX_OFF_PATH=%.2f", absDeltaY, MAX_OFF_PATH)); return v.performClick(); } final long M_SEC = 1000; if (absDeltaX > MIN_DISTANCE && absDeltaX > time * VELOCITY / M_SEC) { if(deltaX < 0) { this.onLeftToRightSwipe(v); return true; } if(deltaX > 0) { this.onRightToLeftSwipe(v); return true; } } else { Log.i(logTag, String.format("absDeltaX=%.2f, MIN_DISTANCE=%.2f, absDeltaX > MIN_DISTANCE=%b", absDeltaX, MIN_DISTANCE, (absDeltaX > MIN_DISTANCE))); Log.i(logTag, String.format("absDeltaX=%.2f, time=%d, VELOCITY=%d, time*VELOCITY/M_SEC=%d, absDeltaX > time * VELOCITY / M_SEC=%b", absDeltaX, time, VELOCITY, time * VELOCITY / M_SEC, (absDeltaX > time * VELOCITY / M_SEC))); } } } return false; } } |
这是一个古老的问题,2011年7月,谷歌发布了兼容软件包,第3版),其中包括用于Android 1.6以上版本的
以下是一些有关详细信息的链接:
- http://android-developers.blogspot.com/2011/08/horizontal-view-swipping-with-viewpager.html
- http://mobile.tutspus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
- http://thepseudocoder.wordpress.com/2011/10/05/android-page-swipping-using-viewpager/
有一个内置接口,您可以直接用于所有手势:下面是对基本级别用户的解释:有两种进口产品在选择时要小心,它们都是不同的。
在Web(和本页)上有一些建议使用viewconfiguration.getscaledTouchlop()为
有了API级别8(android 2.2,froyo),您就有了用于页面滑动的
也作为一个小的增强。
Try/Catch块的主要原因是e1对于初始移动可能为空。除了try/catch之外,还包括一个空值和返回值的测试。类似于以下内容
1 2 3 4 5 | if (e1 == null || e2 == null) return false; try { ... } catch (Exception e) {} return false; |
这里有很多很好的信息。不幸的是,尽管人们会认为这对于许多应用程序来说是必要的,但许多这种抛出的处理代码分散在不同的站点上,处于不同的完成状态。
我花了点时间创建了一个FlingListener来验证是否满足了适当的条件。我添加了一个页面抛出侦听器,它添加了更多的检查以确保抛出满足页面抛出的阈值。这两个监听器都允许您轻松地将Fling限制在水平或垂直轴上。您可以看到它是如何在视图中用于滑动图像的。我承认这里的人做了大部分的研究——我只是把它放在一个可用的图书馆里。
这几天是我在Android上进行编码的第一次尝试;期待更多的事情。
如果有人想要一个有效的实现,这是顶部两个答案的组合答案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | package com.yourapplication; import android.content.Context; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; public abstract class OnSwipeListener implements View.OnTouchListener { private final GestureDetector gestureDetector; public OnSwipeListener(Context context){ gestureDetector = new GestureDetector(context, new OnSwipeGestureListener(context)); gestureDetector.setIsLongpressEnabled(false); } @Override public boolean onTouch(View view, MotionEvent event) { return gestureDetector.onTouchEvent(event); } private final class OnSwipeGestureListener extends GestureDetector.SimpleOnGestureListener { private final int minSwipeDelta; private final int minSwipeVelocity; private final int maxSwipeVelocity; private OnSwipeGestureListener(Context context) { ViewConfiguration configuration = ViewConfiguration.get(context); // We think a swipe scrolls a full page. //minSwipeDelta = configuration.getScaledTouchSlop(); minSwipeDelta = configuration.getScaledPagingTouchSlop(); minSwipeVelocity = configuration.getScaledMinimumFlingVelocity(); maxSwipeVelocity = configuration.getScaledMaximumFlingVelocity(); } @Override public boolean onDown(MotionEvent event) { // Return true because we want system to report subsequent events to us. return true; } // NOTE: see http://stackoverflow.com/questions/937313/android-basic-gesture-detection @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { boolean result = false; try { float deltaX = event2.getX() - event1.getX(); float deltaY = event2.getY() - event1.getY(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(velocityY); float absDeltaX = Math.abs(deltaX); float absDeltaY = Math.abs(deltaY); if (absDeltaX > absDeltaY) { if (absDeltaX > minSwipeDelta && absVelocityX > minSwipeVelocity && absVelocityX < maxSwipeVelocity) { if (deltaX < 0) { onSwipeLeft(); } else { onSwipeRight(); } } result = true; } else if (absDeltaY > minSwipeDelta && absVelocityY > minSwipeVelocity && absVelocityY < maxSwipeVelocity) { if (deltaY < 0) { onSwipeTop(); } else { onSwipeBottom(); } } result = true; } catch (Exception e) { e.printStackTrace(); } return result; } } public void onSwipeLeft() {} public void onSwipeRight() {} public void onSwipeTop() {} public void onSwipeBottom() {} } |
您可以使用DroidQuery库来处理fling、clicks、long clicks和自定义事件。该实现是基于我前面的回答构建的,但DroidQuery提供了一个灵活、简单的语法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | //global variables private boolean isSwiping = false; private SwipeDetector.Direction swipeDirection = null; private View v;//must be instantiated before next call. //swipe-handling code $.with(v).swipe(new Function() { @Override public void invoke($ droidQuery, Object... params) { if (params[0] == SwipeDetector.Direction.START) isSwiping = true; else if (params[0] == SwipeDetector.Direction.STOP) { if (isSwiping) { isSwiping = false; if (swipeDirection != null) { switch(swipeDirection) { case DOWN : //TODO: Down swipe complete, so do something break; case UP : //TODO: Up swipe complete, so do something break; case LEFT : //TODO: Left swipe complete, so do something break; case RIGHT : //TODO: Right swipe complete, so do something break; default : break; } } } } else { swipeDirection = (SwipeDetector.Direction) params[0]; } } }); |
原始答案
此答案使用其他答案中的组件组合。它由
闪光探测器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | package self.philbrown; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; /** * Detect Swipes on a per-view basis. Based on original code by Thomas Fankhauser on StackOverflow.com, * with adaptations by other authors (see link). * @author Phil Brown * @see android-basic-gesture-detection */ public class SwipeDetector implements View.OnTouchListener { /** * The minimum distance a finger must travel in order to register a swipe event. */ private int minSwipeDistance; /** Maintains a reference to the first detected down touch event. */ private float downX, downY; /** Maintains a reference to the first detected up touch event. */ private float upX, upY; /** provides access to size and dimension contants */ private ViewConfiguration config; /** * provides callbacks to a listener class for various swipe gestures. */ private SwipeListener listener; public SwipeDetector(SwipeListener listener) { this.listener = listener; } /** * {@inheritDoc} */ public boolean onTouch(View v, MotionEvent event) { if (config == null) { config = ViewConfiguration.get(v.getContext()); minSwipeDistance = config.getScaledTouchSlop(); } switch(event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); upY = event.getY(); float deltaX = downX - upX; float deltaY = downY - upY; // swipe horizontal? if(Math.abs(deltaX) > minSwipeDistance) { // left or right if (deltaX < 0) { if (listener != null) { listener.onRightSwipe(v); return true; } } if (deltaX > 0) { if (listener != null) { listener.onLeftSwipe(v); return true; } } } // swipe vertical? if(Math.abs(deltaY) > minSwipeDistance) { // top or down if (deltaY < 0) { if (listener != null) { listener.onDownSwipe(v); return true; } } if (deltaY > 0) { if (listener != null) { listener.onUpSwipe(v); return true; } } } } return false; } /** * Provides callbacks to a registered listener for swipe events in {@link SwipeDetector} * @author Phil Brown */ public interface SwipeListener { /** Callback for registering a new swipe motion from the bottom of the view toward its top. */ public void onUpSwipe(View v); /** Callback for registering a new swipe motion from the left of the view toward its right. */ public void onRightSwipe(View v); /** Callback for registering a new swipe motion from the right of the view toward its left. */ public void onLeftSwipe(View v); /** Callback for registering a new swipe motion from the top of the view toward its bottom. */ public void onDownSwipe(View v); } } |
滑动拦截器视图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package self.philbrown; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.RelativeLayout; import com.npeinc.module_NPECore.model.SwipeDetector; import com.npeinc.module_NPECore.model.SwipeDetector.SwipeListener; /** * View subclass used for handling all touches (swipes and others) * @author Phil Brown */ public class SwipeInterceptorView extends RelativeLayout { private SwipeDetector swiper = null; public void setSwipeListener(SwipeListener listener) { if (swiper == null) swiper = new SwipeDetector(listener); } public SwipeInterceptorView(Context context) { super(context); } public SwipeInterceptorView(Context context, AttributeSet attrs) { super(context, attrs); } public SwipeInterceptorView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onTouchEvent(MotionEvent e) { boolean swipe = false, touch = false; if (swiper != null) swipe = swiper.onTouch(this, e); touch = super.onTouchEvent(e); return swipe || touch; } } |
我知道现在回答还为时已晚,但我仍然在为ListView发布滑动检测,说明如何在ListView项目中使用滑动触摸监听器。
参考文献:灭绝者13(本页答案之一)
生成一个ActivitySwipedetector.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | package com.example.wocketapp; import android.content.Context; import android.util.DisplayMetrics; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; public class ActivitySwipeDetector implements View.OnTouchListener { static final String logTag ="SwipeDetector"; private SwipeInterface activity; private float downX, downY; private long timeDown; private final float MIN_DISTANCE; private final int VELOCITY; private final float MAX_OFF_PATH; public ActivitySwipeDetector(Context context, SwipeInterface activity) { this.activity = activity; final ViewConfiguration vc = ViewConfiguration.get(context); DisplayMetrics dm = context.getResources().getDisplayMetrics(); MIN_DISTANCE = vc.getScaledPagingTouchSlop() * dm.density; VELOCITY = vc.getScaledMinimumFlingVelocity(); MAX_OFF_PATH = MIN_DISTANCE * 2; } public void onRightToLeftSwipe(View v) { Log.i(logTag,"RightToLeftSwipe!"); activity.onRightToLeft(v); } public void onLeftToRightSwipe(View v) { Log.i(logTag,"LeftToRightSwipe!"); activity.onLeftToRight(v); } public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { Log.d("onTouch","ACTION_DOWN"); timeDown = System.currentTimeMillis(); downX = event.getX(); downY = event.getY(); v.getParent().requestDisallowInterceptTouchEvent(false); return true; } case MotionEvent.ACTION_MOVE: { float y_up = event.getY(); float deltaY = y_up - downY; float absDeltaYMove = Math.abs(deltaY); if (absDeltaYMove > 60) { v.getParent().requestDisallowInterceptTouchEvent(false); } else { v.getParent().requestDisallowInterceptTouchEvent(true); } } break; case MotionEvent.ACTION_UP: { Log.d("onTouch","ACTION_UP"); long timeUp = System.currentTimeMillis(); float upX = event.getX(); float upY = event.getY(); float deltaX = downX - upX; float absDeltaX = Math.abs(deltaX); float deltaY = downY - upY; float absDeltaY = Math.abs(deltaY); long time = timeUp - timeDown; if (absDeltaY > MAX_OFF_PATH) { Log.e(logTag, String.format( "absDeltaY=%.2f, MAX_OFF_PATH=%.2f", absDeltaY, MAX_OFF_PATH)); return v.performClick(); } final long M_SEC = 1000; if (absDeltaX > MIN_DISTANCE && absDeltaX > time * VELOCITY / M_SEC) { v.getParent().requestDisallowInterceptTouchEvent(true); if (deltaX < 0) { this.onLeftToRightSwipe(v); return true; } if (deltaX > 0) { this.onRightToLeftSwipe(v); return true; } } else { Log.i(logTag, String.format( "absDeltaX=%.2f, MIN_DISTANCE=%.2f, absDeltaX > MIN_DISTANCE=%b", absDeltaX, MIN_DISTANCE, (absDeltaX > MIN_DISTANCE))); Log.i(logTag, String.format( "absDeltaX=%.2f, time=%d, VELOCITY=%d, time*VELOCITY/M_SEC=%d, absDeltaX > time * VELOCITY / M_SEC=%b", absDeltaX, time, VELOCITY, time * VELOCITY / M_SEC, (absDeltaX > time * VELOCITY / M_SEC))); } v.getParent().requestDisallowInterceptTouchEvent(false); } } return false; } public interface SwipeInterface { public void onLeftToRight(View v); public void onRightToLeft(View v); } } |
从您的活动类调用它,如下所示:
1 | yourLayout.setOnTouchListener(new ActivitySwipeDetector(this, your_activity.this)); |
别忘了实现Swipeinterface,它将为您提供两种@override方法:
1 2 3 4 5 6 7 8 9 10 11 | @Override public void onLeftToRight(View v) { Log.e("TAG","L to R"); } @Override public void onRightToLeft(View v) { Log.e("TAG","R to L"); } |
如果不想创建单独的类或使代码复杂,您只需在ontouchlistener中创建一个gesturedetector变量,使代码更加容易。
namvyuvar可以是需要设置listner的视图的任何名称。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 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; } }); }); |
手势是触发触摸屏和用户之间交互的微妙动作。它会持续一段时间,从屏幕上的第一次触摸到最后一根手指离开表面的那一点。
Android为我们提供了一个名为"手势检测器"的类,我们可以使用它来检测常见的手势,如向下和向上轻击、垂直和水平滑动(轻击)、长按和短按、双击等,并将听众连接到这些手势上。
使我们的活动类实现gesturedetector.ondoubletapListener(用于双击手势检测)和gesturedetector.ongestureListener接口,并实现所有抽象方法。有关详细信息。您可以访问https://developer.android.com/training/gestures/detector.html。礼貌
用于演示测试.gesturedetectordemo
所有人:不要忘记case motionevent.action_cancel:
它可以在30%的刷卡中不加任何动作
在这种情况下它等于行动
我编辑了一个更通用的类,我选择了Tomas的类,并添加了一个接口,可以将事件发送到您的活动或片段中。它将在构造函数上注册侦听器,因此请确保实现接口,否则将调用ClassCastException。接口返回定义的四个final int中的一个。在类中,并返回在其上激活的视图。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import android.app.Activity; import android.support.v4.app.Fragment; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class SwipeDetector implements View.OnTouchListener{ static final int MIN_DISTANCE = 100; private float downX, downY, upX, upY; public final static int RIGHT_TO_LEFT=1; public final static int LEFT_TO_RIGHT=2; public final static int TOP_TO_BOTTOM=3; public final static int BOTTOM_TO_TOP=4; private View v; private onSwipeEvent swipeEventListener; public SwipeDetector(Activity activity,View v){ try{ swipeEventListener=(onSwipeEvent)activity; } catch(ClassCastException e) { Log.e("ClassCastException",activity.toString()+" must implement SwipeDetector.onSwipeEvent"); } this.v=v; } public SwipeDetector(Fragment fragment,View v){ try{ swipeEventListener=(onSwipeEvent)fragment; } catch(ClassCastException e) { Log.e("ClassCastException",fragment.toString()+" must implement SwipeDetector.onSwipeEvent"); } this.v=v; } public void onRightToLeftSwipe(){ swipeEventListener.SwipeEventDetected(v,RIGHT_TO_LEFT); } public void onLeftToRightSwipe(){ swipeEventListener.SwipeEventDetected(v,LEFT_TO_RIGHT); } public void onTopToBottomSwipe(){ swipeEventListener.SwipeEventDetected(v,TOP_TO_BOTTOM); } public void onBottomToTopSwipe(){ swipeEventListener.SwipeEventDetected(v,BOTTOM_TO_TOP); } public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: { downX = event.getX(); downY = event.getY(); return true; } case MotionEvent.ACTION_UP: { upX = event.getX(); upY = event.getY(); float deltaX = downX - upX; float deltaY = downY - upY; //HORIZONTAL SCROLL if(Math.abs(deltaX) > Math.abs(deltaY)) { if(Math.abs(deltaX) > MIN_DISTANCE){ // left or right if(deltaX < 0) { this.onLeftToRightSwipe(); return true; } if(deltaX > 0) { this.onRightToLeftSwipe(); return true; } } else { //not long enough swipe... return false; } } //VERTICAL SCROLL else { if(Math.abs(deltaY) > MIN_DISTANCE){ // top or down if(deltaY < 0) { this.onTopToBottomSwipe(); return true; } if(deltaY > 0) { this.onBottomToTopSwipe(); return true; } } else { //not long enough swipe... return false; } } return true; } } return false; } public interface onSwipeEvent { public void SwipeEventDetected(View v , int SwipeType); } } |