Search code examples
javaandroidviewandroid-canvasinvalidation

Android View Canvas onDraw not performed


I am currently working on a custom View which draws some tiles on the canvas. Those tiles are loaded from several files and will be loaded when needed.

They will be loaded by an AsyncTask. If they are already loaded they will just be painted on the canvas. This is working properly!

If those pictures are loaded the AsyncTask is firing view.postInvalidate() The problem is that my custom View is not firing onDraw(Canvas canvas) everytime I fire view.postInvalidate().

The view.postInvalidate only fires onDraw() method the first time when a picture is loaded and then only when I fire this.invalidate() in an onTouchEvent inside my CustomView

Is it possible that a View decides wether it will draw the canvas again or not? Is there a way to FORCE the View to redraw? I think the invalidate method tells the View that it would be cool if the View would think about redrawing -.-

Is it possible that those invalidate methods have a limit?

I hope anyone of you knows more about this problem.

edit:

I just changed every postInvalidate() to invalidate() because the images are all loaded by an AsyncTask executed from the main Thread But there is still the problem that an executed invalidate() is not executing the onDraw() method. I found out that the view.invalidate() is fired by overriding the original method:

@Override
public void invalidate() {
    super.invalidate();
    Log.d(TAG, "invalidate executed");
}

I don't know what to do now. I'm firing view.invalidate() and view.postInvalidate() but nothing works in absolutely no combination.


Solution

  • There's a bit of a misunderstanding here. The invalidate and postInvalidate method are used to tell the View that it needs to be refreshed and redrawn in the earliest drawing cycle. The difference is that the invalidate method should be called from within the UI Thread and the postInvalidate should be called from outside of the UI Thread.

    These methods are briefly described here:

    The AsyncTask on the other hand is a class devised especially for the problem you're facing. When you need to perform a big task in the background, asynchronously you need the AsyncTask for that, but! the AsyncTask's callback method is run in the UIThread!

    Take a look at the explanation of AsyncTask methods here:

    When an asynchronous task is executed, the task goes through 4 steps:

    1. onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

    2. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

    3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

    4. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

    This means that in the onPostExecute method you should try using the invalidate method instead of the postInvalidate method as it is called from the UIThread.