2014-02-21 59 views
0

對,我有三個類'遊戲','GridView'和'TokenView'(儘管只有第一個和最後一個類都需要幫助:D),我正在嘗試這裏做的是使用onTouch方法獲取我的'TokenView'類中的哪個列(getY)。Android畫布onTouch

遊戲:

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view); 

    int[][] pos = new int[mColumn][mRow]; 

    /*Setting ball movement frame*/ 
    FrameLayout tokenFrame = (FrameLayout) findViewById(R.id.widget39); 
    TokenView token = new TokenView(this); 
    tokenFrame.addView(token); 


    /*Setting drop area*/ 
    FrameLayout gridFrame = (FrameLayout) findViewById(R.id.widget41); 
    GridView gridArea = new GridView(this); 
    gridFrame.addView(gridArea); 
} 
public int getPlayer(){ 
    return player; 
} 

public boolean onTouch(View v, MotionEvent event){ 

    if(){ 
     //where I need help :D 
    } 

    return false; 
} 

TokenView:

public class TokenView extends View { 

int Columns = 7; 
public TokenView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
} 

protected void onDraw(Canvas canvas) 
{ 
    super.onDraw(canvas); 

    Paint col = new Paint(); 
    col.setColor(Color.rgb(0,103,231)); 
    col.setStyle(Paint.Style.FILL); 

    Paint red = new Paint(); 
    red.setAntiAlias(true); // smoothens edge 
    red.setColor(Color.RED); 
    red.setStyle(Paint.Style.FILL); 

    Paint yellow = new Paint(); 
    yellow.setAntiAlias(true); // smoothens edge 
    yellow.setColor(Color.YELLOW); 
    yellow.setStyle(Paint.Style.FILL); 
    Rect myRect = new Rect(); 

    canvas.drawRect(0, 0, getWidth(), getHeight(), col); 
    int columnWidth = canvas.getWidth()/7; 
    int rowHeight = (canvas.getHeight()/17); 
    int circleRadius = (canvas.getHeight()/6) - 163; 

    Game game = new Game(); 
    int player = game.getPlayer(); 


    for(int column = 0; column < Columns; column++) 
    { 
     if(player == 1){ 
      canvas.drawCircle((columnWidth*column)+55, (rowHeight)+10, circleRadius, red); 
     } 
     else if(player == 2){ 
      canvas.drawCircle((columnWidth*column)+55, (rowHeight)+10, circleRadius, yellow); 
     } 
     else if(player == 0){ 
      canvas.drawCircle((columnWidth*column)+55, (rowHeight*1)+10, circleRadius, col); 
     } 
    } 
} 

提前

+0

什麼類的類是Game類?你想讓程序做什麼? –

+0

公共類遊戲擴展活動 而我只想獲取TokenView類的畫布中所觸及的令牌的列,以便我可以重新繪製柵格以將該令牌放入所選列的下一個可用行中。 – Oavatog

回答

0

您應該覆蓋在TokenView類的onTouch方法,而不是非常感謝。
畫布只是繪製視圖事物的地方。以重繪視圖

private Cell bufferCell; 

@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    Cell cell = getCell(event); 
    if (cell != null) 
    { 
    switch (event.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
     if (cell != bufferCell) 
      bufferCell = cell; 

     // DO STUFF HERE. 
     // THE PLAYER HAS JUST TOUCHED A CELL. 
     // invalidate(); 
     break; 
     case MotionEvent.ACTION_MOVE: 
     if (cell != bufferCell) 
     { 
      bufferCell = cell; 
      // Continue Pan 
      // DO STUFF HERE 
      // THE PLAYER'S FINGER IS ON A DIFFERENT CELL NOW, AND MOVING 
      // invalidate(); 
     } 
     break; 
     case MotionEvent.ACTION_UP: 
     // Commit Pan 
     bufferCell = null; 
     // DO STUFF HERE 
     // THE PLAYER HAS TAKEN HIS FINGER OFF THE VIEW 
     // invalidate(); 
     break; 
    } 
    } 
    return true; 
} 

private Cell getCell(MotionEvent event) 
{ 
    int motionX = (int) event.getX(); 
    int motionY = (int) event.getY(); 
    if (motionX > 0 && motionX < sizeOfGrid && motionY > 0 
     && motionY < sizeOfGrid) 
    { 
    int rowIndex = motionY/currentSizeOfCell % rowCount; 
    int columnIndex = motionX/currentSizeOfCell % columnCount; 
    return cellArray[rowIndex][columnIndex]; 
    } 
    return null; 
} 

呼叫invalidate()
你應該寫這樣的事情在你的TokenView類。
不要做Game game = new Game();裏面的TokenView的onDraw()方法。
它會繼續創建新的遊戲,而你畫東西,你不會得到任何預期的行爲。

我使用了一個bufferCell變量,這將有助於減少基於邏輯的方法被調用的時間,因爲當玩家將手指放在網格上時,單元不需要改變。只有當它改變時,如果你按照上面提到的代碼設計,調用適當的方法纔會被觸發。

另請參閱如何在Android中創建自定義視圖。
覆蓋的主要方法是onDraw()onSizeChanged()onMeasure()

希望有所幫助。

+0

可靠的人,我會給它一個去。我會讓你知道:) – Oavatog

+0

所以我修改了你的getCell方法以滿足我的需求。只要我正確地做了這件事,如果我爲了測試的目的在交換機內添加控制檯註釋或Cat Log.d消息,它會顯示正確嗎? 因爲到目前爲止,我只是更改了行數和列數以滿足我的需求,並將currentSizeOfCell轉換爲圓半徑。這是正確的嗎? – Oavatog

+0

你會知道你什麼時候測試運行它。 CurrentSizeOfCell是單元格的寬度和高度(每個單元格在我的邏輯中都是一個正方形),所以你可以將你的currentSizeOfCell設置爲每個圓的半徑的兩倍,在我看來,它應該工作。 –