View programming (2): invalidate ()

Source: Internet
Author: User

Blog Android (4): The invalidate () method is mentioned in the method of updating the UI. However, due to lack of experience, there is not much to explain. It is just a general discussion! This article continues to explore this strange method.

Before reading this blog, we recommend that you first look at view programming (1): window, view and setcontentview ()

Based on Android (4): the example in the UI update method is slightly modified:

Package mark. zhang; import android. app. activity; import android. content. context; import android. graphics. canvas; import android. graphics. paint; import android. graphics. rectf; import android. OS. bundle; import android. util. log; import android. view. view; public class viewdrawtestactivity extends activity {// used to test static int times = 1; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (saved Instancestate); setcontentview (New myview (this);}/*** internal class, inheriting view ** @ author Mark */class myview extends view {myview (context) {super (context);} paint vpaint = new paint (); // paint style object int I = 0; // arc angle @ overrideprotected void ondraw (canvas) {super. ondraw (canvas); log. D ("mark", "This run ondraw ()" + (times ++) + "times! "); // Set the drawing style vpaint. setcolor (0xff00ffff); // paint color vpaint. setantialias (true); // vpaint with anti-aliasing. setstyle (paint. style. stroke); // draw an arc canvas. drawarc (New rectf (60,120,260,320), 0, I, true, vpaint); // arc angle if (I + = 10)> 360) I = 0; // re-paint. Execute the ondraw program again // invalidate ();}}}

Running effect:

It can be seen that the image draws only one line, indicating that the ondraw () method is called once. We can also see from the log:

D/mark    (  221): this run onDraw() 1 times!

So what is the force to call the ondraw () method?

The setcontentview () view method calls the setcontentview (view) method of phonewindow. The call relationship is as follows:

It can be seen that the invalidate () method is the force that promotes the ondraw () method to be called.
Modify the code, uncomment invalidate () in the ondraw () method of the internal class myview, and then check the running effect:

Console:

D/mark    (  248): this run onDraw() 5629 times!D/mark    (  248): this run onDraw() 5630 times!D/mark    (  248): this run onDraw() 5631 times!D/mark    (  248): this run onDraw() 5632 times!D/mark    (  248): this run onDraw() 5633 times!D/mark    (  248): this run onDraw() 5634 times!D/mark    (  248): this run onDraw() 5635 times!D/mark    (  248): this run onDraw() 5636 times!D/mark    (  248): this run onDraw() 5637 times!D/mark    (  248): this run onDraw() 5638 times!D/mark    (  248): this run onDraw() 5639 times!D/mark    (  248): this run onDraw() 5640 times!D/mark    (  248): this run onDraw() 5641 times!D/mark    (  248): this run onDraw() 5642 times!D/mark    (  248): this run onDraw() 5643 times!D/mark    (  248): this run onDraw() 5644 times!D/mark    (  248): this run onDraw() 5645 times!D/mark    (  248): this run onDraw() 5646 times!

It can be seen that the invalidate () method keeps ondraw () called to achieve the re-painting effect.

In the invalidate () method source code, there is such a comment:

 /**     * Invalidate the whole view. If the view is visible, {@link #onDraw} will     * be called at some point in the future. This must be called from a     * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.     */

This section describes the above implementation (calling the ondraw () method ). However, the postinvalidate () method must be used in the child thread.

If you are interested, see view programming (3): invalidate () source code analysis.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.