gpt4 book ai didi

android - 如何从屏幕左端检测android滑动/滑动菜单事件?

转载 作者:行者123 更新时间:2023-11-29 15:49:08 24 4
gpt4 key购买 nike

我没有 slider 菜单/抽屉,但我想使用 slider 事件,通常应用程序会在屏幕左侧显示一个抽屉。当用户做这个手势时,我需要做一些不同的任务。

是否可以监听这个事件?如果是怎么办?

最佳答案

import android.app.Activity;
import android.util.Log;
import android.view.MotionEvent;

public class SlidableActivity extends Activity {
protected static final String TAG = "SlidableActivity";
private static final int ACTION_TYPE_DEFAULT = 0;
private static final int ACTION_TYPE_UP = 1;
private static final int ACTION_TYPE_RIGHT = 2;
private static final int ACTION_TYPE_DOWN = 3;
private static final int ACTION_TYPE_LEFT = 4;
private static final int SLIDE_RANGE = 100;
private float mTouchStartPointX;
private float mTouchStartPointY;
private int mActionType = ACTION_TYPE_DEFAULT;

@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouchStartPointX = event.getRawX();
mTouchStartPointY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
if (mTouchStartPointX - x > SLIDE_RANGE) {
mActionType = ACTION_TYPE_LEFT;
} else if (x - mTouchStartPointX > SLIDE_RANGE) {
mActionType = ACTION_TYPE_RIGHT;
} else if (mTouchStartPointY - y > SLIDE_RANGE) {
mActionType = ACTION_TYPE_UP;
} else if (y - mTouchStartPointY > SLIDE_RANGE) {
mActionType = ACTION_TYPE_DOWN;
}
break;
case MotionEvent.ACTION_UP:
if (mActionType == ACTION_TYPE_UP) {
slideUp();
} else if (mActionType == ACTION_TYPE_RIGHT) {
slideToRight();
} else if (mActionType == ACTION_TYPE_DOWN) {
slideDown();
} else if (mActionType == ACTION_TYPE_LEFT) {
slideToLeft();
}
break;
default:
break;
}
return true;
}

protected void slideToLeft() {
Log.d(TAG, "slideToLeft() was called.");
}

protected void slideToRight() {
Log.d(TAG, "slideToRight() was called.");
}

protected void slideUp() {
Log.d(TAG, "slideUp() was called.");
}

protected void slideDown() {
Log.d(TAG, "slideDown() was called.");
}
}

检查这个 link也是

关于android - 如何从屏幕左端检测android滑动/滑动菜单事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31399514/

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