Android游戏开发基础part3--Paint 画笔
游戏开发基础part3--Paint 画笔
Paint画笔是绘图的辅助类,一般它是作为画布的参数来实现相应的效果,Paint类中包含文字与位图的样式、颜色等属性信息。Paint的常用方法如下:
1.setAntiAlias(boolean aa)
作用:设置画笔是否无锯齿
参数:true表示无锯齿,false表示有锯齿,默认为flase。
2.setAlpha(int a)
作用:设置画笔透明度
参数:透明度
3.setTextAlign(Paint.Align align)
作用:设置绘制文本的锚点
参数:Paint.Align类中的常量
4.measureText(String text)
作用:获取文本内容的宽度
参数:文本内容
5.setStyle(Style style)
作用:设置画笔样式
参数:样式实例
6.setColor(int color)
作用:设置画笔颜色
参数:色值
7.setStrokeWidth(float width)
作用:设置画笔的粗细程度
参数;画笔粗细值
8.setTextSize(float textSize)
作用:设置画笔在绘制文本时,文本字体的尺寸
参数:尺寸值
9.setARGB(int a, int r ,int g, int b)
作用:设置画笔的ARGB分量
第一个参数:画笔透明度分量
第二个参数:画笔红色分量
第三个参数:画笔绿色分量
第四个参数:画笔蓝色分量
创建项目实例:PaintProject
项目运行效果:
项目源代码:
==>MySurfaceView.java
package com.paint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Rect; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; public class MySurfaceView extends SurfaceView implements Callback { //用于控制SurfaceView private SurfaceHolder sfh; private Paint paint; private Canvas canvas; public MySurfaceView(Context context) { super(context); //实例SurfaceHolder sfh = this.getHolder(); //为SurfaceView添加状态监听 sfh.addCallback(this); //实例一个画笔 paint = new Paint(); //设置画笔颜色为白色 paint.setColor(Color.WHITE); } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub myDraw(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } /** * 自定义绘图函数 */ public void myDraw(){ try{ canvas = sfh.lockCanvas(); if (canvas != null){ canvas.drawColor(Color.WHITE); //----设置画笔无锯齿 Paint paint1 = new Paint(); canvas.drawCircle(40, 30, 20, paint1); paint1.setAntiAlias(true); canvas.drawCircle(100, 30, 20, paint1); //----设置画笔的透明度 canvas.drawText("无透明度",100,70,new Paint()); Paint paint2 = new Paint(); paint2.setAlpha(0x77); canvas.drawText("半透明度",20,70,paint2); //----设置绘制文本的锚点 canvas.drawText("锚点", 20, 90, new Paint()); Paint paint3 = new Paint(); //设置以文本的中心绘制 paint3.setTextAlign(Paint.Align.CENTER); canvas.drawText("锚点",20,105,paint3); //-----获取文本的长度 Paint paint4 = new Paint(); float len = paint4.measureText("文本宽度:"); canvas.drawText("文本长度:"+len, 20, 130, new Paint()); //----设置画笔样式 canvas.drawRect(new Rect(20,140,40,160),new Paint()); Paint paint5 = new Paint(); //设置画笔不填充 paint5.setStyle(Style.STROKE); canvas.drawRect(new Rect(60,140,40,160), paint5); //----设置画笔颜色 Paint paint6 = new Paint(); paint6.setColor(Color.GRAY); canvas.drawText("灰色", 30, 180, paint6); //----设置画笔粗细程度 canvas.drawLine(20, 200, 70, 200, new Paint()); Paint paint7 = new Paint(); paint7.setStrokeWidth(7); canvas.drawLine(20, 220, 70, 220, paint7); //----设置画笔绘制文本的字体粗细 Paint paint8 = new Paint(); paint8.setTextSize(20); canvas.drawText("文字尺寸", 20, 260, paint8); //----设置画笔ARGB分量 Paint paint9 = new Paint(); paint9.setARGB(0x77, 0xff, 0x00, 0x00); canvas.drawText("红色半透明", 20, 290, paint9); } } catch(Exception e){ }finally{ if(canvas != null) sfh.unlockCanvasAndPost(canvas); } } }