Android学习笔记之图形图像处理技术

来源:互联网 发布:淘宝保税区发货靠谱吗 编辑:程序博客网 时间:2024/05/18 02:56


一、常用绘图类

在Android中,绘制图像时最常用的就是Paint类、Canvas类、Bitmap类和BitmapFactory类。其中,Paint类代表画笔,Canvas类代表画布,通过Paint类和Canvas类即可绘制图像。

1.Paint类

Paint类代表画笔,用来描述图形的颜色和风格,如线宽、颜色、透明度和填充效果等信息。使用Paint类时,首先需要创建该类的对象,这可以通过该类提供的构造方法来实现。通常情况下,只需要使用无参数的构造方法来创建一个使用默认设置的Paint对象,具体代码如下:Paint paint = new Paint();创建Paint类的对象后,还可以通过该对象提供的方法来对画笔的默认设置进行改变,例如,改变画笔的颜色、笔触宽度等。

Paint类的常用方法

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

setColor(int color)

setAlpha(int a)

setAntiAlias(Boolean aa)  用于指定是否使用抗锯齿功能,如果使用,会使绘图速度变慢

setDither(boolean dither)       用于指定是否使用图像抖动处理,如果使用,会使图像颜色更加平滑和饱满,使图像更加清晰

setPathEffect(PathEffect effect)   用于设置绘制路径时的路径效果,如点划线。

setShader(Shader shader)   用于设置渐变,可以使用LinearGradient(线性渐变)、RadialGradient(径向渐变)、或者SweepGradient(角度渐变)

setShaderLayer(float radius,float dx,float dy,int color)  用于设置阴影,参数radius为阴影的角度;dx和dy为阴影在x轴和y轴上的距离;color为阴影的颜色。如果参数radius的值为0,那么将没有阴影。

setStrokeCap(Paint.Cap cap)  用于当画笔的填充样式为STROKE或FILL_AND_STROKE时,设置笔刷的图形样式,参数值可以是Cap.BUTT、Cap.ROUND或Cap.SQUARE。主要体现在线的端点上。

setStrokeJoin(Paint.Join join)  用于设置画笔转弯处的连接风格,参数值为Join.BEVEL、Join.MITER或Join.ROUND

setStrokeWidth(fload width)    用于设置笔触的宽度

setStyle(Paint.Style style)       用于设置填充风格,参数值为Style.FILL、Style.FILL_AND_STROKE或Style.STROKE

setTextAlign(Paint.Align align)    用于设置绘制文本时的文字对齐方式,参数值为Align.CENTER、Align.LEFT或Align.RIGHT

setTextSize(float textSize)       用于设置绘制文本时的文字大小

setFakeBoldText(boolean fakeBoldText)   用于设置是否为粗体文字

setXfermode(Xfermode xfermode)    用于设置图形重叠时的处理方式,如合并、取交集或并集,经常用来制作橡皮的擦除。

eg.

Paint paint = new Paint();

Shader shader = new LinearGradient(0,0,50,50,Color.RED,Color.GREEN,Color.GREEN,Shader.TileMode.MIRROR);

paint.setShader(shader);

canvas.drawRect(10,70,100,150,paint);

shader=new RadialGradient(160,110,50,Color.RED,Color.GREEN,Shader.TileMode.MIRROR);

paint.setShader(shader);

canvas.drawRect(115,70,205,150,paint);

shader=new SweepGradient(265,110,new int[]{Color.RED,Color.GREEN,Color.BLUE},null);

paint.setShader(shader);

canvas.drawRect(220,70,310,150,paint);


2.Canvas类

Canvas类代表画布,通过该类提供的方法,可以绘制各种图形(如矩形、圆形和线条等)。通常情况下,要在Android中绘图,需要先创建一个继承自View类的视图,并且在该类中重写其onDraw(Canvas canvas)方法,然后在显示绘图的Activity中添加该试图。

eg.

public class DrawView entends View{

    public DrawView(Context context,AttributeSet attrs){

        super(context,attrs);

    }


    protected void onDraw(Canvas canvas){

        super.onDraw(canvas);

        

        Paint paint =new Paint();

        paint.setColor(Color.RED);

        paint.setShadowLayer(2,3,3,Color.rgb(180,180,180));

        canvas.drawRect(40,40,200,100,paint);

    }

}


3.Bitmap类

Bitmap类代表位图,是Android系统中图像处理的一个重要类。使用该类,不仅可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,而且还可以指定格式保存图像文件。对于这些操作,都可以通过Bitmap类提供的方法来实现。

Bitmap类的常用方法

compress(Bitmap.CompressFormat,int quality,OutputStream stream)

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

createBitmap(int width,int height,Bitmap.Config config)

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

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

createBitmap(Bitmap src)

createScaledBitmap(Bitmap src,int dstWidth,int dstHeight,boolean filter)

isRecycled()

recycle()


4.BitmapFactory类

在Android中,还提供了一个BitmapFactory类,该类为一个工具类,用于从不同的数据源来解析、创建Bitmap对象。

BitmapFactory类的常用方法

decodeFile(String pathName)

decodeFileDescriptor(FileDescriptor fd)

decodeResource(Resources res,int id)

decodeStream(InputStream is)


eg.

String path="/sdcard/pictures/bccd/img01.jpg";

Bitmap bm = BitmapFactory.decodeFile(path);


Bitmap bm = BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.img02);


二、绘制2D图像

Android提供了非常强大的本机二维图形库,用于绘制2D图像。在Android应用中,比较常用的是绘制几何图形、文本、路径和图片等。

1.绘制几何图形

常见的几何图形包括点、线、弧、圆形、矩形等。在Android中,Canvas类提供了丰富的绘制几何图形的方法,通过这些方法,可以绘制出各种几何图形。常用的绘制几何图形方法如下:

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)

drawOval(RectF oval,Paint paint)

drawPoint(float(float x,float y,Paint paint)

drawPoints(float[] pts,Paint paint)

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

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


eg.

public class MyView entends View{

    public MyView(Context context){

        super(context);

    }


    protected void onDraw(Canvas canvas){

        super.onDraw(canvas);


        canvas.drawColor(Color.WHITE);//指定画布的背景颜色为白色

        Paint paint=new Paint();

        paint.setAntiAlias(true);

         paint.setStrokeWidth(3);

         paint.setStyle(Style.STROKE);//设置填充样式为描边

         paint.setColor(Color.BLUE);

         canvas.drawCircle(50,50,30,paint);//绘制蓝色的圆形

          paint.setColor(Color.YELLOW);

          canvas.drawCircle(100,50,30,paint);

          paint.setColor(Color.GREEN);

          canvas.drawCircle(75,90,30,paint);

          paint.setColor(Color.RED);

          canvas.drawCircle(125,90,30,paint);

    }

}

2.绘制文本

在Android中,虽然可以通过TextView或图片显示文本,但是在开发游戏,特别是开发RPG(角色)类游戏时,会包含很多文字,使用TextView和图片显示文本不太合适,这时,就需要通过绘制文本的方式来实现。Canvas类提供了一系列绘制文本的方法,如下:

1)drawText()方法

drawText()方法用于在画布的指定位置绘制文字。常用格式如下

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

在该语法中,参数text用于指定要绘制的文字;x用于指定文字起始位置的X坐标;y用于指定文字起始位置的Y坐标;paint用于指定使用的画笔。

eg.

Paint paintText = new Paint();

paintText.setTextSize(20);

canvas.drawText("明日科技",165,65,paintText);


2)drawPostText()方法

drawPostText()方法也用于在画布上绘制文字,与drawText()方法不同的时,使用该方法绘制字符串时,需要为每个字符指定一个位置。

drawPostText(String text,float[] pos,Paint paint)

在该语法中,参数text用于指定要绘制的文字;pos用于指定每一个字符的位置;Paint用于指定要使用的画笔。

eg.

Paint paintText=new Paint();

paintText.setTextSize(24);

float[] pos = new float[]{80,215,105,215,130,215,80,240,105,240,130,240};

canvas.drawPostText("很高兴见到你",pos,paintText);


3)绘制路径

在Android中提供了绘制路径的功能。绘制一条路径可以分为创建路径和将定义好的路径绘制在画布上两部分。

创建路径

要创建路径,可以使用android.graphics.Path类来实现。Path类包含一组矢量绘图方法,如画圆、矩形、弧、线条等。

addArc(RectF oval,float startAngle,float sweepAngle)

addCircle(float x,float y,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 dir)

moveTo(float x,float y)

lineTo(float x,float y)

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

close()

说明:在使用addCircle()、addOval()、addRect()和addRoundRect()方法时,需要指定Path.Direction类型的常量,可选值为Path.Direction.CW(顺时针)和Path.Direction.CCW(逆时针)。

Path path=new Path();

path.addCircle(150,200,60,PathDirection.CW);


Path mypath=new Path();

mypath.moveTo(50,100);

mypath.lineTo(100,45);

mypath.lineTo(150,100);

mypath.lineTo(200,80);


Path path =new Path();

path.moveTo(50,50);

path.lineTo(100,10);

path.lineTo(150,50);

path.close();


将定义好的路线绘制在画布上

使用Canvas类提供的drawPath()方法,可以将定义好的路径绘制在画布上。

说明:在Android的canvas类中,还提供了另一个应用路径的方法drawTextOnPath(),也就是沿着指定的路径绘制字符串。使用该方法可以绘制环形文字。

eg.

public class MyView extends View{

    public MyView(Context context){

        super(context);

    }


    protected void onDraw(Canvas canvas){

        super.onDraw(canvas);

        

        Paint paint =new Paint();

        paint.setAntiAlias(true);

        paint.setColor(0xFFFF6600);

        paint.setTextSize(18);

        paint.setStyle(Style.STROKE);

        Path pathCircle=new Path();

        pathCircle.add(70,70,40,Path.Direction.CCW);

        canvas.drawPath(pathCircle,paint);


        Path pathLine=new Path();

        pathLine.moveTo(150,100);

        pathLine.lineTo(200,45);

        pathLine.lineTo(250,100);

        pathLine.lineTo(300,80);

        canvas.drawPath(pathLine,paint);

        

        Path pathTr = new Path();

        pathTr.moveTo(350,80);

        pathTr.lineTo(400,30);

        pathTr.lineTo(450,80);

        pathTr.close();

        canvas.drawPath(pathTr,paint);


        String str="风萧萧兮易水寒,壮士一去兮不复还";

        Path path = new Path();

        path.addCircle(550,100,48,Path.Direction.CW);

        paint.setStyle(Style.FILL);

        canvas.drawTextOnPath(str,path,0,-18,paint);

    }

}


4.绘制图片

在Android中,Canvas类不仅可以绘制几何图形、文件和路径,还可用来绘制图片。要想使用Canvas类绘制图片,只需要使用Canvas类提供的如下所示几种方法将Bitmap对象中保存的图片绘制到画布上即可。

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

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

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


eg.

Rect src = new Rect(0,0,500,300);

Rect dst=new Rect((50,50,450,350);

canvas.drawBitmap(bm,src,dst,paint);


ImageView iv =(ImageView) findViewById(R.id.iv);


Paint paint = new Paint();

String path="/sdcard/pictures/bccd/img01.png";

Bitmap bm=BitmapFactory.decodeFile(path);

canvas.drawBitmap(bm,0,30,paint);

Rect src=new Rect(95,150,175,240);

Rect dst=new Rect(420,30,500,120);

canvas.drawBitmap(bm,src,dst,paint);

Bitmap bitmap=Bitmap.createBitmap(new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.MAGENTA},4,1,Config.RGB_565);

iv.setImageBitmap(bitmap);


重写onDestroy()方法,回收ImageView组件中使用的Bitmap资源:

protected void onDestroy(){

    BitmapDrawable b=(BitmapDrawable)iv.getDrawable();

    if(b!=null&&!b.getBitmap().isRecycled()){

       b.getBitmap().recycle();

    }

    super.onDestroy();

}


三、为图形添加特效

1.旋转图像

使用Android提供的android.graphics.Matrix类的setRotate()、postRotate()和preRotate()方法,可以对图像进行旋转。


2.缩放图像

使用Android提供的android.graphics.Matrix类的setScale()、postScale()和preScale()方法,可对图像进行缩放。


3.倾斜图像

使用Android提供的android.graphics.Matrix类的setSkew()、postSkew()和preSkew()方法,可对图像进行倾斜。


4.平移图像

使用Android提供的android.graphics.Matrix类的setTranslate()、postTranslate()和preTranslate()方法可对图像进行平移。


5.使用BitmapShader渲染图像

在Android中,提供的BitmapShader类主要用来渲染图像。如果需要将一张图片裁剪成椭圆形或圆形等形状并显示到屏幕上,就可以使用BitmapShader类来实现。使用BitmapShader来渲染图像的基本步骤如下:

①创建BitmapShader类的对象,可以通过以下构造方法进行创建:BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)

其中,参数bitmap用于指定一个位图对象,通常是要用来渲染的原图像;参数tileX用于指定在水平方向上图像的重复方式;参数tileY用于指定在垂直方向上图像的重复方式。

说明:Shader.TileMode类型的参数包括CLAMP、MIRROR和REPEAT3个可选值,其中,CLAMP为使用边界颜色来填充剩余的空间;MIRROR为采用镜像方式;REPEAT为采用重复方式。

②通过Paint的setShader()方法来设置渲染对象。

③在绘制图像时,使用已经设置了setShader()方法的画笔。

eg.    在MyView的onDraw()中,定义

Paint paint=new Paint();

paint.setAntiAlias(true);

Bitmap bitmap_bg=BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.android);

BitmapShader bitmapshader= new BitmapShader(bitmap_bg,TileMode.REPEAT,TileMode.REPEAT);

paint.setShader(bitmapshader);

canvas.drawRect(0,0,view_width,view_height,paint);

Bitmap bm=BitmapFactory.decodeResource(MainActivity.this.getResources(),R.drawable.img02);

BitmapShader bs = new BitmapShader(bm,TileMode.REPEAT,TileMode.MIRROR);

paint.setShader(bs);

RectF oval = new RectF(0,0,280,180);

canvas.translate(40,20);

canvas.drawOval(oval,paint);


四、Android中的动画

1.实现逐帧动画


2.实现补间动画

①透明度渐变动画(AlphaAnimation)

②旋转动画(RotateAnimation)

③缩放动画(ScaleAnimation)

④平移动画(TranslateAnimation)





阅读全文
0 0
原创粉丝点击