Android View Canvas onDraw not performed

丶灬走出姿态 提交于 2020-01-24 08:55:06

问题


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.


回答1:


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:

  • http://developer.android.com/reference/android/view/View.html#postInvalidate()
  • http://developer.android.com/reference/android/view/View.html#invalidate()

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.




回答2:


Have you heard of a method call setWillNotDraw(boolean)?

Check it out here

Set it to false and you'll see




回答3:


Just to save this for the future and possible google searchers ;)

I made a new Class which provides datas for my View. This class extends Thread and runns in the background. It checks which tiles my view has to display.

Long story short: Each time a new tile has to be displayed my provider class collects some update requests. If 3 or more update requests are "collected" or it is more than 1 second since the first update request the view will get view.postInvalidate().

The problem was that a view ignores some invalidate requests if you fire several of them at once. If you collect those invalidate requests and fire several of them with just one invalidate() you can see the newest information with just a small lag of time - but you can see it ;)




回答4:


I've solved the problem requesting view.invalidate() or view.postInvalidate() not for invalidating every child view individually (each of these child views incorporated its own AsyncTask), but for invalidating their common parent ViewGroup.

Now in details. I had

public class mViewGroup extends RelativeLayout {...}

and several Views in it, each View as follows:

public class mView extends View {

private mViewGroup mContext = null;

//with a little bit modified standard constructors:
public mView (Context context, mViewGroup ParentViewGroup) {
        super(context);
        mContext = ParentViewGroup;
}

//and also with an AsyncTask:
private class mViewAsyncTask extends AsyncTask<Integer, Void, Void> {
public mAsyncTask() {
   @Override
   protected Void doInBackground(Integer... a) {
       //which does its work and sometimes publishes progress:
       publishProgress();
       return null;
   }

   protected void onProgressUpdate(Void... progress) {
       invalidate();
   }

}

The calls to invalidate(); sometimes worked and sometimes didn't (may be in cases if calls were made from different AsyncTasks belonging to different mViews but in very close moments of time).

Changing

invalidate(); //i.e. invalidating individual View

to

mContext.invalidate(); //i.e. invalidating the whole ViewGroup

solved the problem.




回答5:


In my case, target custom View has height = 0 attribute at that time.(My mistake) and postInvalidate() and invalidate didn't fired onDraw() and Log too.

When I changed height currectly, all problem solved.



来源:https://stackoverflow.com/questions/12253952/android-view-canvas-ondraw-not-performed

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