1 thought on “Why does this code, why paint paint = new paint (); this sentence reports warning?”

  1. This warning information is given by Android Lint, not a standard Java warning.

    View's ONDRAW () method will be frequently called. Android gives this warning. It is not recommended that developers in ONDRAW () The function distribution of the object in the body. If there is an object that needs to be used in it, the distribution of these objects should be placed elsewhere.
    , for example, the problem of the landlord, you can initialize a member variable MPAINT in the constructor. In the onDraw () function, you only call the setcolor () in the body and do not perform NEW operation.

    according to Google's explanation to avoid allocating objects in ONDRAW () to reduce the drawing of garbage recychers to draw drawers The impact of performance.
    I checked it, the DALVIK virtual machine used by Android, the garbage recovery device needs to be the world when working. All threads, waiting for the work of garbage recychers to work, the program threads can work again.
    The most active places for garbage recovery must be on pile memory, that is, the memory occupation generated by the new operation. Try to come out as little as possible, you can minimize the time of the virtual machine Stop the World.
    If it is not understood, it is recommended that the working method of the Java garbage recovery device under Baidu. How frequent is the call? You put your view in one, and then add a line of logs to onDraw (), then roll your screen, and look at the massive log output, and you will experience it.

Leave a Comment