是的,我所拥有的是“Game”,“GridView”和“TokenView”这三个类(虽然我只需要帮助的第一个也是最后一个类:D)我想在这里做的是使用onTouch方法获取在我的'TokenView'类中触摸的列(getY)。
游戏:
@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 :(得分:0)
您应该改写TokenView类中的onTouch方法 画布就是绘制视图内容的地方 您应该在TokenView类中编写类似的内容:
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();
方法中不要 onDraw()
。
它会在您绘制内容时继续创建新游戏,并且您不会得到任何预期的行为。
我使用了bufferCell
变量,这将有助于减少基于逻辑的方法被调用的时间,因为当玩家将手指移动到网格上时,单元格无需更改。只有当它改变时,如果你按照上面提到的代码设计,将触发对适当方法的调用。
另请阅读有关如何在Android中创建自定义视图的信息
要覆盖的主要方法是onDraw()
,onSizeChanged()
和onMeasure()
。
希望有所帮助。