按钮的 GridView 的滑动触摸界面

Swipe-touch interface for a GridView of Buttons

有谁知道(或认识其他人)如何为 Android 可显示小部件(即按钮、ImageView 图像等)的 GridView 实现滑动触摸界面,而不仅仅是 GridView比如说,一个字符串数组? Bejeweled 或 Candy Crush 就是一个很好的例子,说明用户如何能够通过滑动来交换图块。

Here's my post in more detail.

因为,我目前正在开发一款基于经典益智游戏之一 Puzzle-15 的应用程序,并且我正在使用方向按钮(上、下、左、右)来交换数字图块(按钮对象)在黑板上(GridView)...

Here's my app.

非常感谢!

好的,对于透明视图,这是我要做的:

xml 布局(我把自定义视图放在按钮上):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test Button"/>

<com.example.trist_000.teststack.CustomView
    android:id="@+id/customv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

您需要一个可以处理滑动的自定义视图:

public class CustomView extends View implements View.OnTouchListener {


float save_x = 0;
float save_y = 0;

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


    if (mSwipeInterface == null)
        return false;

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        save_x = event.getX();
        save_y = event.getY();
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        float difx = event.getX() - save_x;
        float dify = event.getY() - save_y;

        if (IsBigger(difx, dify)) {
            if (difx > -30 && difx < 30)return false;
            if (difx > 0){
                mSwipeInterface.swipe(SwipeEnum.RIGHT);
            }
            else{
                mSwipeInterface.swipe(SwipeEnum.LEFT);
            }
        }
        else{
            if (dify > -30 && dify < 30)return false;
            if (dify > 0){
                mSwipeInterface.swipe(SwipeEnum.DOWN);
            }
            else{
                mSwipeInterface.swipe(SwipeEnum.TOP);
            }
        }


    }
    return true;
}

private boolean IsBigger(float difx, float dify) {
    if (difx < 0) {
        difx *= -1;
    }
    if (dify < 0) {
        dify *= -1;
    }

    if (difx > dify){
        return true;
    }
    return false;
}

public enum SwipeEnum {
    LEFT,
    DOWN,
    RIGHT,
    TOP;
}

interface swipeInterface {
    public void swipe(SwipeEnum swipeEnum);
}

private swipeInterface mSwipeInterface = null;

public void setSwipeListener(swipeInterface swipe) {
    mSwipeInterface = swipe;
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setOnTouchListener(this);
}

public CustomView(Context context) {
    super(context);
}
}

主要 Activity:

public class MainActivity extends Activity {


Button mButton;
CustomView mCustomView;

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

    setContentView(R.layout.activity_main);
    mButton = (Button)findViewById(R.id.button);
    mCustomView = (CustomView)findViewById(R.id.customv);

    mCustomView.setSwipeListener(new CustomView.swipeInterface() {
        @Override
        public void swipe(CustomView.SwipeEnum swipeEnum) {
            if (swipeEnum == CustomView.SwipeEnum.DOWN){
                mButton.setY(200);
            }
            if (swipeEnum == CustomView.SwipeEnum.TOP){
                mButton.setY(0);
            }
            if (swipeEnum == CustomView.SwipeEnum.RIGHT){
                mButton.setX(200);
            }
            if (swipeEnum == CustomView.SwipeEnum.LEFT){
                mButton.setX(0);
            }
        }
    });
}
}

我只是放了一个按钮,但只是放了你的 gridview。并用您自己的逻辑替换我在 MainActivity 中的不同 "if" 中的内容。

如果您对代码有任何疑问,请立即联系我。

PS:Algos 可能不是最优的 lol 但我测试过它有效,你可以试试 ;)