【问题标题】:How to get Touch Location on Screen (Android)如何在屏幕上获取触摸位置 (Android)
【发布时间】:2014-03-31 11:03:42
【问题描述】:

我正在用 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;
    }


}

【问题讨论】:

  • 您需要在 switch 事件中添加一些代码,目前您的代码很可能会获取 X 和 Y 值,但对它们不做任何事情尝试 "Log.d("DEBUG", String.valueOf (xpos));"并检查 Logcat 看它是否在触摸时显示 X 坐标。
  • 是的,它奏效了。 screen 的打印语句是什么。
  • 您需要使用触摸事件中的坐标来绘制您希望在屏幕上绘制的任何内容。我将在下面更新我的答案。
  • 我需要垂直或水平地画一条线。这条线应该只在两个圆圈之间,不超过两个。
  • 如果你更新你的答案,那将是有利的。

标签: android android-canvas touch-event


【解决方案1】:
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;

    }

然后重写draw方法

 @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; 

到代码的顶部(但在类声明中)

要在圆圈之间绘制 a,您需要在触摸时获取第一个圆圈 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);
}

【讨论】:

  • 现在我想在两个圆圈之间显示一条线,我该怎么做?
  • 你可以看到我的问题。我试着画线,没有使用任何触摸事件。
  • 这些线在水平和垂直方向的第一个圆圈到最后一个圆圈之间,现在我试图在发生触摸事件后仅显示两个圆圈之间的线。
  • 你可以使用相同的代码,但改变drawLine方法,我会再次更新给你一个粗略的想法。
  • 现在请检查代码。不点击屏幕它会在我设备的屏幕左上角显示一条小线。
猜你喜欢
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多