为Android Gallery添加手势翻页


在Android源码中,找到Gallery文件夹,在ViewImage.java文件中,privateclass MyGestureListener extendsGestureDetector.SimpleOnGestureListener{}类中,重载一下onFling函数,然后在里面根据参数判断下手指向左滑还是向右滑,再设置个阀值,如果滑动的距离超过阀值就换图片,切换图片有现成的函数,就是按左右两边按钮调用的函数。

代码如下:

 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 

                 float velocityY) { 

             // TODO

             if ((e1.getX() - e2.getX() > VALUE_DISTANCE) 

                     && Math.abs(velocityX) > VALUE_SPEED) { 

moveNextOrPrevious(1);

                 Log.d("TAG","[+++++++++++][onFling][Fling left]"); 

             } else if ((e2.getX() - e1.getX() > VALUE_DISTANCE) 

                     && Math.abs(velocityX) > VALUE_SPEED) { 

moveNextOrPrevious(-1);

                 Log.d("TAG","[+++++++++++][onDown][Fling right]"); 

  


             } 

             return true; 

         } 

相关内容