Common Android Canvas Methods

Source: Internet
Author: User

Common Android Canvas Methods
We can understand this Canvas as a memory area provided by the system (but it is actually just a set of drawing APIs, and the real memory is the Bitmap below ), it also provides a complete set of operations on the memory area, all of which are drawing APIs. That is to say, in this way, we can draw exactly what we need or use Graphic to draw what we need. We can control everything we need to draw.


This method can be divided into two types based on the environment: canvas painting with common View, and canvas painting with special SurfaceView. The main difference between the two is that you can define a special thread in SurfaceView to complete the drawing work. The application does not need to wait for the View to be refreshed to improve the performance. The previous one is suitable for animations with a small processing capacity and a small frame rate, such as chess games. The latter is mainly used in games and high-quality animations.


Paint indicates the Paint brush, Paint brush, and Paint on the Canvas;
Common Methods of Painting:
SetARGB (int a, int r, int g, int B) // set the color of the Paint object. Parameter 1 is the alpha transparent value.
SetAlpha (int a) // set the alpha opacity in the range of 0 ~ 255
SetAntiAlias (boolean aa) // indicates whether the image is anti-sawtooth.
SetColor (int color) // set the Color. Here, the color class defined in Android contains some common Color definitions.
SetTextScaleX (float scaleX) // sets the text zoom factor. 1.0f is the original
SetTextSize (float textSize) // set the font size
SetUnderlineText (booleanunderlineText) // set the underline


// 1. The Canvas background of the entire control will be filled with the color ARBG
// MCanvas. drawARGB (122, 10,159,163 );
// 2. the Canvas background of the entire control will be filled with the color ARBG
// MCanvas. drawColor (Color. BLUE );
// 3. Draw the color, but set a mode.
// MCanvas. drawColor (Color. BLUE, Mode. SCREEN );
// 4. Draw the background, which is equivalent to 2
// MCanvas. drawPaint (mPaint );
// 5. Draw a vertex
// MCanvas. drawPoint (23, 23, mPaint );
// 6. Draw a lot of points here. float [] indicates {x0, y0, x1, y1, x2, y2, x3, y3 .....}
// MCanvas. drawPoints (new float [] {10, 11, 10, 12, 10, 13, 10, 14, 10, 15, 10, 16}, mPaint );
// 7. Draw a line
// MCanvas. drawLine (...);
// 8. What is the difference between Rect and RectF?
// The precision is different. The Rect uses the int type as the value, and the RectF uses the float type as the value.
// Rect r = new Rect (10, 10, 50, 50 );
// MCanvas. drawRect (r, mPaint );
// RectF rf = new RectF (10, 10, 50, 50 );
// MCanvas. drawRect (rf, mPaint );
// MCanvas. drawRect (10, 10, 50, 50, mPaint );
// 9. The RectF initialization parameters for an elliptical image are (left, top, right, bottom)
// RectF rf = new RectF (100,100,200,250 );
// MCanvas. drawOval (rf, mPaint );
// 10. Circle (center x0, center y0, radius, paint)
// MCanvas. drawCircle (100,100, 50, mPaint );
// 11. Draw an arc RectF object to indicate the left, top, right, and bottom rectangles)
// RectF rf = new RectF (100,100,200,200 );
// Parameter (rf, startAngle, angle, sweepAngle, paint) sweepAngle indicates whether to display the degrees of arc triangle angle painting
// MCanvas. drawArc (rf, 60, 30, true, mPaint );
// 12. The RectF of the rounded rectangle is left, top, right, and bottom)
// RectF rf = new RectF (100,100,200,200 );
// 50 indicates the radius of x and 20 indicates the radius of y.
// MCanvas. drawRoundRect (rf, 50, 20, mPaint );
// 13. Draw Any Polygon
// Path path = new Path ();
// Path. moveTo (100,100 );
// Path. lineTo (200,200 );
// Path. lineTo (300,200 );
// MCanvas. drawPath (path, mPaint );
// 14. You can draw other images through the Path object.
// Path path = new Path ();
// Path. addCircle (100,100, 20, Path. Direction. CCW );
// MCanvas. drawPath (path, mPaint );

/* DrawBitmap
DrawText
DrawPicture */

/* Rect r = new Rect (100,100,200,200 );
ByteArrayOutputStream out = new ByteArrayOutputStream ();
Bitmap bitmap = BitmapFactory. decodeResource (mContext. getResources (), R. drawable. bg );
Bitmap. compress (Bitmap. CompressFormat. JPEG, 100, out );
InputStream in = new ByteArrayInputStream (out. toByteArray ());
*/
/* Picture picture = Picture. createFromStream (mContext. getResources (). openRawResource (R. raw. bg ));
MCanvas. drawPicture (picture );*/

// 15. Draw a bitmap object
// MCanvas. drawBitmap (BitmapFactory. decodeResource (mContext. getResources (), R. drawable. bg), 100,100, mPaint );

// 16. The Matrix contains Bitmap Processing Operations.
/* Matrix m = new Matrix ();
M. postScale (2, 2 );
M. postRotate (60 );
MCanvas. drawBitmap (BitmapFactory. decodeResource (mContext. getResources (), R. drawable. bg), m, mPaint );*/

// 17. Draw a bitmap with the Matrix parameter. The bitmap can be processed by the Matrix object, such as rotation, scaling, and movement. Separate summary on the use of Matrix
/* Bitmap bitmap = BitmapFactory. decodeResource (mContext. getResources (), R. drawable. bg );
Matrix m = new Matrix ();
M. postScale (2, 2 );
M. postRotate (60 );
M. postTranslate (300,300 );
MCanvas. drawBitmap (bitmap, m, mPaint );*/
// MCanvas. drawBitmap (...); not summarized at the moment
// 18. Draw text
// MCanvas. drawText (123, 10, 10, mPaint );

// MCanvas. drawText (...); not summarized at the moment

 

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.