简体   繁体   English

使用onTouchEvent更改字段后未调用onDraw()

[英]onDraw() not called after using onTouchEvent to change fields

I am creating an Android connect 4 game, I am at the stage where I am trying to place the counter depending on the circle that is selected by the user, but right now the code is not working, the counter basically not moving no matter where I click 我正在创建一个Android connect 4游戏,我处于根据用户选择的圆圈放置计数器的阶段,但是现在代码不起作用,无论在哪里,计数器基本上都不会移动我点击

// onDraw() defined above
@Override 
public boolean onTouchEvent(MotionEvent event) {
    rows = (int)event.getX();
    cols = (int)event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
    }
    return false;
}

After you change your rows/cols field values, call invalidate() . 更改rows/cols字段值之后,调用invalidate() This will ask Android to call your onDraw() method at some point in the future. 这将要求Android在将来的某个时刻调用您的onDraw()方法。

Your code should look like this: 您的代码应如下所示:

@Override 
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: // Fall through
        case MotionEvent.ACTION_MOVE: // Fall through
        case MotionEvent.ACTION_UP: // Fall through
            rows = (int)event.getX();
            cols = (int)event.getY();
            invalidate();
            return true;
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM