赵工的个人空间


专业技术部分转网页计算转业余爱好部分


 手持终端

首页 > 专业技术 > 手持终端 > Android图形图像处理
Android图形图像处理

1.画布Canvas:

Android中,使用Canvas作为画布,这是android.graphics下的类,通过Canvas就可以绘制各种图形。
要在Android中绘图,一般流程是先创建一个继承View类的视图,然后重写onDraw()方法,再在Activity中添加视图。示例:
@Override
protected void onDoaw(Canvas canvas){
super.onDraw(canvas);
Paint paint=new Paint();
paint.setColor(Color.RED);
paint.setShadowLayout(2,3,3,Color.BLACK);
canvas.drawRect(20,20,120,120,paint);
}
为了使用上述代码,要在布局文件中使用DrawView控件。

2.画笔Paint:

Android中,画笔为Paint,是android.graphics下的类,通过Paint能够对需要绘制的图像设置颜色和风格,如线宽、颜色、透明度等。
Paint的一般创建方法为:
Paint paint=new Paint();
创建完成后就可以对实例化后的画笔Paint设置或读取相关属性了。Paint常用的方法为:

方法

说明

setColor(int color)

设置颜色,可使用多种方式指定参数

setAlpha(int a)

设置透明度,参数a为0~255之间的整数

void setARGB(int a,int r,int g,int b)

设置颜色,使用ARGB格式

setAntiAlias(boolean aa)

设置是否抗锯齿

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)

设置画笔的填充风格

setTextAlign(Paint.Align align)

设置绘制文本时的文字对齐方式

setTextSize(float textSize)

设置绘制文本时的文字大小

3.位图处理Bitmap:

1)位图对象类Bitmap:
Android中,Bitmap是一个位图对象类,用于存放位图数据。当需要对已有的图片资源进行操作时,通常不会对原始图片文件直接操作,而是将文件加载到Bitmap对象中,然后再对Bitmap实例对象进行相关操作。
如果以Drawable的方式访问Bitmap资源,可以通过实例化BitmapDrawable方法实现:
BitmapDrawable drawable=new BitmapDrawable(bitmap);
如果需要将drawable中的资源载入到一个bitmap中去,使用代码:
Bitmap bitmap=drawable.getBitmap();
Bitmap类提供了很多方法,用于创建和回收Bitmap对象:

方法

说明

createBintmap(Bitmap source,int x,int y,int width,int height)

从源位图source的指定坐标点开始,从中挖取宽width高height的一块出来,创建新的Bitmap对象

createScaledBitmap(Bitmap src,int src,int dstWidth,int dstheight,Boolean filter)

对源位图src进行缩放,缩放成宽dstWidth高dstHeight的新位图

createBitmap(int width,int height,Config config)

创建指定格式、大小的位图

createBitmap(Bitmap source,int x,int y,int width,int height)

以source为原图,创建新的图片,指定起始坐标及新图像的高度宽度

createBitmap(Bitmap source,int x,int y,int width,int height,Matrix m,boolean filter)

从源位图source的指定坐标点开始,挖取宽width高height的一块,创建新Bitmap对象,并按Matrix指定规则变换

Boolean isRecycled()

返回该Bitmap对象是否已被回收

void recycle()

强制一个Bitmap对象立即回收自己

2)BitmapFactory:
BitmapFactory是配合Bitmap使用的一个工具类,提供了解析各种来源文件并创建为Bitmap对象的功能。常用方法为:

方法

说明

decodeByteArray(byte[] data,int offset,int length)

从指定字节数组的offset位置开始,将长度为length的字节数据解析成Bitmap对象

decodeFile(String pathName)

从pathName指定的文件中解析、创建Bitmap对象

decodeFileDescriptor(FileDescriptor fd)

用于从FileDescriptor对应的文件中解析、创建Bitmap对象

decodeResource(Resource res,int id)

用于根据指定的资源ID从指定资源中解析、创建Bitmap对象

decodeStream(InputStream is)

用于从指定输入流中解析、创建Bitmap对象

4.几何图形绘制:

Android支持的几何图形绘制方法有:

方法

说明

drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, 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,Paint paint)
drawLines(float[] pts,int offset,int count,Paint paint)

绘制多条线段

drawOval(RectF oval,Paint paint)

绘制椭圆

drawPoint(float x,float y,Paint paint)

绘制单点

drawPoint(float[] pts,Paint paint)
drawPoint(float[] pts,int offset,int count,Paint paint)

绘制多点

drawRect(float left,float right,float bottom,Paint paint)

绘制矩形

drawRoundRect(RectF rect,float rx,float ry,Paint paint)

绘制圆角矩形

5.文本:

Android中,可在Canvas中绘制文本,有两种方法:

方法

说明

drawText(String text,float x,float y,Paint paint)

给定坐标,绘制文本

drawText(String text,int start,int end,float x,float y,Paint paint)

给定坐标,绘制文本的第start个字符到第end个字符

drawPosText(String text,float[] pso,Paint paint)

给定每个字符坐标绘制文本,不推荐

6.路径:

对于不规则图形,如多边形,要使用路径的方法来绘制。常用方法有:

方法

说明

addArc(RectF oval, float startAngle, float sweepAngle)

添加一条弧线

addCircle(float cx,float cy,float radius,Path.Direction dir)

添加一个圆形路径,要设置方向

addOval(RectF oval,Path.Direction dir)

添加一个椭圆路径,要设置方向

addRect(RectF rect,Path.Direction dir)

添加一个矩形路径,要设置方向

addRoundRect(RectF rect,float rx,float ry,Path.Direction dirt)

添加一个圆角矩形路径,要设置方向

addPath(path src)或addPath(Path src,float dx,float dy)

复制一个Path路径到本路径

close()

闭合路径

moveTo(float x,float y)

设置线段的起点

lineTo(float x,float y)

添加一条线段,以moveTo或上一个lineTo为起点

quadTo(float x1,float y1,float x2,float y2)

添加一条线段路径

7.图片:

Android中,Canvas还可以利用已有的Bitmap资源进行绘图,常用方法有:

方法

说明

addBitmap(int[] colors,int offset,int stride,float x,float y,int width,int height,boolean hasAlpha,Paint paint)

把一组颜色处理为一个Bitmap位图并绘制出来

drawBitmap(Bitmap bitmap,Matrix matrix,Paint paint)

通过矩阵来绘制Bitmap位图

drawBitmap(Bitmap bitmap,Rect src,RectF dst,Paint paint)

从指定点绘制从Bitmap中挖取的矩形

drawBitmap(Bitmap bitmap,float left,float top,Paint paint)

在指定点位置绘制完整的Bitmap位图

creatBitmap(Bitmap src)

通过已经存在的位图创建新位图

createBitmap(int[] colors, int width, int height, Bitmap.Config config)

通过色彩数组创建色彩渐变位图

createBitmapBitmap source,int x,int y,int width,int height,Matrix m,boolean filter)

通过从原图位置挖去一块,然后根据Matrix和filter创建新位图

8.图形特效处理:

Android提供了android.graphic.Matrix类,其中有一系列方法实现对图形的特效处理。Matrix是通过对坐标进行数学变换,基于3x3的矩阵来实现,提供了4类转换,分别是平移Translate、旋转Rotate、缩放Scale、倾斜Skew,每种变换都对应3种运算方法,即清空并执行set、后乘post、前乘pre。这三种方法对应的转换参数是一样的,但执行效果不同。
⑴平移Translate:
平移是最简单的一种变换。

方法

说明

setTranslate(float dx,float dy)

将图形移动到(dx,dy)坐标位置上

postTranslate(float dx,float dy)

通过矩阵来绘制Bitmap位图

preTranslate(float dx,float dy)

从指定点绘制从Bitmap中挖取的矩形

⑵旋转:
旋转有2种参数设置方法,一种是指定旋转度数,另一种是设置轴心和旋转度数。

方法

说明

setRotate(float degrees)

将图形以(0,0)为轴心旋转degrees度

postRotate(float degrees)

 

preRotate(float degrees)

 

setRotate(float degrees,float px,float py)

将图形以(px,py)为轴心旋转degrees度

postRotate(float degrees,float px,float py)

 

preRotate(float degrees,float px,float py)

 

⑶缩放:
缩放有2种参数设置方法,一种是只指定缩放比例,另一种是设置轴心和缩放比例。

方法

说明

setScale(float sx,float sy)

将图形以(0,0)为轴心,水平方向缩放比例sx垂直方向缩放比例sy,进行缩放

postScale(float sx,float sy)

 

preScale(float sx,float sy)

 

setScale(float sx,float sy,float px,float py)

将图形以(px,py)为轴心,水平方向缩放比例sx垂直方向缩放比例sy,进行缩放

postScale(float sx,float sy,float px,float py)

 

preScale(float sx,float sy,float px,float py)

 

⑷倾斜:
倾斜有2种参数设置方法,一种是只指定倾斜度,另一种是设置轴心和倾斜度。

方法

说明

setSkew(float kx,float ky)

将图形以(0,0)为轴心,水平方向倾斜kx垂直方向倾斜ky

postSkew(float kx,float ky)

 

preSkew(float kx,float ky)

 

setSkew(float kx,float ky,float px,float py)

将图形以(px,py)为轴心,水平方向倾斜kx垂直方向倾斜ky

postSkew(float kx,float ky,float px,float py)

 

preSkew(float kx,float ky,float px,float py)

 

9.扭曲:

Canvas提供了drawBitmapMesh()方法,通过它能有效地实现图片扭曲。
drawBitmapMesh(Bitmap bitmap,int meshWidth,int meshHeight,float[] verts,int vertOffset,int[] colors,int colorOffset,Paint paint)
其中包含很多参数:

参数

说明

bitmap

需要扭曲的位图

meshWidth

横向分割网格数

meshHeight

纵向分割网格数

verts

扭曲后,各网格顶点的位置,需要(meshWidth+1)*(meshHeight+1)*2+vertOffset个值

vertOffset

设置verts数组中开始产生扭曲影响的偏移量,即从第几个点开始扭曲

colors

对网格顶点进行着色,可以设为null,如设置则需要有(meshWidth+1)*(meshHeight+1)*2 +colorOffset个值

colorOffset

开始产生顶点着色影响的偏移量

10.位图着色器:

Android提供了BitmapShader类,提供了对2D位图的X方向和Y方向单独着色渲染的功能。只有一个设置方法:
BitmapShader(Bitmap bitmap,Shader.TitleMode tileX,Shader.TileMode tileY)
tileX和tileY代表这两个变量分别指定的是X方向和Y方向,Shader.TileMode是着色方法,一共有3种,分别是使用边界颜色填充CLAMP、镜像MIRROR、重复REPEAT。
实例化后的位图着色器只是一个参数,需将该对象利用Paint的setShader()方法设置给需要的画笔。

Copyright@dwenzhao.cn All Rights Reserved   备案号:粤ICP备15026949号
联系邮箱:dwenzhao@163.com  QQ:1608288659