android - 如何抓住像游戏这样的触摸事件:世界搜索

时间:2015-03-10 08:32:01

标签: matrix textview touch android-linearlayout

我有像WorldSearch on Store这样的游戏,我尝试使用这个解决方案: 我按TextView数组创建了矩阵,并在每个TouchEvent上添加了TextView。然后我通过MOTION.UPMOTION.DOWN抓住了事件。当我触及TextView并拖动它时,虽然它考虑了其他TextViews,但我只能得到第一个TextView的事件。 这是代码:

private void createMetrix(){

    // amound of item in a row and column
    int itemNum = 10;

    // calculate size of item
    ScreenDimension screenDimension = Utilities.getFinalScreenDimension(this);
    int itemSize = screenDimension.screenWidth / 10;

    // mContentLinear has VERTICAL orientation
    // then adding children linear layouts, these layout has HORIZONTAL orientation
    for(int i = 0; i < itemNum; i++) {
        LinearLayout theItemLayout = createHorizontalLayout(itemNum, itemSize, i);
        mContentLinear.addView(theItemLayout);
    }

}

private LinearLayout createHorizontalLayout(int itemNum, int itemSize, int row) {
    LinearLayout theLinear = new LinearLayout(this);
    theLinear.setOrientation(LinearLayout.HORIZONTAL);
    theLinear.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemSize));
    for(int i = 0; i < itemNum; i++){
        // add item metrix into linear layout having HORIZONTAL orientation
        TextView itemTxt = createItemMatrix(itemSize, row, i);
        theLinear.addView(itemTxt);
    }
    return  theLinear;
}
private TextView createItemMatrix(int itemSize, int row, int column) {
    TextView theItemTxt = new TextView(this);
    theItemTxt.setWidth(itemSize);
    theItemTxt.setHeight(itemSize);
    theItemTxt.setTextSize(13);
    theItemTxt.setGravity(Gravity.CENTER);
    theItemTxt.setText("A" + row + "" + column);
    String tag = row + "|" + column;
    theItemTxt.setTag(tag);

    theItemTxt.setOnTouchListener(mOnTouchListener);
    return theItemTxt;
}
// Listener
private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {

    private Rect rect;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v == null) return true;
        TextView itemTxt = (TextView)v;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
                Log.d("TEXT", "MOVE DOWN: " + itemTxt.getText().toString());
                return true;
            case MotionEvent.ACTION_UP:
                if (rect != null
                        && !rect.contains(v.getLeft() + (int) event.getX(),
                        v.getTop() + (int) event.getY())) {
                    // The motion event was outside of the view, handle this as a non-click event

                    return true;
                }
                // The view was clicked.
                Log.d("TEXT", "MOVE ON: " + itemTxt.getText().toString());
                // TODO: do stuff
                return true;
            case MotionEvent.ACTION_MOVE:
                Log.d("TEXT", "MOVE OUT: " + itemTxt.getText().toString());
                return true;
            default:
                return true;
        }
    }
};

有人对此案有任何想法吗?

谢谢, 莱恩

0 个答案:

没有答案