Drawing geometry: Using the Android.graphics class

Source: Internet
Author: User
Tags object drawtext set background string
Example description


"How do I draw 2D graphics on and off the machine?" "This is a problem that many Android game developers are always talking about, and in the Android SDK, there is no Java graphics2d function to use, but instead use the classes below android.graphics to draw 2D vector graphs." This package provides a number of classes and methods for drawing graphics on the phone, where canvas, while the Paint (Android.graphics.Paint) class is like a colored pencil, giving different co-ordination to draw a vector graph of different colors and different kinds of effects.



This example draws a variety of geometric paint on a cavas (canvas) that is hollow, practiced, and discolored using the different setting values of the object (pen).



Run results if






Sample Program



The MyView class that inherits from Android.view.View in the main program, overrides the MyView OnDraw () method, runs the drawing work at the beginning, and draws the geometry to OnDraw in paint () to Paint.setcolor () changes the graphic color and Paint.setstyle () to control whether the drawing is hollow or solid. The last section of the program, which is written directly in canvas, will have different appearance patterns as the attributes in the Paint object are set.


Package com.graphices;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.RectF;
Import Android.graphics.Path;
Import Android.graphics.Shader;
Import android.graphics.LinearGradient;
public class Graphicesactivity extends activity {
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
/* Set Contentview as a custom myview*/
MyView myview=new MyView (this);
Setcontentview (MyView);
}
/* Customize inheritance View's myview*/
Private class MyView extends View
{
Public MyView {
Super (context);
}
/* Rewrite OnDraw () * * *
@Override
protected void OnDraw (Canvas Canvas)
{
Super.ondraw (canvas);
/* Set Background to white */
Canvas.drawcolor (Color.White);
Paint paint=new Paint ();
/* Go sawtooth * *
Paint.setantialias (TRUE);
/* Set the color of paint * *
Paint.setcolor (color.red);
/* Set paint style to stroke: hollow */
Paint.setstyle (Paint.Style.STROKE);
/* Set the paint width of the box * *
Paint.setstrokewidth (3);
/* Draw a hollow circle * *
Canvas.drawcircle (paint);
/* Draw a hollow square * *
Canvas.drawrect (Ten, Paint,);
/* Draw a hollow rectangle * *
Canvas.drawrect (Ten, 170, 70,200, paint);
/* Draw a hollow oval * *
RECTF re=new RECTF (10,220,70,250);
Canvas.drawoval (Re, paint);
/* Draw a hollow triangle * *
Path path=new path ();
Path.moveto (10, 330);
Path.lineto (70,330);
Path.lineto (40,270);
Path.close ();
Canvas.drawpath (path, paint);
/* Draw a hollow trapezoid * *
Path path1=new path ();
Path1.moveto (10, 410);
Path1.lineto (70,410);
Path1.lineto (55,350);
Path1.lineto (25, 350);
Path1.close ();
Canvas.drawpath (path1, paint);
          
/* Set Paint's style to FILL: solid */
Paint.setstyle (Paint.Style.FILL);
/* Set the color of paint * *
Paint.setcolor (Color.Blue);
/* Draw a solid circle * *
Canvas.drawcircle (120,40,30, paint);
/* Draw a solid square * *
Canvas.drawrect (the paint);
/* Draw a solid rectangle * *
Canvas.drawrect (170, 150,200, paint);
/* Draw a solid ellipse * *
RECTF re2=new RECTF (90,220,150,250);
Canvas.drawoval (Re2, paint);
/* Draw a solid triangle * *
Path path2=new path ();
Path2.moveto (90, 330);
Path2.lineto (150,330);
Path2.lineto (120,270);
Path2.close ();
Canvas.drawpath (path2, paint);
/* Draw a solid trapezoid * *
Path path3=new path ();
Path3.moveto (90, 410);
Path3.lineto (150,410);
Path3.lineto (135,350);
Path3.lineto (105, 350);
Path3.close ();
Canvas.drawpath (path3, paint);
/* Set Gradient color *
Shader mshader=new lineargradient (0,0,100,100,
New Int[]{color.red,color.green,color.blue,color.yellow},
Null,shader.tilemode.repeat);
Paint.setshader (Mshader);
        
/* Draw a gradient color circle * *
Canvas.drawcircle (200,40,30, paint);
* * Draw a gradient color square
Canvas.drawrect (170, 230, paint);
/* Draw a gradient color rectangle * *
Canvas.drawrect (170, 170, 230,200, paint);
/* Draw a gradient color ellipse * *
RECTF re3=new RECTF (170,220,230,250);
Canvas.drawoval (Re3, paint);
/* Draw a gradient color triangle * *
Path path4=new path ();
Path4.moveto (170,330);
Path4.lineto (230,330);
Path4.lineto (200,270);
Path4.close ();
Canvas.drawpath (Path4, paint);
/* Draw a gradient color trapezoidal * *
Path path5=new path ();
Path5.moveto (170, 410);
Path5.lineto (230,410);
Path5.lineto (215,350);
Path5.lineto (185, 350);
Path5.close ();
Canvas.drawpath (Path5, paint);
          
* * Write//
Paint.settextsize (24);
Canvas.drawtext (Getresources (). getString (R.string.str_text1), M., paint);
Canvas.drawtext (Getresources (). getString (R.STRING.STR_TEXT2), A., paint);
Canvas.drawtext (Getresources (). getString (R.STRING.STR_TEXT3), 190, and paint);
Canvas.drawtext (Getresources () getString (R.STRING.STR_TEXT4), A., paint);
Canvas.drawtext (Getresources () getString (R.STRING.STR_TEXT5);
Canvas.drawtext (Getresources (). getString (R.STRING.STR_TEXT6), 390, and paint);
}
}
}



Related Article

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.