我在android中开发游戏,在那个游戏中我使用画布来显示圆圈的网格(5 * 6)。 我想查看触摸区域的位置。例如,如果我触摸/点击两个相邻圆圈周围的屏幕,它应该返回位置,或者如果可能的话,它应该在这些圆圈之间显示线条。为此,我使用了这个onTouchEvent()方法。但我的设备屏幕上没有输出。我已经检查了Stackoverflow,那里有方法,但它没有帮助我,这里是链接(How to get the Touch position in android?)。我请求您检查此代码并提出解决方案建议。这是代码文件:
package com.example.tap;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.gesture.Gesture;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new board(this));
}
public class board extends View
{
Paint pBack = new Paint();
Paint pDot = new Paint();
int xpos=0;
int ypos=0;
int cols = 5;
int rows = 6;
public board(Context context)
{
super(context);
pBack.setARGB(255, 15, 102, 0);
pDot.setARGB(255, 255, 255, 255);
}
@SuppressLint("DrawAllocation")
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawPaint(pBack);
float xStep = canvas.getWidth() / (cols + 1);
float yStep = canvas.getHeight() / (rows + 1);
float curCirclXpos, curCirclYpos, lastCirclXpos = 0, lastCirclYpos =0;
//float vertical=
// boolean onTap(Gesture g, Point p)
// {
//
// }
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
canvas.drawCircle((x + 1) * xStep, (y + 1) * yStep, 20, pDot);
curCirclXpos=x;
curCirclYpos=y;
if (y == 0)
{
//canvas.drawLine((x + 1) * xStep, yStep, (x + 1) * xStep, rows * yStep, pDot);
//canvas.drawLine(xpos, ypos, xpos, yStep, pDot);
canvas.drawLine(curCirclXpos, curCirclYpos, lastCirclXpos, lastCirclYpos, pDot);
}
}
//canvas.drawLine(xStep, (y + 1) * yStep, cols * xStep, (y + 1) * yStep, pDot);
}
}
public boolean onTouchEvent(MotionEvent e)
{
xpos=(int) e.getX();
ypos=(int) e.getY();
switch (e.getAction())
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_MOVE:
//Log.d("Umar",String.valueOf(xpos));
//Log.d("Farooq",String.valueOf(ypos));
break;
}
return false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:2)
public boolean onTouchEvent(MotionEvent e)
{
int xpos=(int) e.getX();
int ypos=(int) e.getY();
switch (e.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d("DEBUG", "On touch (down)" + String.valueOf(xpos) + String.valueOf(ypos));
case MotionEvent.ACTION_UP:
Log.d("DEBUG", "On touch (up)" + String.valueOf(xpos) + String.valueOf(ypos));
case MotionEvent.ACTION_MOVE:
Log.d("DEBUG", "On touch (move)" + String.valueOf(xpos) + String.valueOf(ypos));
break;
}
return true;
}
然后覆盖绘制方法
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
}
所以你试试
canvas.drawLine(startX,startY,stopX,stopY,paint);
要直线上线,请尝试
canvas.drawLine(xpos, ypos, xpos, getHeight() , new Paint());
获取高度应该是你屏幕的高度(因为你直接画了一条线)
为此,变量xpos和ypos应该可用于所有方法,因此添加
int xpos,ypos = 0;
到代码的顶部(但在类声明中)
对于在圆圈之间进行绘制,您需要在触摸时获取第一个圆x,y位置,然后使用它们设置drawLine方法,如下所示:
currentCircleXpos, lastCircleXpos = 0; //you would set these in the onDraw method when the circle get's drawn.
currentCircleYpos, lastCircleypos = 0;
canvas.drawLine(currentCircleXpos, currentCircleYpos, lastCircleXpos, lastCircleYpos , new Paint());
的OnDraw() 方法-------
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
canvas.drawCircle((x + 1) * xStep, (y + 1) * yStep, 20, pDot);
currentCircleXpos = x;
currentCircleYpos = y; //gets the circle that is being drawn's location
if (y == 0)
{
//canvas.drawLine((x + 1) * xStep, yStep, (x + 1) * xStep, rows * yStep, pDot);
}
}
//canvas.drawLine(xStep, (y + 1) * yStep, cols * xStep, (y + 1) * yStep, pDot);
}