gpt4 book ai didi

android - 屏幕移动

转载 作者:行者123 更新时间:2023-11-29 02:41:16 25 4
gpt4 key购买 nike

我想要一个布局(种类无关紧要),即实际尺寸比屏幕的实际尺寸大乘以 5。我创建了一个 surfesview,它使用 Canvas 绘制我想要的东西,但是如何通过触摸移动屏幕以便能够看到屏幕的每个部分?我怎样才能知道点击了哪个 View (我希望点击时会发生什么)?

最佳答案

在我看来,您可能希望使用 OnTouchListenerView.onTouchEvent(MotionEvent ev) 来检测触摸并采取适当的行动:

例如:

myView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_UP:
// Perform click operation
return true;
case MotionEvent.ACTION_MOVE:
// Move to another area based on the event
return true;
}
return false;
}
});

或者在你的类里面:

int downX = -1;
int downY = -1;

public boolean onTouchEvent(MotionEvent event) {

int eventAction = event.getAction();

// You may need the x/y location
int x = (int)event.getX();
int y = (int)event.getY();

// put your code in here to handle the event
switch (eventAction) {
case MotionEvent.ACTION_DOWN:
downX = x;
downY = y;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
downX = -1;
downY = -1;
// decide what to do here about movement
break;
case MotionEvent.ACTION_MOVE:
int deltaX = x - downX;
int deltaY = y - downY;
// This is one way of doing it, it might not be exactly what you want, but you can start from her
setTranslationX(getTranslationX() + deltaX);
setTranslationY(getTranslationY() + deltaY);
break;
}

return true;
}

关于android - 屏幕移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43896998/

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