gpt4 book ai didi

android - 使用从上到下的滑动手势控制android中的音量

转载 作者:行者123 更新时间:2023-11-29 02:32:51 27 4
gpt4 key购买 nike

我想使用滑动手势来控制音量,如果我在屏幕上从上到下滑动,它应该会降低,而我从下到上滑动,它应该会增加音量。而不是使用音量按钮。

最佳答案

abstract class OnSwipeTouchListener implements OnTouchListener {

enum Direction{
LEFT, RIGHT, UP, DOWN;
}

private final static String TAG = "ClickFrame";
private final static int SWIPE_THRESHOLD = 100;

// 0: uninitialized
// 1: horizontal
// 2: vertical
private int initialGesture;

protected float initialX;
protected float initialY;
private float decidedX;
private float decidedY;
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
switch (action) {

case MotionEvent.ACTION_DOWN:
initialX = event.getX();
initialY = event.getY();
initialGesture = 0;
break;

case MotionEvent.ACTION_MOVE:
float deltaX;
float deltaY;
if(initialGesture == 0) {
deltaX = event.getX() - initialX;
deltaY = event.getY() - initialY;
}
else{
deltaX = event.getX() - decidedX;
deltaY = event.getY() - decidedY;
}

if(initialGesture == 0 && Math.abs(deltaX) > SWIPE_THRESHOLD){
initialGesture = 1;
decidedX = event.getX();
decidedY = event.getY();
if(deltaX > 0){
onBeforeMove(Direction.RIGHT);
}
else{
onBeforeMove(Direction.LEFT);
}
}
else if(initialGesture == 0 && Math.abs(deltaY) > SWIPE_THRESHOLD){
initialGesture = 2;
decidedX = event.getX();
decidedY = event.getY();
if(deltaY > 0){
onBeforeMove(Direction.DOWN);
}
else{
onBeforeMove(Direction.UP);
}
}

if(initialGesture == 1){
if(deltaX > 0){
onMove(Direction.RIGHT, deltaX);
}
else{
onMove(Direction.LEFT, -deltaX);
}
}
else if(initialGesture == 2){
if(deltaY > 0){
onMove(Direction.DOWN, deltaY);
}
else{
onMove(Direction.UP, -deltaY);
}
}

break;

case MotionEvent.ACTION_UP:
deltaX = event.getX() - initialX;
deltaY = event.getY() - initialY;

if(initialGesture == 0){
onClick();
return true;
}
onAfterMove();
initialGesture = 0;
return true;

case MotionEvent.ACTION_CANCEL:
break;

case MotionEvent.ACTION_OUTSIDE:
break;
}

return true;
}

public abstract void onMove(Direction dir, float diff);

public abstract void onClick();

public abstract void onAfterMove();

public abstract void onBeforeMove(Direction dir);
}

然后mClickFrame.setOnTouchListener(clickFrameSwipeListener);

关于android - 使用从上到下的滑动手势控制android中的音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48679812/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com