Android ApiDemos example resolution (67): Graphics-& gt; Layers

Source: Internet
Author: User

In general, Canvas can be regarded as a Canvas. All drawing operations, such as drawBitmap and drawCircle, occur on this Canvas. This Canvas also defines attributes such as Matrix and color.

However, if you need to implement relatively complex drawing operations, such as multi-layer animation and map (a map can be formed by multiple map layers, such as the political area layer, road layer, and interest point Layer ).

Canvas provides Layer Support. By default, only one Layer is supported. If you need to draw by hierarchy, the Android Canvas can use SaveLayerXXX and Restore to create some middle layers, which are managed according to the "stack structure:

 

Create a new Layer to "stack". You can use saveLayer and savaLayerAlpha to launch a Layer from "stack". You can use restore and restoreToCount. However, when a Layer is added to the stack, subsequent DrawXXX operations occur on this Layer. When a Layer is removed from the stack, the image drawn at this Layer is "drawn" to the upper Layer or Canvas, when copying a Layer to a Canvas, you can specify the transparency of the Layer. This is specified when you create a Layer:

Public int saveLayerAlpha (RectF bounds, int alpha, int saveFlags)

In this example, Layers introduces the basic usage of Layers: Canvas can be viewed as composed of two Layers. to better illustrate the problem, We Can slightly modify the code, the default layer draws a red circle and a blue circle on the new layer. The transparency of the new layer is 0 × 88.

[Java]
@ Override protected void onDraw (Canvas canvas ){
Canvas. drawColor (Color. WHITE );

Canvas. translate (10, 10 );

MPaint. setColor (Color. RED );
Canvas. drawCircle (75, 75, 75, mPaint );

Canvas. saveLayerAlpha (0, 0,200,200, 0x88, LAYER_FLAGS );


MPaint. setColor (Color. BLUE );
Canvas. drawCircle (125,125, 75, mPaint );

Canvas. restore ();
}

@ Override protected void onDraw (Canvas canvas ){
Canvas. drawColor (Color. WHITE );
 
Canvas. translate (10, 10 );
 
MPaint. setColor (Color. RED );
Canvas. drawCircle (75, 75, 75, mPaint );
 
Canvas. saveLayerAlpha (0, 0,200,200, 0x88, LAYER_FLAGS );
 
 
MPaint. setColor (Color. BLUE );
Canvas. drawCircle (125,125, 75, mPaint );
 
Canvas. restore ();
} When you call canvas. saveLayerAlpha creates a new layer, followed by canvas. canvas. restore () copies the image drawn from this new layer to the default Canvas layer with a transparency of 0x88.

 
Author: mapdigit
 


 

Related Article

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.