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