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.
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:
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.