首页 > 代码库 > Android Paint和Color类绘画实例

Android Paint和Color类绘画实例

要绘图,首先得调整画笔,待画笔调整好之后,再将图像绘制到画布上,这样才可以显示在手机屏幕上。Android 中的画笔是 Paint类,Paint 中包含了很多方法对其属性进行设置,主要方法如下: 

   setAntiAlias: 设置画笔的锯齿效果。 
   setColor: 设置画笔颜色 
   setARGB:  设置画笔的a,r,p,g值。 
   setAlpha:  设置Alpha值 
   setTextSize: 设置字体尺寸。 
   setStyle:  设置画笔风格,空心或者实心。 
   setStrokeWidth: 设置空心的边框宽度。 
   getColor:  得到画笔的颜色 
   getAlpha:  得到画笔的Alpha值

下面是一个简单的示例 来说明这些方法的使用。先来看看运行效果吧。

技术分享

  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_GameView"; 15 // 声明Paint对象 16 private Paint mPaint = null; 17  18 public GameView(Context context) { 19 super(context); 20 // 构建对象 21 mPaint = new Paint(); 22  23 // 开启线程 24 new Thread(this).start(); 25 } 26  27 @Override 28 protected void onDraw(Canvas canvas) { 29 super.onDraw(canvas); 30  31 // 设置Paint为无锯齿 32 mPaint.setAntiAlias(true); 33  34 // 设置Paint的颜色 35 mPaint.setColor(Color.RED); 36 mPaint.setColor(Color.BLUE); 37 mPaint.setColor(Color.YELLOW); 38 mPaint.setColor(Color.GREEN); 39 // 同样是设置颜色 40 mPaint.setColor(Color.rgb(255, 0, 0)); 41  42 // 提取颜色 43 Color.red(0xcccccc); 44 Color.green(0xcccccc); 45  46 // 设置paint的颜色和Alpha值(a,r,g,b) 47 mPaint.setAlpha(220); 48  49 // 这里可以设置为另外一个paint对象 50 // mPaint.set(new Paint()); 51 // 设置字体的尺寸 52 mPaint.setTextSize(14); 53  54 // 设置paint的风格为“空心” 55 // 当然也可以设置为"实心"(Paint.Style.FILL) 56 mPaint.setStyle(Paint.Style.STROKE); 57  58 // 设置“空心”的外框的宽度 59 mPaint.setStrokeWidth(5); 60  61 // 得到Paint的一些属性 颜色、Alpha值、外框的宽度、字体尺寸 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----------->" + mPaint.getTextSize()); 66  67 // 绘制一空心个矩形 68 canvas.drawRect((320 - 80), 20, (320 - 80) / 2 + 80, 20 + 40, mPaint); 69  70 // 设置风格为实心 71 mPaint.setStyle(Paint.Style.FILL); 72  73 mPaint.setColor(Color.GREEN); 74  75 // 绘制绿色实心矩形 76 canvas.drawRect(0, 20, 40, 20 + 40, mPaint); 77 } 78  79 // 触笔事件 80 public boolean onTouchEvent(MotionEvent event) { 81 return true; 82 } 83  84 // 按键按下事件 85 public boolean onKeyDown(int keyCode, KeyEvent event) { 86 return true; 87 } 88  89 // 按键弹起事件 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 // 更新界面107 postInvalidate();108 }109 }110 }111 112 113 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 }

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

Android Paint和Color类绘画实例