1

我有一个 5x5 网格(我在画布上绘制了一个矩阵)。我已经按照数组的顺序排列了网格中的文本视图。即(5X5)数组。问题是我想在水平或垂直滑动时更改文本视图的值。

假设我在屏幕上连续使用从 [4][0] 到 [4][2] 的触摸移动,我想修改这些地方的文本视图的值。

有任何想法吗?

4

1 回答 1

1

这段代码可能会对您有所帮助:

public class MyClass extends Activity implements OnGestureListener{
    ArrayList<TextView> tvs = new ArrayList<TextView>();
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.mygrid);
    }

    public void onResume() {
        super.onResume();
        GestureOverlayView gd = (GestureOverlayView)findViewById(R.id.gd);
        LinearLayout g = (LinearLayout)findViewById(R.id.mygrid);
        gd.addOnGestureListener(this);
        g.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < 5; i++) {
            LinearLayout row = new LinearLayout(this);
            g.addView(row);
            row.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams rowlp = (LayoutParams) row.getLayoutParams();
            rowlp.width = LayoutParams.MATCH_PARENT;
            rowlp.weight = 1;
            rowlp.height = LayoutParams.WRAP_CONTENT;
            row.setLayoutParams(rowlp);
            for (int j = 0; j < 5; j++) {
                TextView tv = new TextView(this);
                row.addView(tv);
                LinearLayout.LayoutParams lp = (LayoutParams) tv.getLayoutParams();
                lp.height = LayoutParams.MATCH_PARENT;
                lp.width = 0;
                lp.weight = 1;
                tv.setLayoutParams(lp);
                tv.setText(String.valueOf(i * 5 + j));
                tv.setTag(String.valueOf(i * 5 + j));
                tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
                tvs.add(tv);
                if ((i * 5 + j) % 2 == 0)
                    tv.setBackgroundColor(0xff888888);
            }
        }
    }

    private boolean isPointInsideView(float x, float y, View view){
        int location[] = new int[2];
        view.getLocationOnScreen(location);
        int viewX = location[0];
        int viewY = location[1];

        //point is inside view bounds
        if(( x > viewX && x < (viewX + view.getWidth())) &&
                ( y > viewY && y < (viewY + view.getHeight()))){
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onGesture(GestureOverlayView overlay, MotionEvent event) {
        float x = event.getRawX();
        float y = event.getRawY();
        for (TextView v : tvs) {
            if (isPointInsideView(x, y, v)) {
                v.setText("HERE");
            }
        }
    }

    @Override
    public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {

    }

    @Override
    public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {

    }

    @Override
    public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {

    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gd"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/mygrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.gesture.GestureOverlayView>

希望能帮助到你。

Edit2:用这些方法替换上述方法以提高性能:

private TextView getAbove(float x, float y) {
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            if (isPointInsideView(x, y, tvs[i][j]))
                return tvs[i][j];
    return null;
}

@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
    float x = event.getRawX();
    float y = event.getRawY();
    if (last == null || !isPointInsideView(x, y, last)) {
        last = getAbove(x,y);
        draw();
    }   
}

private void draw() {
    if (last == null)
        return;
    last.setText("+");
}
private void reset() {
    last = null;
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            tvs[i][j].setText(String.valueOf(i * 5 + j));
}

@Override
public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
    reset();
}
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
    reset();
}
@Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
    last = getAbove(event.getRawX(), event.getRawY());
    draw();
}

TextView[][] tvs = new TextView[5][5];
TextView last = null;

并将其arraylist制成TextView[5][5]

于 2013-01-22T16:20:31.200 回答