The process of cutting the canvas for Android is not very difficult, but it is difficult to understand. Here I will write my understanding but it is not necessarily correct:
Canvas. cliprect (30, 30, 70, 70, region. op. XOR); the last parameter has multiple options: // difference is the first time different from the second display.
// Replace is displayed for the second time
// Reverse_difference is the second display that is different from the first display.
// Intersect intersection display
// Union display all
// The XOR complementary set is displayed in the complete set minus the intersection birth part.
Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. Graphics. path;
Import Android. Graphics. region;
Import Android. util. attributeset;
Import Android. View. view;
Public class sbook extends view {
Context mcontext;
Paint mpaint;
Path mpath;
Public sbook (context ){
Super (context );
Init ();
}
Public sbook (context, attributeset attrs ){
Super (context, attrs );
Init ();
}
Public sbook (context, attributeset attrs, int defstyle ){
Super (context, attrs, defstyle );
Init ();
}
Private void Init (){
Mpaint = new paint ();
Mpaint. setantialias (true );
Mpaint. setstrokewidth (6 );
Mpaint. settextsize (16 );
Mpaint. settextalign (paint. Align. Right );
Mpath = New Path ();
}
Protected void ondraw (canvas ){
Canvas. drawcolor (color. Gray );
Canvas. Save ();
Canvas. Translate (10, 10 );
Drawscene (canvas );
Canvas. Restore ();
Canvas. Save ();
Canvas. Translate (160, 10 );
Canvas. cliprect (10, 10, 90, 90 );
Canvas. cliprect (30, 30, 70, 70, Region. Op. XOR );
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 );
Mpath. cubicto (0, 0,100, 0,100,100 );
Mpath. cubicto (100,100, 0,100, 0, 0 );
Canvas. clippath (mpath, Region. Op. Replace );
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 ();
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 (160,310 );
Canvas. cliprect (0, 0, 60, 60 );
Canvas. cliprect (40, 40,100,100, Region. Op. reverse_difference );
Drawscene (canvas );
Canvas. Restore ();
}
Private void drawscene (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 );
}
}