Android Paint and Color painting examples, androidpaint

Source: Internet
Author: User

Android Paint and Color painting examples, androidpaint

To draw a picture, first adjust the paint brush. After the paint brush is adjusted, draw the image on the canvas to display it on the mobile phone screen. The Paint brush in Android is a Paint class, which contains many methods to set its attributes. The main method is as follows:

SetAntiAlias: Set the watermark effect of the paint brush.
SetColor: Set the paint brush color.
SetARGB: Set the, r, p, and g Values of the paint brush.
SetAlpha: Set the Alpha value.
SetTextSize: Set the font size.
SetStyle: Set the paint brush style, hollow or solid.
SetStrokeWidth: Set the width of the hollow border.
GetColor: get the paint brush color.
GetAlpha: Get the Alpha value of the paint brush.

The following is a simple example to illustrate the use of these methods. Let's take a look at the running effect.

1 package eoe. demo; 2 3 import android. content. context; 4 import android. graphics. canvas; 5 import android. graphics. color; 6 import android. graphics. paint; 7 import android. util. log; 8 import android. view. keyEvent; 9 import android. view. motionEvent; 10 import android. view. view; 11 12 public class GameView extends View implements Runnable {13 14 public final static String TAG = "Example_05_03_GameV Iew "; 15 // declare the Paint object 16 private Paint mPaint = null; 17 18 public GameView (Context context) {19 super (context ); 20 // build object 21 mPaint = new Paint (); 22 23 // enable Thread 24 new Thread (this ). start (); 25} 26 27 @ Override 28 protected void onDraw (Canvas canvas) {29 super. onDraw (canvas); 30 31 // set the Paint to a 32 mPaint without tooth. setAntiAlias (true); 33 34 // set the Paint color to 35 mPaint. setColor (Color. RED); 36 mPaint. setColo R (Color. BLUE); 37 mPaint. setColor (Color. YELLOW); 38 mPaint. setColor (Color. GREEN); 39 // set the color to 40 mPaint. setColor (Color. rgb (255, 0, 0); 41 42 // extract Color 43. red (0 xcccccc); 44 Color. green (0 xcccccc); 45 46 // set the color and Alpha value of the paint (a, r, g, B) 47 mPaint. setAlpha (220); 48 49 // here it can be set to another paint object 50 // mPaint. set (new Paint (); 51 // set the font size 52 mPaint. setTextSize (14); 53 54 // set the paint style to "Hollow" 55 // Of Course You can also set Set to "solid" (Paint. style. FILL) 56 mPaint. setStyle (Paint. style. STROKE); 57 58 // set the width of the "Hollow" outer frame 59 mPaint. setStrokeWidth (5); 60 61 // you can obtain the Paint color, Alpha value, outer frame width, and font size 62 Log. I ("TAG", "paint Color ------>" + mPaint. getColor (); 63 Log. I (TAG, "paint Alpha ------->" + mPaint. getAlpha (); 64 Log. I ("TAG", "paint StrokeWidth --------->" + mPaint. getStrokeWidth (); 65 Log. I ("TAG", "paint TextSize ----------->" + mPa Int. getTextSize (); 66 67 // draw a hollow rectangle 68 canvas. drawRect (320-80), 20, (320-80)/2 + 80, 20 + 40, mPaint); 69 70 // set the style to solid 71 mPaint. setStyle (Paint. style. FILL); 72 73 mPaint. setColor (Color. GREEN); 74 75 // draw a GREEN solid rectangle 76 canvas. drawRect (0, 20, 40, 20 + 40, mPaint); 77} 78 79 // trigger event 80 public boolean onTouchEvent (MotionEvent event) {81 return true; 82} 83 84 // press the button to event 85 public boolean onKey Down (int keyCode, KeyEvent event) {86 return true; 87} 88 89 // press the button to bring up event 90 public boolean onKeyUp (int keyCode, KeyEvent event) {91 return true; 92} 93 94 public boolean onKeyMultiple (int KeyCode, int repeatCount, KeyEvent event) {95 return true; 96} 97 98 @ Override 99 public void run () {100 while (! Thread. currentThread (). isInterrupted () {101 try {102 Thread. sleep (100); 103} catch (Exception e) {104 Thread. currentThread (). interrupt (); 105} 106 // Update page 107 postInvalidate (); 108} 109} 110 111 112 package eoe. demo; 114 115 import android. app. activity; 116 import android. OS. bundle; 117 118 public class Activity01 extends Activity {119/** Called when the activity is first created. */120 private GameView mGameView; 121 122 @ Override123 public void onCreate (Bundle savedInstanceState) {124 super. onCreate (savedInstanceState); 125 126 setContentView (R. layout. main); 127 128 mGameView = new GameView (this); 129 130 setContentView (mGameView); 131} 132}

(To: http://www.cnblogs.com/-OYK/archive/2011/10/25/2223624.html)

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.