2014-02-21 61 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设置为每个圆的半径的两倍,在我看来,它应该工作。 –