To draw, you have to adjust the brush first, and then draw the image onto the canvas after the brush is adjusted so that it can be displayed on the phone screen. The brush in Android is the paint class, and the paint contains many ways to set its properties, primarily as follows:
Setantialias: Sets the brush's jagged effect.
SetColor: Setting Brush colors
Setargb: Sets the a,r,p,g value of the brush.
Setalpha: Setting alpha value
Settextsize: Sets the font size.
SetStyle: Sets the brush style, hollow or solid.
Setstrokewidth: Sets the hollow border width.
GetColor: Get the color of the brush
Getalpha: Get the alpha value of the brush
The following is a simple example to illustrate the use of these methods. Let's take a look at the running effect.
1 PackageEoE. Demo;2 3 ImportAndroid.content.Context;4 ImportAndroid.graphics.Canvas;5 ImportAndroid.graphics.Color;6 ImportAndroid.graphics.Paint;7 ImportAndroid.util.Log;8 Importandroid.view.KeyEvent;9 Importandroid.view.MotionEvent;Ten ImportAndroid.view.View; One A Public classGameviewextendsViewImplementsRunnable { - - Public Final StaticString TAG = "Example_05_03_gameview"; the //declaring a Paint object - PrivatePaint Mpaint =NULL; - - PublicGameview (Context context) { + Super(context); - //Building Objects +Mpaint =NewPaint (); A at //Open Thread - NewThread ( This). Start (); - } - - @Override - protected voidOnDraw (canvas canvas) { in Super. OnDraw (canvas); - to //set Paint to non-aliased +Mpaint.setantialias (true); - the //set the color of paint * Mpaint.setcolor (color.red); $ Mpaint.setcolor (color.blue);Panax Notoginseng Mpaint.setcolor (color.yellow); - Mpaint.setcolor (color.green); the //also set the color +Mpaint.setcolor (Color.rgb (255, 0, 0)); A the //Extract Color +Color.Red (0XCCCCCC); -Color.green (0XCCCCCC); $ $ //set color and alpha values for paint (a,r,g,b) -Mpaint.setalpha (220); - the //This can be set to another paint object - //Mpaint.set (New Paint ());Wuyi //set the size of the font theMpaint.settextsize (14); - Wu //set the paint style to "hollow" - //of course, it can also be set to "solid" (Paint.Style.FILL) About Mpaint.setstyle (Paint.Style.STROKE); $ - //set the width of the outline of the "hollow" frame -Mpaint.setstrokewidth (5); - A //get some properties of paint color, alpha value, width of outer frame, font size +LOG.I ("TAG", "Paint Color------>" +Mpaint.getcolor ()); theLOG.I (TAG, "Paint Alpha------->" +Mpaint.getalpha ()); -LOG.I ("TAG", "Paint strokewidth--------->" +mpaint.getstrokewidth ()); $LOG.I ("TAG", "Paint TextSize----------->" +mpaint.gettextsize ()); the the //draw a hollow rectangle theCanvas.drawrect (320-80), 20, (320-80)/2 + 80, 20 + 40, mpaint); the - //set style to solid in Mpaint.setstyle (Paint.Style.FILL); the the Mpaint.setcolor (color.green); About the //draw a solid green rectangle theCanvas.drawrect (0, 20, 40, 20 + 40, mpaint); the } + - //Stylus Events the Public Booleanontouchevent (Motionevent event) {Bayi return true; the } the - //Press the event by pressing the key - Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { the return true; the } the the //press the key to bounce the event - Public BooleanOnKeyUp (intKeyCode, KeyEvent event) { the return true; the } the 94 Public BooleanOnkeymultiple (intKeyCode,intRepeatCount, KeyEvent event) { the return true; the } the 98 @Override About Public voidrun () { - while(!Thread.CurrentThread (). isinterrupted ()) {101 Try {102Thread.Sleep (100);103}Catch(Exception e) {104 Thread.CurrentThread (). interrupt (); the }106 //Update Interface107 postinvalidate ();108 }109 } the }111 the 113 PackageEoE. Demo; the the Importandroid.app.Activity; the ImportAndroid.os.Bundle;117 118 Public classActivity01extendsActivity {119 /**Called when the activity is first created.*/ - PrivateGameview Mgameview;121 122 @Override123 Public voidonCreate (Bundle savedinstancestate) {124 Super. OnCreate (savedinstancestate); the 126 Setcontentview (r.layout.main);127 -Mgameview =NewGameview ( This);129 the Setcontentview (mgameview);131 } the}
(ext: http://www.cnblogs.com/-OYK/archive/2011/10/25/2223624.html)
Android Paint and Color class painting instances