2014-03-31 104 views
0

我正在開發Android遊戲,在那個遊戲中我使用畫布來顯示網格(5 * 6)的圓圈。 我想檢查觸摸區域的位置。例如,如果我觸摸/點擊屏幕上相鄰的兩個圓圈,它應該返回位置,或者如果可能的話,它應該在這些圓圈之間顯示線條。爲此,我使用了這個onTouchEvent()方法。但是我在設備的屏幕上沒有收到任何輸出。我檢查了Stackoverflow那裏有方法,但它並沒有幫助我,這裏是鏈接(How to get the Touch position in android?)。我請求您請檢查此代碼並提出解決方案。下面是代碼文件:如何獲得屏幕上的觸摸位置(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

你需要一些代碼添加到您的切換事件,目前你的代碼將最有可能獲得的X和Y值,但什麼都不做與他們 試試「Log.d(」調試」,將String.valueOf(XPOS));」並檢查Logcat,看它是否顯示X觸摸的聯合。 – Pheonix2105

+0

是的,它的工作。什麼是屏幕的打印語句。 –

+0

您需要使用觸摸事件中的協調工具來繪製您希望在屏幕上繪製的任何內容。我會在下面更新我的答案。 – Pheonix2105

回答

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; 

    } 

然後覆蓋draw方法

@Override 
    public void draw(Canvas canvas) { 
    super.draw(canvas); 
    } 

所以你想

canvas.drawLine(startx的,startY,STOPX,stopY,油漆);

爲了使線走直線上升嘗試

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); 
} 
+0

現在我想展示兩個圈子之間的界限,我將如何做到這一點? –

+0

你可以看到我的問題。我試圖畫線,不使用任何觸摸事件。 –

+0

,那些線條在水平和垂直的第1個圓圈到最後一個圓圈之間,現在我試圖在發生觸摸事件之後僅在兩個圓圈之間顯示線條。 –

相關問題