Android Paint Canvar

Source: Internet
Author: User

Android Paint Canvar

Recently I have been studying custom controls and encountered geometric drawing. Here I will post a common example:

① First code in the main Activity:

Package com. example. mycustomwidget;


Import android. app. Activity;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. RectF;
Import android. OS. Bundle;
Import android. util. AttributeSet;
Import android. view. View;


Public class MainActivity extends Activity {


@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
MyCustomView myView = new MyCustomView (MainActivity. this );
SetContentView (myView );
}
}

 

 

② Code in MyCustomView:

Package com. example. mycustomwidget;


Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Rect;
Import android. graphics. RectF;
Import android. util. AttributeSet;
Import android. view. View;


Public class MyCustomView extends View {


Public MyCustomView (Context context, AttributeSet attrs ){
Super (context, attrs );
// TODO Auto-generated constructor stub
}




Public MyCustomView (Context context, AttributeSet attrs, int defStyleAttr ){
Super (context, attrs, defStyleAttr );
// TODO Auto-generated constructor stub
}






Public MyCustomView (Context context ){
Super (context );
// TODO Auto-generated constructor stub
}






@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );
// Set the canvas color
Canvas. drawColor (Color. BLUE );

// ------------------------------- ① Draw a hollow square --------------------------------
// Create a paint brush
Paint mPaint = new Paint ();
// Set the paint brush to hollow
MPaint. setStyle (Paint. Style. STROKE );
// Set the painting color
MPaint. setColor (Color. RED );
// Set the paint width
MPaint. setStrokeWidth (10 );
/**
* Create a rectangle
* First parameter: X axis coordinate of the Left vertex of the rectangle
* Second parameter: Y axis of the Left vertex of the rectangle
* Third parameter: X-axis coordinate of the vertex in the lower right corner of the rectangle
* Fourth parameter: Y axis of the vertex in the lower right corner of the rectangle
*/
Rect mRect = new Rect (10, 10, 50, 50 );
// Start to draw a rectangle
Canvas. drawRect (mRect, mPaint );

// ------------------------------- ② Draw a solid square --------------------------------
// Set the paint brush to solid.
MPaint. setStyle (Paint. Style. FILL );
// Reset the rectangle size
MRect. set (60, 60,100,100 );
Canvas. drawRect (mRect, mPaint );

// ------------------------------- ③ Draw a hollow rectangle --------------------------------
// Set the width of the paint brush
MPaint. setStrokeWidth (2 );
MPaint. setStyle (Paint. Style. STROKE );
MRect. set (70,110,140,140 );
Canvas. drawRect (mRect, mPaint );

// ------------------------------- ④ Draw a hollow circle --------------------------------
// Eliminate the Sawtooth
MPaint. setAntiAlias (true );
/**
* Draw a hollow circle
* First parameter: X-axis coordinate of the center
* Second parameter: Y-axis coordinate of the center
* Third parameter: circle radius
* Fourth parameter: paint brush
*/
Canvas. drawCircle (180,180, 20, mPaint );

// ------------------------------- ⑤ Draw a solid circle --------------------------------
MPaint. setStyle (Paint. Style. FILL );
Canvas. drawCircle (230,220, 20, mPaint );

// ------------------------------- Draw a solid slice section --------------------------------
// Draw a rectangle first
RectF oval = new RectF (240,240,300,300 );
/**
* Draw a solid slice
* First parameter: a rectangle that defines the size of the slice.
* Second parameter: Start angle of the slice
* Third parameter: angle to be drawn
* Indicates whether the fourth parameter contains a circle (true is included, which is generally used to draw a slice; false is not included, and generally used to draw an arc)
* Fourth parameter: paint brush
*/
Canvas. drawArc (oval, 0, 40, true, mPaint );

// ------------------------------- 7 draw a hollow slice --------------------------------
MPaint. setStyle (Paint. Style. STROKE );
Oval. set (290,290,350,350 );
Canvas. drawArc (oval, 0, 60, true, mPaint );

// ------------------------------- Draw an arc section --------------------------------
Oval. set (360,360,400,400 );
Canvas. drawArc (oval, 0, 70, false, mPaint );

// ------------------------------- Draw a circle --------------------------------
Oval. set (360,360,400,400 );
Canvas. drawRect (oval, mPaint );
Canvas. drawArc (oval, 0,360, false, 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.