Android canvas cutting cliprect

Source: Internet
Author: User

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 );
}
}

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.