왼쪽 / 오른쪽과 위 / 아래 사이의 스 와이프 방향을 감지하는 방법
내 질문 : 사용자가 손가락을 위 / 아래 또는 왼쪽 / 오른쪽으로 움직일 때를 어떻게 감지합니까 (그리고 손가락이 움직 인 그룹의 방향을 어떻게 알 수 있습니까)?
내 상황 : 손가락을 위아래로 움직일 때 내 앱의 밝기를 변경하고 (위 = 더 밝게, 아래 = 더 어둡게), 왼쪽 / 오른쪽 스 와이프를 기준으로 활동 및 / 또는보기 사이를 전환하고 싶습니다.
SimpleOnGestureListener 클래스를 확장하기 만하면됩니다.
수업에서 이것을 선언하십시오.
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
가로 스 와이프의 예로서 아래 코드를 볼 수 있습니다.
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
return false;
}
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onLeftSwipe();
}
// left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onRightSwipe();
}
} catch (Exception e) {
}
return false;
}
}
수직 스 와이프 목적으로도 이와 유사하게 수행 할 수 있습니다.
나는 이것에 대한 간단한 수업을 썼다 : 문서화가 잘되어있어서 여기서는 설명하지 않겠다
public class OnSwipeListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Grab two events located on the plane at e1=(x1, y1) and e2=(x2, y2)
// Let e1 be the initial event
// e2 can be located at 4 different positions, consider the following diagram
// (Assume that lines are separated by 90 degrees.)
//
//
// \ A /
// \ /
// D e1 B
// / \
// / C \
//
// So if (x2,y2) falls in region:
// A => it's an UP swipe
// B => it's a RIGHT swipe
// C => it's a DOWN swipe
// D => it's a LEFT swipe
//
float x1 = e1.getX();
float y1 = e1.getY();
float x2 = e2.getX();
float y2 = e2.getY();
Direction direction = getDirection(x1,y1,x2,y2);
return onSwipe(direction);
}
/** Override this method. The Direction enum will tell you how the user swiped. */
public boolean onSwipe(Direction direction){
return false;
}
/**
* Given two points in the plane p1=(x1, x2) and p2=(y1, y1), this method
* returns the direction that an arrow pointing from p1 to p2 would have.
* @param x1 the x position of the first point
* @param y1 the y position of the first point
* @param x2 the x position of the second point
* @param y2 the y position of the second point
* @return the direction
*/
public Direction getDirection(float x1, float y1, float x2, float y2){
double angle = getAngle(x1, y1, x2, y2);
return Direction.fromAngle(angle);
}
/**
*
* Finds the angle between two points in the plane (x1,y1) and (x2, y2)
* The angle is measured with 0/360 being the X-axis to the right, angles
* increase counter clockwise.
*
* @param x1 the x position of the first point
* @param y1 the y position of the first point
* @param x2 the x position of the second point
* @param y2 the y position of the second point
* @return the angle between two points
*/
public double getAngle(float x1, float y1, float x2, float y2) {
double rad = Math.atan2(y1-y2,x2-x1) + Math.PI;
return (rad*180/Math.PI + 180)%360;
}
public enum Direction{
up,
down,
left,
right;
/**
* Returns a direction given an angle.
* Directions are defined as follows:
*
* Up: [45, 135]
* Right: [0,45] and [315, 360]
* Down: [225, 315]
* Left: [135, 225]
*
* @param angle an angle from 0 to 360 - e
* @return the direction of an angle
*/
public static Direction fromAngle(double angle){
if(inRange(angle, 45, 135)){
return Direction.up;
}
else if(inRange(angle, 0,45) || inRange(angle, 315, 360)){
return Direction.right;
}
else if(inRange(angle, 225, 315)){
return Direction.down;
}
else{
return Direction.left;
}
}
/**
* @param angle an angle
* @param init the initial bound
* @param end the final bound
* @return returns true if the given angle is in the interval [init, end).
*/
private static boolean inRange(double angle, float init, float end){
return (angle >= init) && (angle < end);
}
}
}
사용하려면 단순히 메서드 를 확장 OnSwipeListener
하고 재정의하십시오.onSwipe
Fernandour의 대답은 내가 그것을 사용하는 방법에 대한이 답변 쓰고, 완벽 Activity
하고 Fragment
많은 사람들이 찾고 있습니다로합니다.
public class MyActivity extends Activity implements View.OnTouchListener{
private RelativeLayout someLayout;
//take any layout on which you want your gesture listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetector=new GestureDetector(this,new OnSwipeListener(){
@Override
public boolean onSwipe(Direction direction) {
if (direction==Direction.up){
//do your stuff
Log.d(TAG, "onSwipe: up");
}
if (direction==Direction.down){
//do your stuff
Log.d(TAG, "onSwipe: down");
}
return true;
}
});
someLayout.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "onTouch: ");
gestureDetector.onTouchEvent(event);
return true;
}
}
주기 사용 예 에 대한 fernandohur 대답 위 :
뷰 중 하나에 OnSwipeListener를 적용하려면 다음과 같이하십시오.
이 뷰가 어디에 있든-다음과 같이 해당 뷰에 대한 터치 리스너를 설정하십시오.
myview.setOnTouchListener(this);
이제 활동의 OnCreate 또는 사용자 정의보기 생성자에서 다음을 수행하십시오.
// Global
private GestureDetectorCompat detector;
// In OnCreate or custome view constructor (which extends one of Android views)
detector = new GestureDetectorCompat(context, onSwipeListener);
다음과 같이 동일한 클래스에서 onTouch 이벤트를 재정의합니다.
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return detector.onTouchEvent(motionEvent);
}
또한 동일한 클래스에이 리스너 객체가 있습니다.
OnSwipeListener onSwipeListener = new OnSwipeListener() {
@Override
public boolean onSwipe(Direction direction) {
// Possible implementation
if(direction == Direction.left|| direction == Direction.right) {
// Do something COOL like animation or whatever you want
// Refer to your view if needed using a global reference
return true;
}
else if(direction == Direction.up|| direction == Direction.down) {
// Do something COOL like animation or whatever you want
// Refer to your view if needed using a global reference
return true;
}
return super.onSwipe(direction);
}
};
이 방법으로 해결했습니다.
viewPager.setOnTouchListener(new View.OnTouchListener() {
float prevX = -1;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (prevX != -1) {
if (event.getX() > prevX) {
if (viewPager.getCurrentItem() == 0) {
// Left to Right swipe
}
//Log.d("DEBUG", MotionEvent.ACTION_MOVE + ":" + event.getAction() + ":" + event.getActionMasked() + ":Left Swipe" + ":" + prevX + ":" + event.getX() + ":" + viewPager.getCurrentItem());
} else if (prevX > event.getX()) {
// Right to left swipe
//Log.d("DEBUG", MotionEvent.ACTION_MOVE + ":" + event.getAction() + ":" + event.getActionMasked() + ":Right Swipe" + ":" + prevX + ":" + event.getX() + ":" + viewPager.getCurrentItem());
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
prevX = event.getX();
} else {
prevX = -1;
}
return false;
}
});
그게 내가 한 방법, 가장 쉬운 방법
float initialX, initialY;
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
initialX = event.getX();
initialY = event.getY();
// Log.d(TAG, "Action was DOWN");
break;
case MotionEvent.ACTION_MOVE:
//Log.d(TAG, "Action was MOVE");
break;
case MotionEvent.ACTION_UP:
float finalX = event.getX();
float finalY = event.getY();
//Log.d(TAG, "Action was UP");
if (initialX < finalX) {
// Log.d(TAG, "Left to Right swipe performed");
}
if (initialX > finalX) {
// Log.d(TAG, "Right to Left swipe performed");
}
if (initialY < finalY) {
// Log.d(TAG, "Up to Down swipe performed");
}
if (initialY > finalY) {
// Log.d(TAG, "Down to Up swipe performed");
}
break;
case MotionEvent.ACTION_CANCEL:
//Log.d(TAG,"Action was CANCEL");
break;
case MotionEvent.ACTION_OUTSIDE:
// Log.d(TAG, "Movement occurred outside bounds of current screen element");
break;
}
return super.onTouchEvent(event);
}
사용 가능한 답변은 이러한 간단한 문제에 대해 너무 복잡합니다. 나는 그것에 대한 다른 접근 방식을 제안합니다 (코드는 as3이지만 아이디어를 얻을 수 있습니다).
var touchDistance:Number = Point.distance(_moveTouchPoint, _startTouchPoint);
if (touchDistance >= SWIPE_MIN_DISTANCE)
{
var xDiff:Number = _moveTouchPoint.x - _startTouchPoint.x;
var yDiff:Number = _moveTouchPoint.y - _startTouchPoint.y;
var yGreater:Boolean = Math.abs(yDiff) >= Math.abs(xDiff);
if (yGreater)
{
// direction is up or down
changePlayerDirectionTo(yDiff < 0 ? DIRECTION_UP : DIRECTION_DOWN);
}
else
{
// direction is left or right
changePlayerDirectionTo(xDiff < 0 ? DIRECTION_LEFT : DIRECTION_RIGHT)
}
}
각각의 경우 x 또는 y는 절대 값이 더 커지며 특정 방향 설정으로 해석 될 수 있습니다. 그때부터 좌표 기호를 사용하여 정확히 어느 방향을 감지 할 수 있습니다.
SimpleGestureListener를 재정의하고 시작 끝 현재 좌표 간의 차이를 계산할 수 있습니다.
private class GestureListener extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (e2.getY() > e1.getY()) {
// direction up
}else {
// direction down
}
if (e2.getX() > e1.getX()) {
// direction right
}else {
// direction left
}
return true;
}
}
이 작업을 수행하는 bitbucket에 오픈 소스 제스처 라이브러리가 있습니다. 이 라이브러리에는 'HGFling'클래스가 있습니다. 이것은 플링의 방향을 감지하는 방법을 보여줍니다. https://bitbucket.org/warwick/hacergestov3 에서 라이브러리를 다운로드 할 수 있습니다 . 오픈 소스입니다.
@Fernandour 답변에 대한 Kotlin
구현 추가 . Java의 경우 @f arhan patel 답변을 살펴보십시오. 어려웠 기 때문에 이것을 추가하고 있습니다. 누군가의 시간을 절약하기를 바랍니다.
class ClientFragment : Fragment(), View.OnTouchListener {
private lateinit var gestureDetector: GestureDetector
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
Log.d(TAG, "onTouch: ");
gestureDetector.onTouchEvent(event);
return true
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
...
gestureDetector = GestureDetector(activity, object : OnSwipeListener() {
override fun onSwipe(direction: Direction): Boolean {
when(direction){
Direction.up ->
{
Log.d(TAG, "onSwipe: up")
sendCommand("UP")
return true
}
Direction.down ->{
Log.d(TAG, "onSwipe: down")
sendCommand("DOWN")
return true
}
Direction.left ->
{
Log.d(TAG, "onSwipe: left")
sendCommand("LEFT")
return true
}
Direction.right ->{
Log.d(TAG, "onSwipe: right")
sendCommand("RIGHT")
return true
}
else -> {
}
}
return true
}
})
dpadLayout.setOnTouchListener(this)
@Fernandour의 답변 의 확장 버전 은 터치 리스너로 직접 구현하기 쉽고 추가 코드를 수행하지 않습니다. 터치시, 길게 누름, 두 번 클릭 구현 ....
public class OnSwipeListener implements View.OnTouchListener{
public enum Direction{up,down,left,right;}
private GestureDetector gestureDetector;
private Context context;
public OnSwipeListener(Context c) {
this.context = c;
gestureDetector = new GestureDetector(c, new GestureListener(c));
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeUp() {
}
public void onSwipeDown() {
}
public void onClick() {
}
public void onDoubleClick() {
}
public void onLongClick() {
}
public double getAngle(float x1, float y1, float x2, float y2) {
double rad = Math.atan2(y1-y2,x2-x1) + Math.PI;
return (rad*180/Math.PI + 180)%360;
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
Context context;
public GestureListener(Context c) {
this.context = c;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
onClick();
return super.onSingleTapUp(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
onDoubleClick();
return super.onDoubleTap(e);
}
@Override
public void onLongPress(MotionEvent e) {
onLongClick();
super.onLongPress(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float x1 = e1.getX();
float y1 = e1.getY();
float x2 = e2.getX();
float y2 = e2.getY();
Direction direction = getDirection(x1,y1,x2,y2);
return onSwipe(direction);
}
public Direction getDirection(float x1, float y1, float x2, float y2){
double angle = getAngle(x1, y1, x2, y2);
return fromAngle(angle);
}
public Direction fromAngle(double angle){
if(inRange(angle, 45, 135)){
onSwipeUp();
return Direction.up;
}
else if(inRange(angle, 0,45) || inRange(angle, 315, 360)){
onSwipeRight();
return Direction.right;
}
// else if(inRange(angle, 225, 315)){
// //onSwipeDown();
//
// }
else if(inRange(angle,135, 225)){
onSwipeLeft();
return Direction.left;
}
else {
return Direction.down;
}
}
private boolean inRange(double angle, float init, float end){
return (angle >= init) && (angle < end);
}
public boolean onSwipe(Direction direction){
return false;
}
};
}
'programing' 카테고리의 다른 글
자바 InputStream 모의 (0) | 2021.01.16 |
---|---|
삼항 연산자에 사용하는 코딩 스타일은 무엇입니까? (0) | 2021.01.16 |
Spark : Spark Shell에서 Spark 파일을 실행하는 방법 (0) | 2021.01.16 |
인수 밑줄이있는 디 바운스 함수 (0) | 2021.01.16 |
.NET에서 이중 곱셈이 끊어 집니까? (0) | 2021.01.16 |