applibgroup / HarmonyOS-Knowledgebase

This repository contains code samples of HarmonyOS Training
Apache License 2.0
17 stars 6 forks source link

invalidate() in onDraw is not calling onDraw #27

Closed yashrajgupta closed 3 years ago

yashrajgupta commented 3 years ago

I am using invalidate() in my onDraw to get a loading view but it is not starting the onDraw call.

public void onDraw(Component component, Canvas canvas) {
<function body>
invalidate()
}

Link to stackoverflow

https://stackoverflow.com/questions/68586761/invalidate-in-ondraw-is-not-calling-ondraw

Additional information

Developer Platform: Windows DevEco Studio version: 2.1.0.303 SDK API version: 5 SDK version: 2.1.1.21 Device: Not Required Device OS version: Not Required

samuel-abhishek commented 3 years ago

Make sure you are calling the addDrawTask method in your implementation Even after calling the addDrawTask method, if invalidate() is not called on UI thread, then the onDraw() might not be invoked. Therefore, we can be sure of calling it on UI thread by implementing the following statement :

 context.getUITaskDispatcher().asyncDispatch(this::invalidate);

This personally worked for me when i was trying to call the onDraw() method continuously for displaying an animation.