问题描述
我正在 Android Studio 上开发一款游戏,其中用户触摸驱动图块进行组合。为此,我使用了一个分成行和列的 GridLayout。该值与 imageview 一起显示,并且所有 gridlayout 单元格都链接到包含 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 事件在如下所示的方法中的单独类中处理:
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 只是为了测试它是否有效。
应用程序加载但未从滑动事件中产生响应,有人对如何解决此问题有任何建议吗?
谢谢:)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)