Android onTouchEvent中判断上滑

本文介绍了一种在Android中实现触摸屏滑动方向检测的方法。通过覆盖onTouchEvent方法并使用MotionEvent来捕捉用户的手势,该类能够识别并响应用户在屏幕上的水平和垂直滑动。此外,还提供了一个示例代码片段,展示了如何使用GesterTouchEvent进行更复杂的滑动操作检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ControlViewLinearLayout extends LinearLayout {
   private float downX ;    //按下时 的X坐标
    private float downY ;    //按下时 的Y坐标
    private float moveX ;   
    private float moveY ;  
   
public ControlViewLinearLayout(Context context) {
super(context);


}

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
   
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
 downX = event.getX();
          downY =  event.getY();
break;


case MotionEvent.ACTION_MOVE:
moveX =event.getX();
moveY = event.getY();
break;

 case MotionEvent.ACTION_UP:
          float dx= moveX - downX;
          float dy = moveY - downY;
          int orientation = getOrientation(dx, dy);
          switch (orientation) {
              case 'r':
                  break;
              case 'l':
                  break;
              case 't':
//               Log.e(TAG, "slide top .............");
              hideXPanel();
                  break;
              case 'b':
//             Log.e(TAG, "slide bottom .............");
                  break;
          }
          break;
}
return super.onTouchEvent(event);
}


    private int getOrientation(float dx, float dy) {
        if (Math.abs(dx)>Math.abs(dy)){
            //X轴移动
            return dx>0?'r':'l';
        }else{
            //Y轴移动
            return dy>0?'b':'t';
        }
    }

}、

GesterTouchEvent

+    
+    /**
+     *   if(beginX - endX >  minMove && Math.abs(velocityX) > minVelocity){   //左滑
+        }else if(endX - beginX > minMove && Math.abs(velocityX) > minVelocity){   //右滑
+        }else if(beginY - endY > minMove && Math.abs(velocityY) > minVelocity){   //上滑
+        }else if(endY - beginY > minMove && Math.abs(velocityY) > minVelocity){   //下滑
+        }
+     */
     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
             float velocityY) {
         Log.i(TAG, "[onFling] (" + velocityX + ", " + velocityY + ")");
+        //add by larry
+ int currentCameraId = mCameraActivity.getCameraId();
+ float minMove = 60;         //最小滑动距离
+ float minVelocity = 0;      //最小滑动速度
+ float beginX = e1.getX();     
+ float endX = e2.getX();
+ float beginY = e1.getY();     
+ float endY = e2.getY();
+ if ((beginY - endY > minMove && Math.abs(velocityY) > minVelocity)  ||(endY - beginY > minMove && Math.abs(velocityY) > minVelocity) ) {
+ mCameraActivity.onCameraPicked(currentCameraId == 0 ? 1 : 0);
+ }
+//         if ((beginY - endY > minMove && Math.abs(velocityY) > minVelocity)  ) { //上下滑动
+//         //todo
+// }else if(endY - beginY > minMove && Math.abs(velocityY) > minVelocity){
+// //todo
+// }
+  //add by larry end
         return false;
     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值