Android ApiDemos example resolution (58): Graphics-& gt; Clipping

Source: Internet
Author: User

This example describes the cropping function of Canvas.

Canvas provides methods such as ClipPath, ClipRect, and ClipRegion for cropping. Android supports almost any Cropping Area in the current situation through different combinations of Path, Rect, and Region.

Android. the graphics package defines the Point, Rect, Path, and Region geometric shapes. The Path can be an arc, elliptic, quadratic curve, cubic curve, line segment, A rectangle or other basic ry or a combination of these basic ry shapes. A Path can be an open or closed curve. Rect provides a concise method for defining rectangles. Region supports multiple Region combinations by logical operations such as "add", "Subtract", "merge", and "XOR" of the Region. Region. Op defines the types of inter-regional operations supported by Region.

Clipping introduces the effect of the last display of the same image after the Region is defined for Canvs by using the Region operation. Canvas. save (); and canvas. restore (); are used to save and restore Canvas state attributes.

DrawScene defines the drawing content: a red line, a green circle, and "Clipping" Text

[Java]
Private void drawScene (Canvas canvas ){
Canvas. clipRect (0, 0,100,100 );

Canvas. drawColor (Color. WHITE );

MPaint. setColor (Color. RED );
Canvas. drawLine (0, 0,100,100, mPaint );

MPaint. setColor (Color. GREEN );
Canvas. drawCircle (30, 70, 30, mPaint );

MPaint. setColor (Color. BLUE );
Canvas. drawText ("Clipping", 100, 30, mPaint );
}

Private void drawScene (Canvas canvas ){
Canvas. clipRect (0, 0,100,100 );
 
Canvas. drawColor (Color. WHITE );
 
MPaint. setColor (Color. RED );
Canvas. drawLine (0, 0,100,100, mPaint );
 
MPaint. setColor (Color. GREEN );
Canvas. drawCircle (30, 70, 30, mPaint );
 
MPaint. setColor (Color. BLUE );
Canvas. drawText ("Clipping", 100, 30, mPaint );
} The cropping area is two rectangles:

[Java]
Canvas. save ();
Canvas. translate (160, 10 );
Canvas. clipRect (10, 10, 90, 90 );
Canvas. clipRect (30, 30, 70, 70, Region. Op. DIFFERENCE );
DrawScene (canvas );
Canvas. restore ();

Canvas. save ();
Canvas. translate (160, 10 );
Canvas. clipRect (10, 10, 90, 90 );
Canvas. clipRect (30, 30, 70, 70, Region. Op. DIFFERENCE );
DrawScene (canvas );
Canvas. restore (); the cropping area is a circle.

[Java]
Canvas. save ();
Canvas. translate (10,160 );
MPath. reset ();
Canvas. clipPath (mPath); // makes the clip empty
MPath. addCircle (50, 50, 50, Path. Direction. CCW );
Canvas. clipPath (mPath, Region. Op. REPLACE );
DrawScene (canvas );
Canvas. restore ();

Canvas. save ();
Canvas. translate (10,160 );
MPath. reset ();
Canvas. clipPath (mPath); // makes the clip empty
MPath. addCircle (50, 50, 50, Path. Direction. CCW );
Canvas. clipPath (mPath, Region. Op. REPLACE );
DrawScene (canvas );
Canvas. restore (); the cropping area is the union of two rectangles:

[Java]
Canvas. save ();
Canvas. translate (160,160 );
Canvas. clipRect (0, 0, 60, 60 );
Canvas. clipRect (40, 40,100,100, Region. Op. UNION );
DrawScene (canvas );
Canvas. restore ();

Canvas. save ();
Canvas. translate (160,160 );
Canvas. clipRect (0, 0, 60, 60 );
Canvas. clipRect (40, 40,100,100, Region. Op. UNION );
DrawScene (canvas );
Canvas. restore (); the cropping area is the exclusive or set of two rectangles:

[Java]
Canvas. save ();
Canvas. translate (10,310 );
Canvas. clipRect (0, 0, 60, 60 );
Canvas. clipRect (40, 40,100,100, Region. Op. XOR );
DrawScene (canvas );
Canvas. restore ();

Canvas. save ();
Canvas. translate (10,310 );
Canvas. clipRect (0, 0, 60, 60 );
Canvas. clipRect (40, 40,100,100, Region. Op. XOR );
DrawScene (canvas );
Canvas. restore (); the last is the inverse difference set of the two rectangles:

[Java]
Canvas. save ();
Canvas. translate (160,310 );
Canvas. clipRect (0, 0, 60, 60 );
Canvas. clipRect (40, 40,100,100,
Region. Op. REVERSE_DIFFERENCE );
DrawScene (canvas );
Canvas. restore ();

Canvas. save ();
Canvas. translate (160,310 );
Canvas. clipRect (0, 0, 60, 60 );
Canvas. clipRect (40, 40,100,100,
Region. Op. REVERSE_DIFFERENCE );
DrawScene (canvas );
Canvas. restore ();


Author: mapdigit

 

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.