gpt4 book ai didi

java - 长按触摸事件

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:58 26 4
gpt4 key购买 nike

我有三个按钮,它们是根据特定边界内的触摸事件点击的,我想更改它,以便触摸事件可以是长按而不是点击。我不知道该怎么做。这是我的相关代码:

private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
// 1. All touch input is handled here:
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);

if (event.type == TouchEvent.TOUCH_DOWN) {

if (inBounds(event, 340,512,140,140)) {
B1Pressed = true;
Log.d("GameScreen", "Button 1 Pressed");
// Button 1
}

if (inBounds(event, 340,320,140,140)) {
B2Pressed = true;
Log.d("GameScreen", "Button 2 Pressed");
// Button 2
}

if (inBounds(event, 340,120,140,140)) {
B3Pressed = true;
Log.d("GameScreen", "Button 3 Pressed");
// Button 3
}

}

private boolean inBounds(TouchEvent event, int x, int y, int width,
int height) {
if (event.x > x && event.x < x + width - 1 && event.y > y
&& event.y < y + height - 1)
return true;
else
return false;
}

输入.java:

 public interface Input {

public static class TouchEvent {
public static final int TOUCH_DOWN = 0;
public static final int TOUCH_UP = 1;
public static final int TOUCH_DRAGGED = 2;
public static final int TOUCH_HOLD = 3;

public int type;
public int x, y;
public int pointer;


}

public boolean isTouchDown(int pointer);

public int getTouchX(int pointer);

public int getTouchY(int pointer);

public List<TouchEvent> getTouchEvents();
}

TouchHandler.java:

public interface TouchHandler extends OnTouchListener {
public boolean isTouchDown(int pointer);

public int getTouchX(int pointer);

public int getTouchY(int pointer);

public List<TouchEvent> getTouchEvents();
}

单点触摸处理程序:

 public class SingleTouchHandler implements TouchHandler {
boolean isTouched;
int touchX;
int touchY;
Pool<TouchEvent> touchEventPool;
List<TouchEvent> touchEvents = new ArrayList<TouchEvent>();
List<TouchEvent> touchEventsBuffer = new ArrayList<TouchEvent>();
float scaleX;
float scaleY;

public SingleTouchHandler(View view, float scaleX, float scaleY) {
PoolObjectFactory<TouchEvent> factory = new PoolObjectFactory<TouchEvent>() {
@Override
public TouchEvent createObject() {
return new TouchEvent();
}
};
touchEventPool = new Pool<TouchEvent>(factory, 100);
view.setOnTouchListener(this);

this.scaleX = scaleX;
this.scaleY = scaleY;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
synchronized(this) {
TouchEvent touchEvent = touchEventPool.newObject();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchEvent.type = TouchEvent.TOUCH_DOWN;
isTouched = true;
break;
case MotionEvent.ACTION_MOVE:
touchEvent.type = TouchEvent.TOUCH_DRAGGED;
isTouched = true;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
touchEvent.type = TouchEvent.TOUCH_UP;
isTouched = false;
break;
}

touchEvent.x = touchX = (int)(event.getX() * scaleX);
touchEvent.y = touchY = (int)(event.getY() * scaleY);
touchEventsBuffer.add(touchEvent);

return true;
}
}

@Override
public boolean isTouchDown(int pointer) {
synchronized(this) {
if(pointer == 0)
return isTouched;
else
return false;
}
}

@Override
public int getTouchX(int pointer) {
synchronized(this) {
return touchX;
}
}

@Override
public int getTouchY(int pointer) {
synchronized(this) {
return touchY;
}
}

@Override
public List<TouchEvent> getTouchEvents() {
synchronized(this) {
int len = touchEvents.size();
for( int i = 0; i < len; i++ )
touchEventPool.free(touchEvents.get(i));
touchEvents.clear();
touchEvents.addAll(touchEventsBuffer);
touchEventsBuffer.clear();
return touchEvents;
}
}
}

我该如何实现长按?

最佳答案

让你的类实现 View.OnLongClickListener 为

  SingleTouchHandler Implements View.OnLongClickListener
@Override
public boolean onLongClick(View v) {
return false;
}

您将获得执行长按操作的方法希望这会有所帮助

关于java - 长按触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36919070/

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