问题描述
我正在使用 Exoplayer 开发视频播放器应用程序并应用滑动手势来调节音量和亮度。顶部、底部、右侧、左侧的滑动手势运行良好。 但我想将宽度分为音量和亮度功能的两部分,建议我做额外的代码。 这是我的 OnSwipetouchListner.java 代码
struct ContainerView: UIViewRepresentable {
func makeUIView(context: Context) -> DonationCell {
DonationCell()
}
func updateUIView(_ uiView: DonationCell,context: Context) {
}
}
}
下面的代码是在Main Activity中的实现
public class OnSwipetouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipetouchListener(Context ctx) {
gestureDetector = new GestureDetector(ctx,new GestureListener());
}
@Override
public boolean onTouch(View v,MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELociTY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
///below first one is downEvent second is moveEvent
@Override
public boolean onFling(MotionEvent e1,MotionEvent e2,float veLocityX,float veLocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(veLocityX) > SWIPE_VELociTY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(veLocityY) > SWIPE_VELociTY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipetop();
}
result = true;
}
} catch (Exception exception) {
exception.printstacktrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipetop() {
}
public void onSwipeBottom() {
}}
解决方法
要检查屏幕的哪一部分被触摸(左侧部分或右侧部分),首先尝试在 onDown 中计算屏幕的宽度和高度:
screen_width = getResources().getDisplayMetrics().widthPixels;
screen_height = getResources().getDisplayMetrics().heightPixels;
创建 2 个布尔值:
private boolean isLeft;
private boolean isRight;
@Override
public boolean onDown(MotionEvent event) {
Log.d("TAG","onDown: ");
screen_width = getResources().getDisplayMetrics().widthPixels;
screen_height = getResources().getDisplayMetrics().heightPixels;
if (event.getX() < (screen_width / 2)) {
// isLeft means the left side is touched
isLeft = true;
isRight = false;
} else if (event.getX() > (screen_width / 2)) {
// isRight means the right side is touched
isLeft = false;
isRight = true;
}
// don't return false here
// or else none of the other gestures will work
return true;
}
然后根据需要使用布尔值:
if (isRight) {
} else if (isLeft) {
}