invalidate() doesn't recall OnDraw method

时光毁灭记忆、已成空白 提交于 2019-12-08 11:33:32

问题


invalidate() doesn't recall my OnDraw() method. I use invalidate() in a setter method in the same class that OnDraw() is in.

public class PuzzleDraw extends View {

    // defining some variables //

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // codes for painting puzzle //
    }

    public PuzzleDraw(Context context) {
        super(context);
    }

    // Method for giving cursor X/Y from MainActivity OnTouch() method and using it in OnDraw() method in some part of painting code //

    public void setCursor(int i, int j) {
        this.xx = i;
        this.yy = j;
        invalidate();
    } 

When I debug my code, everything is good except RECALLING OnDraw() when invalidate() executes... in fact, invalidate() is doing nothing for me! What is wrong? I used postInvalidate(), this.invalidate(), this.postInvalidate() too, but nothing changed.


回答1:


Calling to invalidate() wouldn't execute onDraw directly, it only sets the appropriate flag in the View class. Your onDraw method will be called by framework the next time when view hierarchy is traversed.




回答2:


You might be calling setCursor(float, float) method only a single time by calling it from activity but invalidate method have to be called again and again if you wish to call onDraw method internally by the framework itself.

Hope this will work



来源:https://stackoverflow.com/questions/11720199/invalidate-doesnt-recall-ondraw-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!