如何在屏幕上得到触摸位置(Android) [英] How to get Touch Location on Screen (Android)
问题描述
我正在开发中的Android游戏,在那场比赛我使用的画布显示圈(5 * 6)网格。 我要检查触摸区域的位置。例如,如果我触摸/点击屏幕上的左右两个相邻的圈,它应该返回位置,或者如果可能的话,应该显示这些界的行。对于我用这个onTouchEvent()方法。但我没有得到我的设备屏幕上的任何输出。我检查#2有给有方法,但它并不能帮助我,这里是链接(的如何在Android的触摸位置?)。我请你,请检查该code和建议的解决方案。这里是code文件:
包com.example.tap;进口android.os.Bundle;
进口android.annotation.Sup pressLint;
进口android.app.Activity;
进口android.content.Context;
进口android.gesture.Gesture;
进口android.graphics.Bitmap;
进口android.graphics.Bitmap.Config;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MotionEvent;
进口android.view.View;公共类MainActivity延伸活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(新板(本));
} 公共类板扩展视图
{
涂料pBack =新的油漆();
涂料PDOT =新的油漆();
INT XPOS = 0;
INT ypos = 0; INT COLS = 5;
INT行= 6; 公板(上下文的背景下)
{
超级(上下文);
pBack.setARGB(255,15,102,0);
pDot.setARGB(255,255,255,255);
} @燮pressLint(DrawAllocation)
保护无效的onDraw(帆布油画)
{
super.onDraw(画布);
canvas.drawPaint(pBack);
浮XSTEP = canvas.getWidth()/(COLS + 1);
浮yStep = canvas.getHeight()/(行+ 1);
浮curCirclXpos,curCirclYpos,lastCirclXpos = 0,lastCirclYpos = 0;
//浮动垂直=
//布尔中的onTap(手势克,点P)
// {
//
//} 对于(INT Y = 0; Y<行; Y ++)
{
为(中间体X = 0; X&下; COLS; X ++)
{
canvas.drawCircle((X + 1)* XSTEP,(Y + 1)* yStep,20,PDOT);
curCirclXpos = X;
curCirclYpos = Y; 如果(Y == 0)
{
//canvas.drawLine((x + 1)* XSTEP,yStep,(X + 1)* XSTEP,行* 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);
}
}
公共布尔onTouchEvent(MotionEvent五)
{
XPOS =(int)的e.getX();
ypos =(int)的e.getY();
开关(e.getAction())
{
案例MotionEvent.ACTION_DOWN:
案例MotionEvent.ACTION_UP:
案例MotionEvent.ACTION_MOVE:
//Log.d(\"Umar\",String.valueOf(xpos));
//Log.d(\"Farooq\",String.valueOf(ypos)); 打破; }
返回false;
}
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){ 。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
}
公共布尔onTouchEvent(MotionEvent五)
{
INT XPOS =(int)的e.getX();
INT ypos =(int)的e.getY();
开关(e.getAction())
{
案例MotionEvent.ACTION_DOWN:
Log.d(调试,在触摸(下)+将String.valueOf(XPOS)+将String.valueOf(ypos));
案例MotionEvent.ACTION_UP:
Log.d(调试,在触摸(上)+将String.valueOf(XPOS)+将String.valueOf(ypos));
案例MotionEvent.ACTION_MOVE:
Log.d(调试,在触摸(移动)+将String.valueOf(XPOS)+将String.valueOf(ypos));
打破;
}
返回true; }
然后重写绘制方法
@覆盖
公共无效画(油画画布){
super.draw(画布);
}
所以,你尝试
canvas.drawLine(startx的,startY,stopX,stopY,油漆);
为了使线直线上涨试试
canvas.drawLine(XPOS,ypos,XPOS,的getHeight(),新的油漆());
获取的高度应该是你的屏幕的高度(因为你的画线直线上升)
有关这个变量XPOS和ypos应该是提供给所有方法,这样添加
INT XPOS,ypos = 0;
要你的code的顶部(但在类声明中)
有关圆之间慢吞吞一个你需要得到第一个圆圈的x,触摸y位置,然后与他们设置的drawLine方法,所以是这样的:
currentCircleXpos,lastCircleXpos = 0; //你将在的onDraw方法时圆获取的绘制设置这些。
currentCircleYpos,lastCircleypos = 0;
canvas.drawLine(currentCircleXpos,currentCircleYpos,lastCircleXpos,lastCircleYpos,新的油漆());
的OnDraw()
方法-------
的for(int y = 0; Y<行; Y ++)
{
为(中间体X = 0; X&下; COLS; X ++)
{
canvas.drawCircle((X + 1)* XSTEP,(Y + 1)* yStep,20,PDOT);
currentCircleXpos = X;
currentCircleYpos = Y; //获取正在绘制圆的位置
如果(Y == 0)
{
//canvas.drawLine((x + 1)* XSTEP,yStep,(X + 1)* XSTEP,行* yStep,PDOT);
}
} //canvas.drawLine(xStep,(Y + 1)* yStep,COLS * XSTEP,(Y + 1)* yStep,PDOT);
}
I am developing game in android, in that game I am using canvas to show grid(5*6) of circles. I want to check location of Touch area. For Example if I touch/Tap on screen around two adjacent circles it should return location or if possible it should display line between those circles. For that I used this onTouchEvent() method. But I am not getting any output on my device's screen. I have checked on Stackoverflow there was method given there but it doesn't help me, here is the link(How to get the Touch position in android?). I request you to please check this code and suggest solution. Here is the code file:
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;
}
}
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;
}
Then override the draw method
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
}
So you try
canvas.drawLine(startX, startY, stopX, stopY, paint);
To make a line go straight up try
canvas.drawLine(xpos, ypos, xpos, getHeight() , new Paint());
Get height should be your screen's height (Since your drawing a line straight up)
For this the variables xpos and ypos should be available to all methods so add
int xpos,ypos = 0;
To the the top of your code (but within the class declaration)
For drawling a between the circles you would need to get the first circles x,y position on touch then set the drawLine method with them, so something like:
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() method-------
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);
}
这篇关于如何在屏幕上得到触摸位置(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!