How Android View is drawn

Source: Internet
Author: User
<span id="Label3"></p><p><p>Here's how Android is measured, but a nice control I only know where you grow, which is of course Not. Simply rewrite the OnDraw method and invoke the colorful brush on the canvas object to draw the "sexy" appearance of the Control. So how is the view drawn?</p></p><p><p>To understand how the view is drawn, you need to know what the canvas is. What the paint (brush) can do.</p></p><p><p>ⅰ, Canvas means a canvas, and you can paint what you miss. When we rewrite the OnDraw method, we can get a canvas object, This is your stage, draw what you think. On the Code.</p></p><p><p> </p></p><pre><pre><span>@Override protected void<span> OnDraw (canvas canvas) { Super<span>. OnDraw (canvas); }</span> </span></span></pre></pre><p><p>When you need to redraw a certain control, we just need to call the Canvas.drawxxx method on this big stage to draw a variety of graphics.</p></p><p><p>ⅱ, paint is the aplomb brush on the Canvas. To draw, you have to adjust the brush so that the brush is adjusted so that the image is drawn to the canvas so that it can be displayed on the phone Screen. Many properties and methods are available on the paint to set the Brush.</p></p><p><p>1. Setantialias: Sets the Brush's sawtooth Effect.<br>2. setcolor: Set Brush color<br>3. Setargb: Sets the a,r,p,g value of the Brush.<br>4. Setalpha: Set alpha value<br>5, settextsize: Set the font size.<br>6, setStyle: Set Brush style, Hollow or Solid.<br>7, Setstrokewidth: set the hollow border Width.<br>8, getColor: Get the color of the brush<br>9. Getalpha: Gets the alpha value of the Brush.</p></p><p><p>Now that we know the brushes and the canvas, let's look at a short example of how the canvas and the brush work together to create a desired shape. The old way, on the code:</p></p><p><p></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>MyView<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">View {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Paint paint; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">MyView (context context, attributeset attrs) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(context, attrs); Init (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Init () {paint</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Paint (paint.anti_alias_flag); Paint.setcolor (color.red); Paint.setstrokewidth (</span></span>5<span style="color: #000000;"><span style="color: #000000;">); Paint.settextsize (</span></span>28<span style="color: #000000;"><span style="color: #000000;">); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onmeasure (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>widthmeasurespec,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">HEIGHTMEASURESPEC) {setmeasureddimension (measurewidth (widthmeasurespec), measurewidth (heightmeasurespec)); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Measurewidth (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">MEASURESPEC) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>result = 0<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Specmode =<span style="color: #000000;"><span style="color: #000000;">Measurespec.getmode (measurespec); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Specsize =<span style="color: #000000;"><span style="color: #000000;">measurespec.getsize (measurespec); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(specmode = =<span style="color: #000000;"><span style="color: #000000;">measurespec.exactly) {result</span></span>=<span style="color: #000000;"><span style="color: #000000;">specsize; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{result</span></span>= 200<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(specmode = =<span style="color: #000000;"><span style="color: #000000;">Measurespec.at_most) {result</span></span>=<span style="color: #000000;"><span style="color: #000000;">math.min (specsize, result); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">result; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnDraw (canvas canvas) {canvas.drawtext (</span></span>"china", 0, 80<span style="color: #000000;"><span style="color: #000000;">, paint); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnDraw (canvas); }}</span></span></pre><p><p>The above code, we have done so a few things:</p></p><p><p>ⅰ, we use the knowledge of the previous article, rewrite the onmeasure method, let this simple custom control adaptive wrap_content Property.</p></p><p><p>ⅱ, we called the OnDraw method, got canvas and paint, and wrote "chinese" text on the VIEW.</p></p><p><p>The end result Is:</p></p><p><p></p></p><p><p>This is how I draw a little insights on Android View, i caishuxueqian, welcome everyone treatise.</p></p><p><p></p></p><p><p>How Android View is drawn</p></p></span>

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.