gpt4 book ai didi

android - 在各种 Activity 中使用相同的 onTouchEvent

转载 作者:可可西里 更新时间:2023-11-01 19:06:22 25 4
gpt4 key购买 nike

我在我的 MainActivity.class 中使用了一个 onTouchEvent。它工作正常:如果用户用手指做一个双 L,我称之为 fragment 。我想在另一个 Activity 中使用此 onTouchEvent,但我认为如果我复制我的所有代码,它会很脏。

现在我为此创建了一个工具 TouchListenerImpl :

class TouchListenerImpl implements View.OnTouchListener {

private boolean movingDownL, movingDownR, movingLeft, movingRight, movingSuccessL, movingSuccessR = false;
private Point oldCoordsL, oldCoordsR, startPointL, startPointR = new Point(0, 0);
private boolean admin_touch = false;
private OnLTouch callback;

void setCallback(OnLTouch c) {
callback = c;
}

interface OnLTouch {
void lTouchSuccess();
}

@Override
public boolean onTouch(View v, MotionEvent event) {

Log.d("debugTouch", "onTouch");

int pIndexL = event.findPointerIndex(event.getPointerId(0));
int pIndexR = 0;

if(event.getPointerCount() > 1) pIndexR = event.findPointerIndex(event.getPointerId(1));

if(event.getPointerCount() > 1 && event.getX(pIndexL) > event.getX(pIndexR)) {
int tmp = pIndexR;
pIndexR = pIndexL;
pIndexL = tmp;
}

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
movingDownL = true;
movingDownR = true;
movingSuccessL = false;
movingSuccessR = false;

if(event.getPointerCount() > 1) {
startPointR = new Point((int) event.getX(pIndexR), (int) event.getY(pIndexR));
oldCoordsR = new Point((int) event.getX(pIndexR), (int) event.getY(pIndexR));
}

startPointL = new Point((int) event.getX(pIndexL), (int) event.getY(pIndexL));
oldCoordsL = new Point((int) event.getX(pIndexL), (int) event.getY(pIndexL));
break;
case MotionEvent.ACTION_MOVE:
int downMinDistance = 300;
int lnrInaccuracy = 10;
int downInaccuracy = 30;
if(event.getPointerCount() > 1) {
if(!movingDownR) {
if(Math.abs(oldCoordsR.x - event.getX(pIndexR)) < downInaccuracy &&
oldCoordsR.y < event.getY(pIndexR)) break;
if(Math.abs(oldCoordsR.y - event.getY(pIndexR)) < lnrInaccuracy &&
oldCoordsR.x > event.getX(pIndexR) && !movingRight) {
movingRight = true;
startPointR = new Point(new Point((int)event.getX(pIndexR), (int)event.getY(pIndexR)));
}
} else {
if (Math.abs(oldCoordsR.x - event.getX(pIndexR)) > downInaccuracy ||
oldCoordsR.y < event.getY(pIndexR)) {
movingDownR = false;
break;
} else if(findDistance(startPointR,
new Point((int)event.getX(pIndexR), (int)event.getY(pIndexR))) >= downMinDistance){
movingDownR = false;
}
}
}

if(!movingDownL) {
if(Math.abs(oldCoordsL.x - event.getX(pIndexL)) < downInaccuracy &&
oldCoordsL.y < event.getY(pIndexL)) break;
if(Math.abs(oldCoordsL.y - event.getY(pIndexL)) < lnrInaccuracy &&
oldCoordsL.x < event.getX(pIndexL) && !movingLeft) {
movingLeft = true;
startPointL = new Point(new Point((int)event.getX(pIndexL), (int)event.getY(pIndexL)));
}
}else {
if (Math.abs(oldCoordsL.x - event.getX(pIndexL)) > downInaccuracy ||
oldCoordsL.y > event.getY(pIndexL)) {
movingDownL = false;
break;
} else if(findDistance(startPointL,
new Point((int)event.getX(pIndexL), (int)event.getY(pIndexL))) >= downMinDistance){
movingDownL = false;
}
}

int lnrMinDistance = 50;
if(movingLeft) {
if (Math.abs(oldCoordsL.y - event.getY(pIndexL)) > lnrInaccuracy ||
oldCoordsL.x > event.getX(pIndexL)) {
movingLeft = false;
break;
} else if(findDistance(startPointL,
new Point((int)event.getX(pIndexL), (int)event.getY(pIndexL))) >= lnrMinDistance) {
movingLeft = false;
movingSuccessL = true;
}
}

if(movingRight) {
if (Math.abs(oldCoordsR.y - event.getY(pIndexR)) > lnrInaccuracy ||
oldCoordsR.x < event.getX(pIndexR)) {
movingRight = false;
break;
} else if(findDistance(startPointR,
new Point((int)event.getX(pIndexR), (int)event.getY(pIndexR))) >= lnrMinDistance) {
movingRight = false;
movingSuccessR = true;
}
}

if(movingSuccessL && movingSuccessR) {
if (!admin_touch)
{
admin_touch = true;

if (callback != null)
callback.lTouchSuccess();
}
}

oldCoordsL = new Point((int)event.getX(pIndexL), (int)event.getY(pIndexL));
oldCoordsR = new Point((int)event.getX(pIndexR), (int)event.getY(pIndexR));

break;
case MotionEvent.ACTION_UP:
movingDownL = false;
movingDownR = false;
movingLeft = false;
movingRight = false;
break;
default:
return false;
}
return true;
}

private double findDistance(Point p1, Point p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
}

在我的 Activity 中,我这样调用该工具:

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
TouchListenerImpl imp = new TouchListenerImpl();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imp.setCallback(new TouchListenerImpl.OnLTouch() {
@Override
public void lTouchSuccess() {
Log.d("debugTouch", "WORKING !");
}
});
}


@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("debugTouch", "onTouch");
return imp.onTouch(v, event);
}
}

问题是我从不在我的日志中输入。它根本不起作用...

最佳答案

您可以使用 @DrilonBlakqori 解决方案,稍作修改。

制作一个包含通用代码的单独类,但使用回调使 View 可见。

class TouchListenerImpl implements OnTouchListener {
private OnLTouch callback;
@Override
public boolean onTouch(View v, MotionEvent event) {
// all your code
...
if (callback != null)
callback.lTouchSuccess();
...
}

void setCallback(OnLTouch c) {
callback = c;
}

interface OnLTouch {
void lTouchSuccess();
}
}

在您的 MainActivity 中,创建 TouchListenerImplsetCallback 的新实例

TouchListenerImpl imp = new TouchListenerImpl();
imp.setCallback(new OnLTouch() {
public void lTouchSuccess() {
frameLayoutAdmin.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction()
.replace(R.id.framelayout_admin,new AdminLoginFragment())
.commit();
img_close.setVisibility(View.VISIBLE);
}
});

并且在 MainActivity 中,您要在 View 集合上检测 double LView View 上的此监听器。

view.setOnTouchListener(imp);

我假设您想在主布局上检测double L。为此你可以做

findViewById(R.id.mylayout).setOnTouchListener(imp);

希望这能解决您的问题。

关于android - 在各种 Activity 中使用相同的 onTouchEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42232409/

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