繁体   English   中英

Android Studio - 如何在 android 游戏中使用实现滑动手势

[英]Android Studio - How to use implement swipe gesture in android game

我正在 Android 工作室开发一款游戏,用户触摸驱动瓷砖组合。 为此,我使用了分隔为行和列的 GridLayout。 该值与 imageview 一起显示,并且所有网格布局单元都链接到一个单独的单元 class,其中包括 imageview ID。 我在每个图像视图上实现了一个侦听器,以检测何时发生滑动动作。 初始化板子和监听器的代码如下:

for(int i = 0; i<noOfBlocks; i++) {
    for (int j = 0; j < noOfBlocks; j++) {
        ImageView imageView = new ImageView(this);
        imageView.setId(iDcnt);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(widthOfBlock, widthOfBlock));
        imageView.setMaxHeight(widthOfBlock);
        imageView.setMaxWidth(widthOfBlock);
        int randomNum = (int) Math.floor(random.nextDouble() * gamepiece.length);
        imageView.setImageResource(gamepiece[randomNum]);
        Cell c = new Cell(i + 1, j+1, randomNum, imageView.getId());
        imageView.setOnTouchListener(new View.OnTouchListener() {
                                         @Override
                                         public boolean onTouch(View v, MotionEvent event) {
                                             SquarePlay s = null;
                                             try {
                                                 s = new SquarePlay();
                                             } catch (ClassNotFoundException e) {
                                                 e.printStackTrace();
                                             } catch (NoSuchMethodException e) {
                                                 e.printStackTrace();
                                             }
                                             s.onSwipeEvent(event);
                                             return false;
                                         }
                                     });
                gameBoard.addView(imageView);
        iDcnt++;
    }

OnTouch 事件在单独的 class 中处理,方法如下所示:

    float x1, x2, y1, y2, dx, dy;

public void onSwipeEvent(MotionEvent event){
switch(event.getAction()) {
    case(MotionEvent.ACTION_DOWN):
        x1 = event.getX();
        y1 = event.getY();
        break;

    case(MotionEvent.ACTION_UP): {
        x2 = event.getX();
        y2 = event.getY();
        dx = x2-x1;
        dy = y2-y1;

        if(Math.abs(dx) > Math.abs(dy)) {
            if(dx>0)
                onSwipeRight();
            else
                onSwipeLeft();
        } else {
            if(dy>0)
                onSwipeDown();
            else
                onSwipeUp();
        }
    }
}

}

private void onSwipeLeft() {
    System.exit(0);
}

private void onSwipeRight() {
    System.exit(0);
}

private void onSwipeUp() {
    System.exit(0);
}

private void onSwipeDown() {
    System.exit(0);
}

注意:System.exit 只是为了测试它是否有效。

应用程序加载但不会从滑动事件中产生响应,有人对如何解决此问题有任何建议吗?

谢谢:)

要使用滑动,您可以在活动 class 中创建 class,如下所示##

在活动 class 中声明 class 名称

    SwipeListener swipeListener;

在活动 class 中编写此代码来处理滑动##

private class SwipeListener implements View.OnTouchListener {
    //create gesture detector variable
    GestureDetector gestureDetector;

    //create constructor
    SwipeListener(View view){
        int threshold = 100;
        int velocity_threshold = 100;
        GestureDetector.SimpleOnGestureListener listener =
                new GestureDetector.SimpleOnGestureListener(){
                    @Override
                    public boolean onDown(MotionEvent e) {
                        return super.onDown(e);
                    }

                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                        //get x and y difference
                        float xDiff = e2.getX() - e1.getX();
                        float yDiff = e2.getY() - e1.getY();

                        try {
                            if(Math.abs(xDiff)>Math.abs(yDiff)){
                                if(Math.abs(xDiff)>threshold &&
                                        Math.abs(velocityX)> velocity_threshold){
                                    if(xDiff > 0){
                                        //swipe right
                                        Intent intent = new Intent(TopUpBalance.this, HomePage.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();
                                    }else{
                                        //swipe left
                                    }
                                    return true;
                                }//you can handle swipe down and above here as well, the same way above
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        return false;
                    }
                };
        //init gesture

        gestureDetector = new GestureDetector(listener);

        view.setOnTouchListener(this);



    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }
}

如下声明 SwipeListener onCreate 方法##

  linearLayout = findViewById(R.id.layoutID);

    //swipe
    swipeListener = new SwipeListener(linearLayout);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM