Canvas for Android Game Development

Source: Internet
Author: User

Get the canvas object
[Java]
Canvas canvas = getHolder (). lockCanvas ();

Canvas attributes, methods, and applications
[Java]
If (canvas! = Null ){
// ---- Set the canvas drawing to be non-sawtooth
Canvas. setDrawFilter (pfd );
// ---- Use the canvas to fill the screen
Canvas. drawColor (Color. BLACK );
// ---- Draw text
Canvas. drawText ("drawText", 10, 10, paint );
// ---- Draw the pixel
Canvas. drawPoint (10, 20, paint );
// ---- Draw multiple pixels
Canvas. drawPoints (new float [] {10, 30, 30, 30}, paint );
// ---- Draw a straight line
Canvas. drawLine (10, 40, 50, 40, paint );
// ---- Draw multiple straight lines
Canvas. drawLines (new float [] {10, 50, 50, 50, 70, 50,110, 50}, paint );
// ---- Draw a rectangle
Canvas. drawRect (10, 60, 40,100, paint );
// ---- Draw a rectangle 2
Rect rect = new Rect (10,110, 60,130 );
Canvas. drawRect (rect, paint );
Canvas. drawRect (rect, paint );
// ---- Draw a rounded rectangle
RectF rectF = new RectF (10,140, 60,170 );
Canvas. drawRoundRect (rectF, 20, 20, paint );
// ---- Draw a circle
Canvas. drawCircle (20,200, 20, paint );
// ---- Draw an arc
Canvas. drawArc (new RectF (150, 20,200, 70), 0,230, true, paint );
// ---- Draw an ellipse
Canvas. drawOval (new RectF (150, 80,180,100), paint );
// ---- Draw the specified path Image
Path path = new Path ();
// Set the path start point
Path. moveTo (160,150 );
// Route 1
Path. lineTo (200,150 );
// Route 2
Path. lineTo (180,200 );
// The path ends.
Path. close ();
Canvas. drawPath (path, paint );
// ---- Draw the specified path Image
Path pathCircle = new Path ();
// Add a circular path
PathCircle. addCircle (130,260, 20, Path. Direction. CCW );
// ---- Draw the path text with a circle
Canvas. drawTextOnPath ("PathText", pathCircle, 10, 20, paint );
}
 
NOTE: For the above Code, refer to "programming for Android games from scratch".

Get the bitmap with the width of the custom image (parameter 1: context object, parameter 2: Resource ID, parameter 3: Custom width, parameter 4: Custom height)
[Java]
Public static Bitmap loadBallView (Context context, int resId, int width, int height ){

Resources resources = context. getResources ();

Drawable image = resources. getDrawable (resId );

Bitmap bitmap = Bitmap. createBitmap (width, height, Bitmap. Config. ARGB_8888 );

Canvas canvas = new Canvas (bitmap );
Image. setBounds (0, 0, width, height );
Image. draw (canvas );

Return bitmap;
}
 
Use canvas to draw bitmap
[Java]
Bitmap bitmap = loadBallView (context, R. drawable. image, 100,100 );
 
Paint paint = new Paint ();
 
Canvas. drawBitmap (bitmap, startX, startY, paint); // parameter 1: bitmap resource, parameter 2: Start X coordinate, parameter 3: Start Y coordinate, parameter 4: paint object

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.