【问题标题】:How to move a onDraw shape by click direction button如何通过单击方向按钮移动 onDraw 形状
【发布时间】:2014-09-21 03:05:06
【问题描述】:

我正在 Android 上做一个模拟器来模拟我们机器人的移动(在本例中用圆圈表示)。单击前进/右/左按钮时,圆圈应相应地向前、右转或左转。 但是当我运行这个程序并点击按钮时,圆圈没有移动......

在主要活动中,我有这样的 onClickListener:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.forward_button).setOnClickListener(mGlobal_OnClickListener);
    findViewById(R.id.right_button).setOnClickListener(mGlobal_OnClickListener);
    findViewById(R.id.left_button).setOnClickListener(mGlobal_OnClickListener);


}


 View.OnClickListener mGlobal_OnClickListener = new View.OnClickListener() {
    public void onClick(View v) {
        GameView gameView = new GameView(Menu.this);
        switch(v.getId()) {
            case R.id.forward_button:
                gameView.controlRobot(GameView.FORWARD);
                break;
            case R.id.right_button:
                gameView.controlRobot(GameView.RIGHT);
                break;
            case R.id.left_button:
                gameView.controlRobot(GameView.LEFT);


        }
    }
};

在扩展 View 类的 GamaView 类中,我画了一个这样的圆圈:

canvas.drawCircle((currentX * cellHeight)+(cellHeight/2),   //x of center
            (currentY * cellHeight)+(cellHeight/2),  //y of center
            (cellHeight*3*0.45f),                           //radius
            robot);

另外,在 GameView 类中,我有这个 controlRobot 方法来移动这个圆圈: (移动/旋转方法是正确的,我已经测试过了)

public boolean controlRobot(int keyCode) {
    boolean moved = false;
    boolean rotated = false;

    //move(0):move up on the map
    //move(3):move right on the map
    //move(6):move down on the map
    //move(9):move left on the map

        //forward
    switch (keyCode) {
        case FORWARD:
            if(rotate_count%12==0)
                moved = maze.move(0);

            if(rotate_count%12==3)
                moved = maze.move(3);
            if(rotate_count%12==6)
                moved = maze.move(6);
            if(rotate_count%12==9)
                moved = maze.move(9);
            break;
        case RIGHT:
            rotated = maze.rotate(Maze.RIGHT,rotate_count);
            if(rotated)
            rotate_count+=3;
            break;
        case LEFT:
            rotated = maze.rotate(Maze.LEFT,rotate_count);
            if(rotated)
            rotate_count-=3;
            break;
    }


    if(moved||rotated) {
        //the ball was moved so we'll redraw the view
        invalidate();
    }
    return true;
}

【问题讨论】:

  • 您是否将 GameView 放入布局文件中,因为您在 onClick 方法中使用的 GameView 是全新的 GameView
  • @JRowan 我将 GameView 放在布局文件中。并且 GameView 可以显示在 UI 上。但是 controlRobot 方法不起作用。
  • 我提出了一个答案,当您 setContentView() 时,您正在使用布局中的 GameView,在您的 onClick 方法中,您没有从布局中引用 GameView

标签: java android onclick onclicklistener


【解决方案1】:

当您像这样在 onclick 中引用 GameView 时

GameView gameView = new GameView(Menu.this);

那不是你布局文件中的 GameView

R.layout.main

尝试在你的 onClick 中引用 GameView,就像你做按钮一样

findViewById(R.id.forward_button)

布局文件中 GameView 的 R.id 除外

【讨论】:

    猜你喜欢
    • 2020-04-29
    • 2017-12-12
    • 2023-03-16
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多