applibgroup / HarmonyOS-Knowledgebase

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

How to override the onDraw() function in Component for Harmony OS? #60

Closed shreyshrivastava4799 closed 3 years ago

shreyshrivastava4799 commented 3 years ago

Describe the query

I can't find the onDraw() method in Component. Can someone explain to me how to override the onDraw method for Component in Harmony OS?

Create the query with harmonyos tag in stackoverflow and share the link here:

https://stackoverflow.com/questions/68599362/how-to-override-the-ondraw-function-in-component-for-harmony-os

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

GowthamRayar commented 3 years ago

1) First you have to implement Component.DrawTask to your custom Component, Then you will be able to override onDraw() and perform custom Drawing operations. For Example,

 public class CustomComponent extends Component implements Component.DrawTask {

  public CustomComponent(Context context) {
        super(context);
        // DrawTask
        addDrawTask(this::onDraw);
    }

     @Override
    public void onDraw(Component component, Canvas canvas) {
        ...
    }

 }

Also Please refer : https://stackoverflow.com/questions/68599362/how-to-override-the-ondraw-function-in-component-for-harmony-os