vus520 发表于 2013-1-3 17:02:01

android 中 Canvas和Paint

android 中Canvas和Paint

<div class="postbody"><div id="cnblogs_post_body">相关链接:
Drawable、Bitmap、Canvas和Paint的区别
http://www.eoeandroid.com/thread-158137-1-1.html
关于Canvas清屏的常用方法
http://www.eoeandroid.com/thread-212471-1-1.html
【eoeAndroid社区索引】图形图像之Canvas
http://www.eoeandroid.com/thread-173058-1-1.html
---------------正文----------------
Canvas类主要实现了屏幕的绘制过程,其中包含了很多实用的方法,比如绘制一条路径、区域、贴图、画点、画线、渲染文本,下面是Canvas类常用的方法:

void drawRect(RectF rect, Paint paint) //绘制区域,参数一为RectF一个区域
void drawPath(Path path, Paint paint) //绘制一个路径,参数一为Path路径对象
voiddrawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
//贴图,参数一就是我们常规的Bitmap对象,参数二是源区域(即Bitmap的某个区域),参数三是目标区域(应该在canvas的位置和大
小),参数四是Paint画刷对象,因为用到了缩放和拉伸的可能,当原始Rect不等于目标Rect时性能将会有大幅损失。

voiddrawLine(float startX, float startY, float stopX, float stopY, Paint ,paint)//画线,参数一起始点的x轴位置,参数二起始点的y轴位置,参数三终点的x轴水平位置,参数四y轴垂直位置,最后一个参数为Paint
画刷对象。
voiddrawPoint(float x, float y, Paint paint) //画点,参数一水平x轴,参数二垂直y轴,第三个参数为Paint对象。
void drawText(String text, float x, float y, Paint paint)//渲染文本,Canvas类除了上面的还可以描绘文字,参数一是String类型的文本,参数二x轴,参数三y轴,参数四是Paint对象。
voiddrawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) //在路径上绘制文本,相对于上面第二个参数是Path路径对象

从上面来看我们可以看出Canvas绘制类比较简单同时很灵活,实现一般的方法通常没有问题,同时可以叠加的处理设计出一些效果,不过细心的网友可能发现
最后一个参数均为Paint对象。如果我们把Canvas当做绘画师来看,那么Paint就是我们绘画的工具,比如画笔、画刷、颜料等等。

Paint类常用方法:

voidsetARGB(int a, int r, int g, int b)设置Paint对象颜色,参数一为alpha透明通道
voidsetAlpha(int a)设置alpha不透明度,范围为0~255
voidsetAntiAlias(boolean aa)//是否抗锯齿
voidsetColor(int color)//设置颜色,这里Android内部定义的有Color类包含了一些常见颜色定义
voidsetFakeBoldText(boolean fakeBoldText)//设置伪粗体文本
voidsetLinearText(boolean linearText)//设置线性文本
PathEffectsetPathEffect(PathEffect effect)//设置路径效果
RasterizersetRasterizer(Rasterizer rasterizer) //设置光栅化
ShadersetShader(Shader shader)//设置阴影
voidsetTextAlign(Paint.Align align)//设置文本对齐
voidsetTextScaleX(float scaleX)//设置文本缩放倍数,1.0f为原始
voidsetTextSize(float textSize)//设置字体大小

TypefacesetTypeface(Typeface typeface)//设置字体,Typeface包含了字体的类型,粗细,还有倾斜、颜色等。

voidsetUnderlineText(boolean underlineText)//设置下划线
Canvas和Paint在onDraw中直接使用
<div class="cnblogs_code">@Override protected void onDraw(Canvas canvas) {    Paint paintRed=new Paint();    paintRed.setColor(Color.Red);    canvas.drawPoint(11,3,paintRed); //在坐标11,3上画一个红点 }
页: [1]
查看完整版本: android 中 Canvas和Paint