我有一个5x5网格(我在画布上绘制了一个矩阵)。我在网格中安排了textviews 按照array..ie(5X5)数组的顺序。我想在水平或垂直滑动时更改文本视图的值。
假设我从[4] [0]到[4] [2]连续在屏幕上使用触摸移动我想修改那些地方的文本视图的值。
有什么想法吗?
答案 0 :(得分: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]