Chinaunix首页 | 论坛 | 博客
  • 博客访问: 180728
  • 博文数量: 38
  • 博客积分: 638
  • 博客等级: 下士
  • 技术积分: 395
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-07 17:16
文章分类

全部博文(38)

文章存档

2013年(1)

2012年(5)

2011年(32)

分类: 嵌入式

2011-12-21 10:02:25

1. Paint
    setAntiAlias:设置画笔的抗锯齿效果
    setColor:设置画笔的颜色
    setAlpha:设置Alpha值
    setTextSize:设置字体大小
    setStyle:设置画笔的风格, Paint.style.FILL(实心) Paint.style.STROKE(空心)
    setStrokeWidth:设置空心的边框宽度
    getColor:得到画笔颜色
    getAlpha:得到Alpha值
2. Color
     Color.BLACK 黑色
     ...
3. Canvas
    Canvas() :创建一个空的画布
    Canvas(Bitmap bitmap): 以bitmap对象创建一个画布
    Canvas(GL gl):OpenGLx相关
    drawColor: 设置Canvas的背景颜色
    setBitmap:设置具体画布
    clipRect: 设置显示区域
    isOpaque: 设置是否支持透明
    rotate:旋转画布
    setViewport: 设置画布中的显示窗口
    skew: 设置偏移量
  1. //Canvas
  2. try {
  3.         canvas = sfh.lockCanvas();
  4.         if (canvas != null) {
  5.             //----设置画布绘图无锯齿
  6.             canvas.setDrawFilter(pfd);
  7.             //----利用填充画布,刷屏
  8.             canvas.drawColor(Color.BLACK);
  9.             //----绘制文本
  10.             canvas.drawText("drawText", 10, 10, paint);
  11.             //----绘制像素点
  12.             canvas.drawPoint(10, 20, paint);
  13.             //----绘制多个像素点
  14.             canvas.drawPoints(new float[] { 10, 30, 30, 30 }, paint);
  15.             //----绘制直线
  16.             canvas.drawLine(10, 40, 50, 40, paint);
  17.             //----绘制多条直线
  18.             canvas.drawLines(new float[] { 10, 50, 50, 50, 70, 50, 110, 50 }, paint);
  19.             //----绘制矩形
  20.             canvas.drawRect(10, 60, 40, 100, paint);
  21.             //----绘制矩形2
  22.             Rect rect = new Rect(10, 110, 60, 130);
  23.             canvas.drawRect(rect, paint);
  24.             canvas.drawRect(rect, paint);
  25.             //----绘制圆角矩形
  26.             RectF rectF = new RectF(10, 140, 60, 170);
  27.             canvas.drawRoundRect(rectF, 20, 20, paint);
  28.             //----绘制圆形
  29.             canvas.drawCircle(20, 200, 20, paint);
  30.             //----绘制弧形
  31.             canvas.drawArc(new RectF(150, 20, 200, 70), 0, 230, true, paint);
  32.             //----绘制椭圆
  33.             canvas.drawOval(new RectF(150, 80, 180, 100), paint);
  34.             //----绘制指定路径图形
  35.             Path path = new Path();
  36.             //设置路径起点
  37.             path.moveTo(160, 150);
  38.             //路线1
  39.             path.lineTo(200, 150);
  40.             //路线2
  41.             path.lineTo(180, 200);
  42.             //路径结束
  43.             path.close();
  44.             canvas.drawPath(path, paint);
  45.             //----绘制指定路径图形
  46.             Path pathCircle = new Path();
  47.             //添加一个圆形的路径
  48.             pathCircle.addCircle(130, 260, 20, Path.Direction.CCW);
  49.             //----绘制带圆形的路径文本
  50.             canvas.drawTextOnPath("PathText", pathCircle, 10, 20, paint);
  51.         }
  52.     } catch (Exception e) {
  53.         // TODO: handle exception
  54.     } finally {
  55.         if (canvas != null)
  56.             sfh.unlockCanvasAndPost(canvas);
  57.     }
//Paint
try {
canvas = sfh.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.WHITE);
//-----设置画笔无锯齿
Paint paint1 = new Paint();
canvas.drawCircle(40, 30, 20, paint1);
paint1.setAntiAlias(true);
canvas.drawCircle(100, 30, 20, paint1);
//-----设置画笔的透明度
canvas.drawText("无透明度", 100, 70, new Paint());
Paint paint2 = new Paint();
paint2.setAlpha(0x77);
canvas.drawText("半透明度", 20, 70, paint2);
//-----设置绘制文本的锚点
canvas.drawText("锚点", 20, 90, new Paint());
Paint paint3 = new Paint();
//设置以文本的中心点绘制
paint3.setTextAlign(Paint.Align.CENTER);
canvas.drawText("锚点", 20, 105, paint3);
//------获取文本的长度
Paint paint4 = new Paint();
float len =paint4.measureText("文本宽度:");
canvas.drawText("文本长度:"+len, 20, 130, new Paint());
//------设置画笔样式
canvas.drawRect(new Rect(20,140,40,160), new Paint());
Paint paint5 = new Paint();
//设置画笔不填充
paint5.setStyle(Style.STROKE);
canvas.drawRect(new Rect(60,140,80,160), paint5);
//------设置画笔颜色
Paint paint6 = new Paint();
paint6.setColor(Color.GRAY);
canvas.drawText("灰色", 30, 180, paint6);
//------设置画笔的粗细程度
canvas.drawLine(20, 200,70, 200, new Paint());
Paint paint7 = new Paint();
paint7.setStrokeWidth(7);
canvas.drawLine(20, 220,70, 220,paint7);
//------设置画笔绘制文本的字体粗细
Paint paint8 = new Paint();
paint8.setTextSize(20);
canvas.drawText("文字尺寸", 20, 260, paint8);
//------设置画笔的ARGB分量
Paint paint9 = new Paint();
paint9.setARGB(0x77, 0xff, 0x00, 0x00);
canvas.drawText("红色半透明", 20, 290, paint9);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (canvas != null)
sfh.unlockCanvasAndPost(canvas);
}

//Bitmap
try {
canvas = sfh.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.BLACK);
//----------绘制位图
//canvas.drawBitmap(bmp, 0, 0, paint);
//----------旋转位图(方式1)
//canvas.save();
//canvas.rotate(30, bmp.getWidth()/2, bmp.getHeight()/2);
//canvas.drawBitmap(bmp, 0, 0, paint);
//canvas.restore();
//canvas.drawBitmap(bmp, 100, 0, paint);
//----------旋转位图(方式2)
//Matrix mx = new Matrix();
//mx.postRotate(30, bmp.getWidth() / 2, bmp.getHeight() / 2);
//canvas.drawBitmap(bmp, mx, paint);
//----------平移位图(方式1)
//canvas.save();
//canvas.translate(10, 10);
//canvas.drawBitmap(bmp, 0, 0, paint);
//canvas.restore();
//----------平移位图(方式2)
//Matrix maT = new Matrix();
//maT.postTranslate(10, 10);
//canvas.drawBitmap(bmp, maT, paint);
//----------缩放位图(方式1)
//canvas.save();
//canvas.scale(2f, 2f, 50 + bmp.getWidth() / 2, 50 + bmp.getHeight() / 2);
//canvas.drawBitmap(bmp, 50, 50, paint);
//canvas.restore();
//canvas.drawBitmap(bmp, 50, 50, paint);
//----------缩放位图(方式2)
//Matrix maS = new Matrix();
//maS.postTranslate(50, 50);
//maS.postScale(2f, 2f, 50 + bmp.getWidth() / 2, 50 + bmp.getHeight() / 2);
//canvas.drawBitmap(bmp, maS, paint);
//canvas.drawBitmap(bmp, 50, 50, paint);
//----------镜像反转位图(方式1)
//X轴镜像
//canvas.drawBitmap(bmp, 0, 0, paint);
//canvas.save();
//canvas.scale(-1, 1, 100 + bmp.getWidth() / 2, 100 + bmp.getHeight() / 2);
//canvas.drawBitmap(bmp, 100, 100, paint);
//canvas.restore();
//Y轴镜像
//canvas.drawBitmap(bmp, 0, 0, paint);
//canvas.save();
//canvas.scale(1, -1, 100 + bmp.getWidth() / 2, 100 + bmp.getHeight() / 2);
//canvas.drawBitmap(bmp, 100, 100, paint);
//canvas.restore();
//----------镜像反转位图(方式2)
//X轴镜像
canvas.drawBitmap(bmp, 0, 0, paint);
Matrix maMiX = new Matrix();
maMiX.postTranslate(100, 100);
maMiX.postScale(-1, 1, 100 + bmp.getWidth() / 2, 100 + bmp.getHeight() / 2);
canvas.drawBitmap(bmp, maMiX, paint);
//Y轴镜像
canvas.drawBitmap(bmp, 0, 0, paint);
Matrix maMiY = new Matrix();
maMiY.postTranslate(100, 100);
maMiY.postScale(1, -1, 100 + bmp.getWidth() / 2, 100 + bmp.getHeight() / 2);
canvas.drawBitmap(bmp, maMiY, paint);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (canvas != null)
sfh.unlockCanvasAndPost(canvas);
}
阅读(2507) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~