将 Fling 手势添加到图像视图 - Android
好吧,我一直在引用这里的代码: 在网格布局上进行 Fling 手势检测
但就是无法获取让它工作。在我的主要活动中,我定义了一个简单的图像。我想检测图像上的抖动。下面是我的代码。底部的onclick方法是空的。是因为这个吗?我将其留空,因为在其他代码示例中它不是我想要的。我只想弹出一个简单的吐司,说“向右滑动”或“向左滑动”。
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) {}
}
Okay I have been referencing the code here: Fling gesture detection on grid layout
but just can not get it to work. In my main activity I have a simple image defined. I want to detect a fling on the image. Here is my code below. The onclick method at the bottom is empty. Is it because of this? I left it blank because in the other code sample its not what I want. I just want a simple toast to pop up saying fling right or fling left.
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) {}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是我能想到的最简单的 fllinger 工作版本。
实际上,您可以将其绑定到任何组件,而不仅仅是 ImageView。
但 onClickListener 没有活动(如果您不需要捕获任何 onclick 操作)
它不仅捕获水平方向,还捕获垂直方向(如果不需要,只需删除垂直部分),并且水平滑动具有优先级,如您所见。
在方法返回的地方(以及我的评论所在的地方)只需调用您的方法或其他:)
Here's the simpliest working version of flinger I can think of.
You can actually tie it to any component, not only ImageView.
No activity onClickListener though (if you don't need to catch any onclick actions)
It captures not only horizontal, but vertical also (just delete vertical part if you don't need it), and horizontal swipes have priority as you can see.
In places where method returns (nad where my comments are) just call your method or whatever :)
试试这个
Try this
一些先决条件
1) setonClick 方法
2) 在 onTouch() 中设置手势检测
3) 创建 SwingGestureDetection 类并实现所有方法
4) 在构造函数中传递你的 imageview
这对我来说是完美的工作。如果有任何查询,请发表评论。
some of the precondition
1) setonClick method
2) set your gesture detection in onTouch()
3) create SwingGestureDetection class and implement all method
4) pass your imageview in constructor
This is perfect work for me.if any query then put comment.
尝试阅读:
http://illusionsandroid.blogspot.com/2011 /05/adding-fling-gesture-listener-to-view.html
它允许您将活动实现与自定义侦听器分开。这只是 Alex Orlov 报告的解决方案的重构
Try to read:
http://illusionsandroid.blogspot.com/2011/05/adding-fling-gesture-listener-to-view.html
It allow you to separate your activity implementation from your custom listener. It is simply a refactoring of the solution reported by Alex Orlov
如果您不喜欢创建单独的类或使代码变得复杂,
您可以在 OnTouchListener 中创建一个 GestureDetector 变量,并使您的代码更容易
namVyuVar 可以是您需要在其上设置监听器的视图的任何名称
If you dont like to create a separate class or make code complex,
You can just create a GestureDetector variable inside OnTouchListener and make your code more easier
namVyuVar can be any name of the View on which you need to set the listner
如果您正在寻找像 ImageView.setOnGestureListener 这样的方法,那么没有。你可以去看一下Android源码。最好的选择是在 View 对象的 onTouch() 中处理它。
这是我能做的最简单的手势检测器。
我有一个名为 GenesMotionDetector.java 的类。下面是它的代码:
该类相应的 XML 布局文件是 Nothing.xml。这是它的代码:
现在,采用我可以制作的最简单的 GestureDetector(从上面)并根据您想要的进行修改,我得到:
您不妨在 onTouch() 中处理所有内容。我基本上是在 onTouch 中获取手势并将其传递给 onFling()。
If you're looking for a method like ImageView.setOnGestureListener, there is none. You can go look at the Android source code. Your best option is to handle it in onTouch() of the View object.
This is the simplest GestureDetector I can make.
I have a class called GenesMotionDetector.java. Here's the code for it:
The corresponding XML layout file for that class is nothing.xml. Here's the code for it:
Now, taking the simplest GestureDetector I can make (from above) and modifying it for what you want, I get this:
You might as well just handle everything in onTouch(). I'm basically getting the gestures in onTouch and passing it to onFling().
使用 ImageView 设计布局。
添加
setOnTouchListener
到imageView。imageview.setOnTouchListener(this)
。添加未实现的方法View.OnTouchListener
。然后添加onTouch
方法你可以实现你的手势逻辑。这个示例告诉你从左到右和从右到左的手势,Design your layout with ImageView.
Add
setOnTouchListener
to imageView.imageview.setOnTouchListener(this)
.add unimplemented methodView.OnTouchListener
.ThenonTouch
method you can implemtn your guesture logic.This sample tells you left to right and right to left guesture,kotlin 的快速示例:
设置为任何视图:
Quick example for kotlin:
Set to any view: