如何处理向下滑动的手势上的ImageView [英] How to handle swipe down gesture on an ImageView
本文介绍了如何处理向下滑动的手势上的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对一个ImageView的认识向下滑动的手势。我试图 GestureDetector
,但它现在pcated德$ P $。示例code我使用如下。但是,它不会内onFling()方法。结果
GestureDetector gestureDetector;
gestureDetector =新GestureDetector(新GestureListener()); view.findViewById(R.id.imageView).setOnTouchListener(新OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
如果(gestureDetector.onTouchEvent(事件)){
的System.out.println(DONE -------);
updateSwipeAction();
返回true;
}
返回false;
}
});类GestureListener扩展SimpleOnGestureListener { 私有静态最终诠释SWIPE_MIN_DISTANCE = 10;
私有静态最终诠释SWIPE_THRESHOLD_VELOCITY = 10; @覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,浮velocityY){
如果(e1.getX() - e2.getX()> SWIPE_MIN_DISTANCE){
//从右到左
返回true;
}否则如果(e2.getX() - e1.getX()> SWIPE_MIN_DISTANCE){
//从左到右
返回true;
} 如果(e1.getY() - e2.getY()> SWIPE_MIN_DISTANCE){
//从底部到顶部
返回true;
}否则如果(e2.getY() - e1.getY()> SWIPE_MIN_DISTANCE){
//从上到下
返回true;
}
返回false;
} @覆盖
公共布尔onDown(MotionEvent五){
//总是返回true,因为所有的手势总是onDown和&lt开始; BR>
//如果返回false,该框架将不会尝试拿起onFling为例。
返回true;
}
}
解决方案
试试这个,实现ongestureListener和那里有一个onscroll方法U可以直接访问
@燮pressLint(NewApi)
块引用>公共类AndroidExplorer扩展活动实现结果
android.view.GestureDetector.OnGestureListener{
私人处理程序mHandler =新的处理程序();
@覆盖
保护无效的onCreate(捆绑mybundle)
{
// TODO自动生成方法存根
super.onCreate(mybundle);
的setContentView(R.layout.main);
}
@覆盖
公共布尔onDown(MotionEvent五){
// TODO自动生成方法存根
返回false;
}
@覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
浮动velocityY){
// TODO自动生成方法存根
返回false;
}
@覆盖
公共无效onLong preSS(MotionEvent五){
// TODO自动生成方法存根}
@覆盖
公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,
浮动distanceY){
// TODO自动生成方法存根
返回false;
}
@覆盖
公共无效昂秀preSS(MotionEvent五){
// TODO自动生成方法存根}
@覆盖
公共布尔onSingleTapUp(MotionEvent五){
// TODO自动生成方法存根
返回false;
}}
I am trying for recognizing swipe down gesture on an imageview. I tried
GestureDetector
but it's deprecated now. Sample code I am using is below. But, it's not going inside onFling() method.GestureDetector gestureDetector; gestureDetector = new GestureDetector(new GestureListener()); view.findViewById(R.id.imageView).setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { System.out.println("DONE-------"); updateSwipeAction(); return true; } return false; } }); class GestureListener extends SimpleOnGestureListener { private static final int SWIPE_MIN_DISTANCE = 10; private static final int SWIPE_THRESHOLD_VELOCITY = 10; @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) { //From Right to Left return true; } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) { //From Left to Right return true; } if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) { //From Bottom to Top return true; } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) { //From Top to Bottom return true; } return false; } @Override public boolean onDown(MotionEvent e) { //always return true since all gestures always begin with onDown and<br> //if this returns false, the framework won't try to pick up onFling for example. return true; } }
解决方案Try this ,Implements ongestureListener and theres a onscroll method u can access directly
@SuppressLint("NewApi")
public class AndroidExplorer extends Activity implements
android.view.GestureDetector.OnGestureListener{ private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle mybundle) { // TODO Auto-generated method stub super.onCreate(mybundle); setContentView(R.layout.main); } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; }
}
这篇关于如何处理向下滑动的手势上的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文