手持终端
Eclipse平台下Android开发环境的搭建
PhoneGap配置到Eclipse开发环境
使用PhoneGap开发Android应用程序
PhoneGap3.0以上版本配置到Eclipse开发环境
使用PhoneGap3.0版本开发Android应用程序
PhoneGap3.0的第三方Android插件
Cordova配置到Android Studio开发环境
Android系统基本架构
Android的字符串颜色图片样式
Android的常用控件及菜单
Android的消息提示及传递
Android系统的组件通信
Android系统的Service
Android的广播消息接收
Android的数据存储与访问
Android系统的网络应用
Android图形图像处理
Android的Fragment框架
Android系统的并发编程
Java源文件结构与保留字
Java语言的数据类型与变量
Java的运算符与流程控制
Java语言的数组及操作
Java面向对象编程
Java的常用类
Android开发常用工具及命令
Android应用程序权限管理
Android系统提供的R类内置资源
Android系统的控件类及其方法
Android系统的菜单类及其方法
Android系统的对话框
Android应用程序模型及组件
Android的绘图类及方法
Android系统的动画效果
Android的数据库操作
Android的多媒体功能
Android系统照相机的使用
Android的文本朗读TTS
获取Android系统信息
Android系统的电话操作
Android系统的定位
Android系统的传感器
Android主要通过Graphics软件包来绘制图形,其中提供了Canvas画布、Paint画笔等常用类,通过这些类中的方法,可以方便地绘制点、线、颜色以及各种几何图形。在Android系统中,几乎所有的游戏都可用Android绘图来实现。
1.Paint类:画笔
在Android中,绘图操作一般是通过Paint画笔在Canvas画布上进行绘制的,最后将Canvas画布呈现给用户。绘图之前需要首先设置Paint画笔,Paint类中提供了很多方法来设置画笔属性,例如颜色、字体、透明度等。
1)setColor方法:设置颜色
该方法用于设置画笔的颜色,可以通过Color类中的预定义颜色来设置,也可以通过指定RGB值来设置。该方法是设置颜色的主要方法,通过改变画笔颜色,可以绘制出色彩缤纷的图形。语法格式:
public void setColor(int color)
其中,参数color为颜色值,也可以直接使用系统Color类中定义的颜色,有如下值:
Color.BLOCK、Color.BLUE、Color.CYAN、Color.DKGRAY、Color.YELLOW、Color.GRAY、
Color.GREEN、Color.LTGRAY、Color.MAGENTA、Color.RED、Color.TRANSPARENT、
Color.WHITE
示例:
package iflab.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class MyGraphics extends View implements Runnable{
private Paint paint=null;
public MyGraphics(Context context){
super(context);
paint=new Paint();
new Thread(this).start();
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setColor(Color.RED);
canvas.drawColor(Color.WHITE);
canvas.drawLine(50,50,450,50,paint);
canvas.drawRect(100,100,200,600,paint);
canvas.drawRect(300,100,400,600,paint);
}
@Override
public void run(){
while(!Thread.currentThread().isInterrupted()){
try{
Thread.sleep(100);
}catch(InterruptedException e){
Thead.currentThead().interrupt();
}
postInvalidate();
}
}
}
代码中,自定义了MyGraphics类,该类继承了View类,并构建了Paint对象。这里重载了onDraw方法,在其中使用setColor方法来设置画笔为红色,接着使用该画笔在Canvas画布上绘制了直线和矩形。完成以上设置之后,还需要在Activity中设置显示这个自定义的View,通过setContentView方法实现,代码为:
public class firstActivity extends Activity{
private MyGraphics myGraphics=null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.myGraphics=new MyGraphics(this);
setContentView(myGraphics);
}
}
2)setAlpha方法:设置透明度
该方法用于设置画笔的透明度,直观上表现为颜色变淡。该方法经常用于一些图片重叠或者特效显示的场合。语法格式:
public void setAlpha(int a)
其中,参数a为透明度,取值范围为0~255,数值越小越透明。
3)setStyle方法:设置风格
该方法用于设置画笔的风格,可以指定是实心还是空心。该方法在矩形、圆形等图形上有明显的效果。语法格式:
public void setStyle(Paint.Style style)
其中,参数style为画笔风格,为Paint.Style类型,可以取值有:
·Style.FILL:实心
·Style.FILL_AND_STROKE:同时实心和空心,在某些场合会带来不可预期效果
·Style.STROKE:空心
4)setStrokeWidth方法:设置空心线宽
该方法用于设置画笔的空心线宽。该方法在矩形、圆形等图形上有明显的效果。语法格式:
public void setStrokeWidth(float width)
其中,参数width为线宽,浮点型数据。示例:
paint.setStrokeWidth((float)10.0);
5)setTextSize方法:设置字体大小
该方法用于设置画笔的字体大小,主要用在绘制字符串的场合。语法格式:
public void setTextSize(float textSize)
其中,参数textSize为字体大小,浮点型数据。
6)setTypeface方法:设置字体样式
该方法用于设置画笔的字体样式,可以指定系统自带的字体,也可以使用自定义的字体。该方法是设置画笔显示文本字体的最常用方法。语法格式:
public Typeface setTypeface(Typeface typeface)
其中,参数typeface为字体样式,具有如下几种取值:
·Typeface.DEFAULT:默认字体
·Typeface.DEFAULT_BOLD:加粗字体
·Typeface.MONOSPACE:monospace字体
·Typeface.SANS_SERIF:sans字体
·Typeface.SERIF:serif字体
7)setTextScaleX方法:设置比例因子
该方法用于设置画笔字体的比例因子,默认为1,当大于1时表示横向拉伸,当小于1时表示横向压缩。该方法往往用于一些特效字体的显示场合。语法格式:
public void setTextScaleX(float scaleX)
其中,参数scaleX为字体比例因子,当大于1时表示横向拉伸,当小于1时表示横向压缩。示例:
paint.setTextScaleX((float)0.7);
8)setARGB方法:设置透明度和颜色
该方法用于设置画笔的颜色和透明度,其中颜色采用的是RGB数值的方式指定。该方法的功能相当于setColor方法和setAlpha方法。语法格式:
public void setARGB(int a,int r,int g,int b)
其中,参数a为透明度,取值范围0~255,数值越小越透明;参数r为红色的颜色值,取值范围为0~255;参数g为绿色的颜色值,取值范围为0~255;参数b为蓝色的颜色值,取值范围为0~255。示例:
paint.setARGB(255,0,255,0);
9)setUnderlineText方法:设置下划线
该方法用于设置画笔的下划线。该方法主要用于绘制字符串的场合。语法格式:
public void setUnderlineText(boolean underlineText)
其中,参数underlineText表示是否显示下划线,true表示显示下划线,false表示不显示下划线。
10)setTextSkewX方法:设置倾斜因子
该方法用于设置画笔的倾斜因子,默认为0,正数表示向左倾斜,负数表示向右倾斜。该方法主要用于绘制字符串的场合。语法格式:
public void setTextSkewX(float skewX)
其中,参数skewX为倾斜因子,正数表示向左倾斜,负数表示向右倾斜。示例:
paint.setTextSkewX((float)-1.0);
2.Canvas类:画布
Android系统中的绘图操作主要是在Canvas画布上进行的,在绘图时使用设置好的Paint画笔。在Android系统中,Canvas类提供了很多常用的图形,如直线、矩形、圆形、文字等,也可以对画布设置颜色、尺寸等。
1)drawColor方法:设置背景颜色
该方法用于设置画布的背景颜色,可以通过Color类中的预定义颜色来设置,也可以通过指定RGB值来设置。该方法是设置颜色的主要方法,通过改变画布颜色,可以绘制出色彩缤纷的背景。语法格式:
public void drawColor(int color)
其中,参数color为颜色值,也可以直接使用系统Color类中定义的颜色。示例:
canvas.drawColor(Color.YELLOW);
2)drawLine方法:绘制直线
该方法用于在画布上绘制直线,通过指定直线的两个端点坐标来绘制。该方法只能绘制单条直线。语法格式:
public void drawLinr(float startX,float startY,float stopX,float stopY,Paint paint)
其中,参数startX为起始点的Z坐标;startY为起始点的Y坐标;stopX为终止点的X坐标;stopY为终止点的Y坐标;paint为绘制直线所使用的画笔。示例:
paint.drawLine(50,250,450,250,paint);
3)drawLines方法:绘制多条直线
该方法用于在画布上绘制多条直线,通过指定直线的端点坐标数组来绘制。语法格式:
public void drawLines(float[] pts,Paint paint)
其中,参数pts绘制直线的端点数组,每条直线占用4个数据;paint为绘制直线所使用的画笔。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setColor(Color.BLACK);
float[] pts={50,50,400,50,400,50,400,600,400,600,50,600,60,600,50,50};
canvas.drawColor(Color.WHITE);
paint.setStrokeWidth((float)5.0);
canvas.drawLines(pts,paint);
}
drawLines还可以有选择地绘制多条直线,语法格式:
public void drawLines(float[] pts,int offset,int count,Paint paint)
其中,参数pts为绘制直线的端点数组,每条直线占用4个数据;offset为跳过的数据个数,这些数据不参与绘制过程;count为实际参与绘制的数据个数;paint为绘制直线所使用的画笔。示例:
canvas.drawLines(pts,4,12,paint);
指定跳过前4个数据,取出12个数据绘制直线。
4)drawPoint方法:绘制点
该方法用于在画布上绘制一个点,通过指定端点坐标来绘制。该方法只能绘制单个点。语法格式:
public void drawPoint(float x,float y,Paint paint)
其中,参数x为绘制点的X坐标;参数y为绘制点的Y坐标。示例:
canvas.drawPoint(300,300,paint);
5)drawPoints方法:绘制多个点
该方法用于在画布上绘制多个点,通过指定端点坐标数组来绘制。该方法可以绘制多个点,同时也可以指定哪些点绘制,而哪些点不绘制。语法格式有两种:
public void drawPoints(float[] pts,Paint paint)
public void drawPoints(float[] pts,int offset,int count,Paint paint)
其中,参数pts为绘制点的数组,每个端点占用2个数据;offset为跳过的数据个数,这些数据不参与绘制过程;count为实际参与绘制的数据个数;paint为绘制直线所使用的画笔。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setColor(Color.BLACK);
float[] pts={50,50,400,50,400,600,60,600};
canvas.drawColor(Color.WHITE);
paint.setStrokeWidth((float)20.0);
canvas.drawLines(pts,paint);
}
6)drawRect方法:绘制矩形
该方法用于在画布上绘制矩形,可以通过指定矩形的四条边来实现,也可以通过指定Rect对象来实现。同时也可以通过设置画笔的空心效果来绘制空心的矩形。语法格式:
public void drawRect(Rect r,Paint paint)
public void drawRect(RectF rect,Paint paint)
public void drawRect(float left,float top,float right,float bottom,Paint paint)
其中,参数r为Rect对象;参数rect为RectF对象;left为矩形的左边位置;top为矩形的上边位置;right为矩形的右边位置;bottom为矩形的下边位置;paint为绘制时所使用的画笔。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
paint.setStrokeWidth((float)3.0);
paint.setStyle(Style.STROKE);
Rect r=new Rect();
r.left=50;
r.top=50;
r.right=450;
r.bottom=250;
canvas.drawRect(r,paint);
canvas.drawRect(50,400,450,600,paint);
}
7)drawRoundRect方法:绘制圆角矩形
该方法用于在画布上绘制圆角矩形,通过指定RectF对象及圆角半径来实现。该方法是绘制圆角矩形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆角矩形。语法格式:
public void drawRoundRect(RectF rect,float rx,float ry,Paint paint)
其中,参数rect为RectF对象;rx为x方向上的圆角半径;ry为y方向上的圆角半径;paint为绘制时所使用的画笔。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
paint.setStrokeWidth((float)3.0);
paint.setStyle(Style.STROKE);
Rect r=new RectF();
r.left=50;
r.top=400;
r.right=450;
r.bottom=600;
canvas.drawRect(r,10,10,paint);
}
8)drawCircle方法:绘制圆形
该方法用于在画布上绘制圆形,通过指定圆形圆心的坐标和半径来实现。该方法是绘制圆心的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆心。语法格式:
public void drawCircle(float cx,float cy,float radius,Paint paint)
其中,参数cx为圆心x坐标;cy为圆心y坐标;radius为圆的半径;paint为绘制所使用的画笔。示例:
canvas.drawCircle(50,50,10,paint);
9)drawOval方法:绘制椭圆形
该方法用于在画布上绘制椭圆形,通过指定椭圆外切矩形RectF对象实现。该方法是绘制椭圆形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的椭圆形。语法格式:
public void drawOval(RectF oval,Paint paint)
其中,参数oval为椭圆外切矩形的RectF对象;paint为绘制时所使用的画笔。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
paint.setStrokeWidth((float)3.0);
paint.setStyle(Style.STROKE);
Rect oval=new RectF();
oval.left=50;
oval.top=400;
oval.right=450;
oval.bottom=600;
canvas.drawOval(oval,paint);
}
10)drawPath方法:绘制任意多边形
该方法用于在画布上绘制任意多边形,通过指定Path对象来实现。在Path对象中规划了多边形的路径信息。该方法是绘制多边形的主要方法,也可以使用drawLines方法来实现。语法格式:
public void drawPath(Path path,Paint paint)
其中,参数path包含路径信息的Path对象;paint为绘制时所使用的画笔。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
paint.setStrokeWidth((float)3.0);
paint.setStyle(Style.STROKE);
Path path=new Path();
path.moveTo(50,100);
path.lineTo(50,300);
path.lineTo(100,500);
path.lineTo(400,500);
path.lineTo(300,300);
path.lineTo(450,50);
path.lineTo(200,200);
canvas.drawPath(path,paint);
}
11)drawArc方法:绘制圆弧
该方法用于在画布上绘制圆弧,通过指定圆弧所在的椭圆对象、起始角度、终止角度来实现。该方法是绘制圆弧的主要方法。语法格式:
public void drawArc(RectF oval,float startAngle,float sweepAngle,boolean useCenter,Paint paint)
其中,参数oval为圆弧所在的椭圆对象;startAngle为圆弧的起始角度;sweepAngle为圆弧的角度;useCenter设置是否显示半径连线,true表示显示圆弧与圆心的半径连线,false表示不显示;paint为绘制时所使用的画笔。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
paint.setStrokeWidth((float)3.0);
paint.setStyle(Style.STROKE);
Rect oval=new RectF();
oval.left=100;
oval.top=100;
oval.right=400;
oval.bottom=300;
canvas.drawOval(oval,200,135,true,paint);
}
12)drawText方法:绘制字符串
该方法用于在画布上绘制字符串,通过指定字符串的内容和显示的位置来实现。在画布上绘制字符串是经常用到的操作,字体的大小、样式等信息都需要在Paint画笔中来指定。基本语法有多种形式:
public void drawText(String text,float x,float y,Paint paint)
public void drawText(char[] text,int index,int count,float x,float y,Paint paint)
public void drawText(CharSequence text,int start,int end,float x,float y,Paint paint)
public void drawText(String text,int start,int end,float x,float y,Paint paint)
其中,参数text为字符串内容,可以采用String格式,也可以采用char字符数组形式;x为显示位置的x坐标;y为显示位置的y坐标;index为显示的起始字符位置;count为显示的字符个数;start为显示的起始字符位置;end为显示的终止字符位置;paint为绘制时所使用的画笔。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
paint.setTextSize((float)30.0);
String str="Android应用程序开发";
char[] ch={'H','e','l','l','o',' ','A','n','d','r','o','i','d'};
canvas.drawText(str,50,200,paint);
canvas.drawText(ch,0,ch.length,50,300,paint);
canvas.drawText(str+" API详解",0,str.length()+6,50,400,paint);
canvas.drawText(str,7,str.length(),50,500,paint);
}
13)drawBitmap方法:绘制图像
该方法用于在画布上绘制图像,通过指定Bitmap对象来实现。语法格式:
public void drawBitmap(Bitmap bitmap,float left,float top,Paint paint)
其中,bitmap为Bitmap对象,代表了图像资源;left为图像显示的左边位置;top为图像显示的上边位置;paint为绘制时所使用的画笔。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
paint.setTextSize((float)3.0);
paint.setStyle(Style.STROKE);
Bitmap bitmap=null;
bitmap=(BitmapDrawable)getResources().getDrawable(R.drawable.icon)).getBitmap();
canvas.drawBitmap(bitmap,50,50,null);
bitmap=(BitmapDrawable)getResources().getDrawable(R.drawable.bulb_on)).getBitmap();
canvas.drawBitmap(bitmap,50,150,null);
bitmap=(BitmapDrawable)getResources().getDrawable(R.drawable.bulb_off)).getBitmap();
canvas.drawBitmap(bitmap,50,450,null);
}
14)save方法:锁定画布
该方法用于锁定画布,这种方法主要用于锁定画布中的某一个或几个对象。使用save方法锁定画布并完成操作之后,需要使用restore方法解除锁定。语法格式:
public int save()
15)restore方法:解除锁定
该方法用于解除锁定的画布,这种方法主要用在save方法之后。使用save方法锁定画布并完成操作之后,需要使用restore方法解除锁定。语法格式:
public void restore()
16)clipRect方法:设置裁剪区
该方法用于裁剪画布,也就是设置画布的显示区域。在使用时,可以使用Rect对象来指定裁剪区,也可以通过指定矩形的4条边来指定裁剪区。语法格式有几种:
public boolean clipRect(Rect rect)
public boolean clipRect(float left,float top,float right,float bottom)
public boolean clipRect(int left,int top,int right,int bottom)
其中,rect为Rect对象,用于定义裁剪区的范围;left为矩形裁剪区的左边位置,可以是浮点型或者整型;top为矩形裁剪区的上边位置,可以是浮点型或者整型;right为矩形裁剪区的右边位置,可以是浮点型或者整型;bottom为矩形裁剪区的下边位置,可以是浮点型或者整型。
17)rotate方法:旋转画布
该方法用于旋转画布。通过旋转画布,可以将画布上绘制的所有对象旋转,为了只对某一个对象进行旋转,则可以通过save方法锁定画布,然后执行旋转操作,最后通过restore方法解锁,此后再绘制其他图形。语法格式有几种:
public void rotate(float degrees)
public final void rotate(float degrees,float px,float py)
其中,参数degrees为旋转的角度,正数为顺时针方向,负数为逆时针方向;px为旋转点的x坐标;py为旋转点的y坐标。示例:
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setTextSize((float)30.0);
canvas.drawColor(Color.WHITE);
canvas.clipRect(50,50,400,700);
canvas.save();
canvas.rotate(45,230,250);
paint.setColor(Color.BLUE);
canvas,drawText("Hello Android!",130,250,paint);
canvas.restore();
paint.setColor(Color.RED);
canvas,drawText("Hello Android!",130,250,paint);
RectF oval=new RectF();
oval.left=150;
oval.top=500;
oval.right=350;
oval.bottom=600;
canvas.drawOval(oval,paint);
}