All kinds of graphic drawing of Android

Source: Internet
Author: User

Android drawing pictures or shapes is something we often encounter, through recent studies and online learning cases and materials so I'll summarize today . Android some of the classes and methods used in drawing, where The main include 3 a class Canvas,Paint,Bitmap, here I do not standardize, directly in the main class to add code, if you want to standardize a point you can customize a View , overwrite the OnDraw () method, Add code in OnDraw(), and look first:

Some of these explanations are for reference: http://blog.csdn.net/rhljiayou/article/details/7212620

Common methods for Canvas classes:

drawrect (RECTF rect, paint paint)// draw a rectangle,

DrawPath (path path, paint paint)// Draw a path ( Multiple parameters are composed )

Drawbitmap (Bitmap Bitmap, Rect src, rectdst, paint paint)// Drawing Pictures

DrawLine (float StartX, float starty, floatstopx, float stopy, paintpaint)// Draw Lines Drawpoint (floatx, float y, paint paint)// Draw points

DrawText (String text, float x, floaty,paint Paint)// Render Text

DrawOval (RECTF Oval, paint paint)// Draw Ellipse

drawcircle (float CX, float CY, float radius,paint paint)// Draw a circle, XY coordinate as center point

DrawArc (RECTF Oval, float startangle, float sweepAngle, Booleanusecenter, Paint Paint)// arc,

parameter one is RECTF object, the bounds of a rectangular area ellipse are used to define the shape, size, arc, parameter two is the starting angle ( " at the beginning of the arc,

parametric tri-Sweep angle ( degrees ) start measuring clockwise, parameter four yes, if that's true. , arcs including the center of the ellipse , and close it , if it's fake it's going to be an arc , parameter Five is Paint object;

Common methods of Paint class:

Setargb (int A, int r, int g, int b)// Set Paint Object Color

setalpha (int a)// Set Alpha opacity, the range is 0~255

Setantialias (Boolean aa)// whether Jagged

setcolor (int color)// Set Color

Settextscalex (float ScaleX)// Set the text zoom multiplier, 1.0f for the original

settextsize (float textSize)// Set Font size

Setunderlinetext (Booleanunderlinetext)// Set Underline

Package Com.edu;import Android. R.color;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.Path; Import Android.graphics.rectf;import Android.graphics.typeface;import Android.os.bundle;import Android.view.display;import Android.widget.imageview;public class Drawpictureactivity extends Activity {ImageView ImageView = null; Bitmap alter = null;/** Called when the activity is first created. */@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main) ImageView = (ImageView) Findviewbyid (r.id.imageview1);D isplay current = Getwindowmanager (). Getdefaultdisplay ();//Get system device size//Create a canvas like screen properties, if you do not need to create alter = BITMAP.CREATEBITMAP (Current.getwidth () in the OnDraw method) , Current.getheight (), Bitmap.Config.ARGB_8888);//argb_8888 is composed of 4 (ARGB) 8 bits, namely 32 bits//bitmap bits higher means that it can store more color information, Of course, the image is more realistic. Canvas canvas = new Canvas (alter); Paint paint = new paint ();//Draw Point Paint.setstrokewidth (4.5f);p Aint.setcolor (color.red); Canvas.drawpoint (max, paint) ;//Draw a dot canvas.drawpoints (new float[] {15.5f, 520, 520, 520}, paint);//Draw multiple dots//Set the thickness of the brush paint.setstrokewidth, or the width of the pen (in the Order of ());/ /Paint.setcolor (COLOR.ARGB (200, 200, 200, 200));//Draw Line Paint.setcolor (Color.yellow); Canvas.drawline (100, 50, 500, 650, paint);//Draw round Paint.setstyle (Paint.Style.STROKE);//Paint.Style.STROK---contour//Paint.Style.FILL_AND_ STROKE---fill paint.setcolor (color.blue);//default serrated canvas.drawcircle (n, A, a, a, paint);p Aint.setantialias (TRUE);// Sets the sawtooth effect of the brush, true to remove the sawtooth canvas.drawcircle (S, Max, Max, paint);//Draw ellipse RECTF RF = new RECTF (s, S, max);p Aint.setcolor ( Color.White); Canvas.drawoval (RF, paint);//Draw Rectangle Canvas.drawrect (RF, paint);//arc RECTF Rf1 = new RECTF (200, 600, 500, 800); Paint.setcolor (color.red); Canvas.drawarc (Rf1, N, +, false, paint);//Draw fan RECTF Rf2 = new RECTF (n, N, +);p AI Nt.setcolor (color.red); Canvas.drawarc (Rf2,The first parameter is RECTF: The class is the second argument is the beginning of the angle, the third parameter is how many degrees,//The fourth parameter is true when the sector is painted, is false when the arc//Draw the path Paint.setcolor ( Color.green); Path PATH = new Path ();p Ath.moveto (Ath.lineto),;p ath.lineto (850);p Ath.lineto (400, 1100);p Path.lineto (850);p Ath.lineto (+);p ath.close ();//Closed or Path.lineto (400, 100); that is, the starting position Canvas.drawpath (path, paint);//Draw Text Paint.setcolor (Color.green);p aint.setstrokewidth (2.0f);p aint.settypeface (Typeface.sans_serif);// The parameter typeface is the font style typeface.default: Default font. Paint.settextsize (30); Sets the size of the font canvas.drawtext ("Hello word!! ", A.//Draw a picture//picture, that is, a decal bitmap bitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.ic_ Canvas.drawbitmap (Bitmap, 6, launcher, paint), Imageview.setimagebitmap (Alter), or, of course, the OnDraw of a custom view (canvas Canvas) do not need to get system properties and Settings Paint//IMAGEVIEW.SETIMAGEBITMAP (ALTER), you can remove the */* Typeface.default_bold: Bold font. * * Typeface.MONOSPACE:monospace fonts. * * Typeface.SANS_SERIF:sans fonts. * * Typeface.SERIF:serif fonts. */}}
The comments in the code are clearest, and if you are clear, look at the notes carefully.

All kinds of graphic drawing of Android

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.