绘图——Android绘图基础:Canvas、Paint等
Android的绘图应该继承View组件,并重写它的onDraw(Canvas canvas)方法即可。
重写onDraw(Canvas canvas)方法时涉及一个绘图API:Canvas,Canvas代表了“依附”于指定View的画布,它提供了如表7.1所示的方法绘制各种图形。
方法签名 | 简要说明 |
---|---|
drawArc(RecF oval,float startAngle,flaot sweepAngle,boolean useCenter,Paint paint) | 绘制弧度 |
drawBitmap(Bitmap bitmap,Rect src,Rect dst,Paint paint) | 在指定点绘制从源位图中“挖取”的一块 |
drawBitmap(Bitmap bitmap,float left,float top,Paint paint) | 在指定点绘制位图 |
drawCircle(float cx,float cy,float radius,Paint paint) | 在指定点绘制一个圆形 |
drawLine(float startX,float startY,float stopX,float stopY,Paint paint) | 绘制一条线 |
drawLines(float[]pts,int offset,int count,Paint paint) | 绘制多条线 |
drawOval(RectF oval,Paint paint) | 绘制椭圆 |
drawPath(Path path,Paint paint) | 沿着指定path绘制任意形状 |
drawPoint(float x,float y,Paint) | 绘制一个点 |
drawPoints(float[] pts,int offset,int count,Paint paint) | 绘制多个点 |
drawRec(float left,float top,float right,float bottom,Paint paint) | 绘制矩形 |
drawRoundRect(RectF rect,float rx,float ry,Paint paint) | 绘制圆角矩形 |
drawText(String text,int start,int end,Paint paint) | 绘制字符串 |
drawTextOnPath(String text,Path path,float hOffset,float vOffset,Paint paint) | 沿着路径绘制字符串 |
clipRect(float left,float top,float right,float bottom) | 剪切一个矩形区域 |
clipRegion(Region region) | 剪切指定区域 |
除了表7.1所定义的各种方法之外,Canvas还提供了如下方法进行坐标变换。
- rotate(float degrees,float px,float py):对Canvas执行旋转变换。
- scale(float sx,float sy,float px,float py):对Canvas执行缩放变换。
- skew(float sx,float sy):对Canvas执行倾斜变换。
- translate(float dx,float dy):移动Canvas。向右移动dx距离(dx为负数即向左移动);向下移动dy距离(dy为负数即向上移动)。
Canvas提供的上面的方法还涉及一个API:Paint,Paint代表了Canvas上的画笔,因此Paint类主要用于设置绘制风格,包括画笔颜色、画笔笔触粗细、填充风格等。Paint提供了如表7.2所示的方法。
方法签名 | 简要说明 |
---|---|
setARGB(int a,int g,int b)/setColor(int color) | 设置颜色 |
setAlpha(int a) | 设置透明度 |
setAntiAlias(boolean aa) | 设置是否抗锯齿 |
setColor(int color) | 设置颜色 |
setPathEffect(PathEffect effect) | 设置绘制路径时的路径效果 |
setShader(Shader shader) | 设置画笔的填充效果 |
setShadowLayer(float radius,float dx,float dy,int color) | 设置阴影 |
setStrokeWidth(float width) | 设置画笔的笔触宽度 |
setStrokeJoin(Paint.Join join) | 设置画笔转弯处的连接风格 |
setStyle(Paint.Style style) | 设置Paint的填充风格 |
setTextAlign(Paint.Align align) | 设置绘制文本时文本的对齐方式 |
setTextSize(float textSize) | 设置绘制文本时的文字大小 |
在Canvas提供的绘制方法中还用到了一个API:Path,Path代表了任意多条直线连接而成的任意图形,当Canvas根据Path绘制时,它可以绘制出任意的形状。
下面的程序示范了如何在Android应用中绘制基本的集合图形,该程序的关键在于一个自定义的View组件,程序重写该View组件的onDraw(Canvas)方法,接下来在该Canvas上绘制了大量几何图形。这个自定义View的代码如下。
package com.example.studydrawable; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.graphics.Shader; import android.util.AttributeSet; import android.view.View; public class MyView extends View { public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override //重写该方法,进行绘图 protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //把整张画布绘制成白色 canvas.drawColor(Color.WHITE); Paint paint=new Paint(); //去锯齿 paint.setAntiAlias(true); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3); //绘制圆形 canvas.drawCircle(40, 40, 30, paint); //绘制正方形 canvas.drawRect(10, 80,70,140,paint); //绘制矩形 canvas.drawRect(10, 150,70,190,paint); RectF re1=new RectF(10,200,70,230); //绘制圆角矩形 canvas.drawRoundRect(re1, 15, 15, paint); RectF rel1=new RectF(10,240,70,270); //绘制椭圆 canvas.drawOval(rel1, paint); //定义一个Path对象,封闭成一个三角形 Path path1=new Path(); path1.moveTo(10, 340); path1.lineTo(70, 340); path1.lineTo(40, 290); path1.close(); //根据Path进行绘制,绘制三角形 canvas.drawPath(path1,paint); //定义一个Path对象,封闭成一个五角形 Path path2=new Path(); path2.moveTo(26, 360); path2.lineTo(54, 360); path2.lineTo(70, 392); path2.lineTo(40, 420); path2.lineTo(10, 392); path2.close(); //根据path进行绘制,绘制五角形 canvas.drawPath(path2, paint); //----------设置填充风格后绘制---------------- paint.setStyle(Paint.Style.FILL); paint.setColor(Color.RED); canvas.drawCircle(120, 40, 30, paint); //绘制正方形 canvas.drawRect(90, 80,150,140,paint); //绘制矩形 canvas.drawRect(90, 150,150,190,paint); RectF re2=new RectF(90,200,150,230); //绘制圆角矩形 canvas.drawRoundRect(re2, 15, 15,paint); RectF re21=new RectF(90,240,150,270); //绘制椭圆 canvas.drawOval(re21, paint); Path path3=new Path(); path3.moveTo(90, 340); path3.lineTo(150, 340); path3.lineTo(120, 290); path3.close(); //绘制三角形 canvas.drawPath(path3, paint); Path path4=new Path(); path4.moveTo(106, 360); path4.lineTo(134, 360); path4.lineTo(150, 392); path4.lineTo(120, 420); path4.lineTo(90, 392); path4.close(); //绘制五角形 canvas.drawPath(path4, paint); //---------设置渐变器后绘制---------- //为Paint设置渐变器 Shader mShader=new LinearGradient(0,0,40,60,new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW} ,null,Shader.TileMode.REPEAT); paint.setShader(mShader); //设置阴影 paint.setShadowLayer(45,10, 10, Color.GRAY); //绘制圆形 canvas.drawCircle(200, 40, 30, paint); //绘制正方形 canvas.drawRect(170,80,230,140, paint); //绘制矩形 canvas.drawRect(170,150,230,190, paint); RectF re3=new RectF(170,200,230,230); //绘制圆角矩形 canvas.drawRoundRect(re3, 15, 15, paint); RectF re31=new RectF(170,240,230,270); //绘制椭圆 canvas.drawOval(re31, paint); Path path5=new Path(); path5.moveTo(170, 340); path5.lineTo(230, 340); path5.lineTo(200, 290); path5.close(); //根据Path进行绘制,绘制三角形 canvas.drawPath(path5, paint); Path path6=new Path(); path6.moveTo(186, 360); path6.lineTo(214, 360); path6.lineTo(230, 392); path6.lineTo(200, 420); path6.lineTo(170, 392); path6.close(); //根据Path进行绘制,绘制五角形 canvas.drawPath(path6,paint); //------设置字符大小后绘制-------- paint.setTextSize(24); paint.setShader(null); //绘制7个字符串 canvas.drawText(getResources().getString(R.string.circle), 240, 50, paint); canvas.drawText(getResources().getString(R.string.square), 240, 120, paint); canvas.drawText(getResources().getString(R.string.rect), 240, 175, paint); canvas.drawText(getResources().getString(R.string.round_rect), 230, 220, paint); canvas.drawText(getResources().getString(R.string.oval), 240, 260, paint); canvas.drawText(getResources().getString(R.string.triangle), 240, 325, paint); canvas.drawText(getResources().getString(R.string.circle), 240, 390, paint); } }
上面的程序中大量调用了Canvas的方法来绘制几何图形,而且程序的粗体字代码还为Paint画笔设置了使用渐变、阴影,因此接下来绘制的几何图形将采用渐变填充,而且具有阴影。使用一个Activity来显示上