-
得到canvas画布对象
[java]
Canvas canvas = getHolder().lockCanvas();画布的一些属性和方法以及一些应用
[java]
if (canvas != null) {
//----设置画布绘图无锯齿
canvas.setDrawFilter(pfd);
//----利用填充画布,刷屏
canvas.drawColor(Color.BLACK);
//----绘制文本
canvas.drawText("drawText", 10, 10, paint);
//----绘制像素点
canvas.drawPoint(10, 20, paint);
//----绘制多个像素点
canvas.drawPoints(new float[] { 10, 30, 30, 30 }, paint);
//----绘制直线
canvas.drawLine(10, 40, 50, 40, paint);
//----绘制多条直线
canvas.drawLines(new float[] { 10, 50, 50, 50, 70, 50, 110, 50 }, paint);
//----绘制矩形
canvas.drawRect(10, 60, 40, 100, paint);
//----绘制矩形2
Rect rect = new Rect(10, 110, 60, 130);
canvas.drawRect(rect, paint);
canvas.drawRect(rect, paint);
//----绘制圆角矩形
RectF rectF = new RectF(10, 140, 60, 170);
canvas.drawRoundRect(rectF, 20, 20, paint);
//----绘制圆形
canvas.drawCircle(20, 200, 20, paint);
//----绘制弧形
canvas.drawArc(new RectF(150, 20, 200, 70), 0, 230, true, paint);
//----绘制椭圆
canvas.drawOval(new RectF(150, 80, 180, 100), paint);
//----绘制指定路径图形
Path path = new Path();
//设置路径起点
path.moveTo(160, 150);
//路线1
path.lineTo(200, 150);
//路线2
path.lineTo(180, 200);
//路径结束
path.close();
canvas.drawPath(path, paint);
//----绘制指定路径图形
Path pathCircle = new Path();
//添加一个圆形的路径
pathCircle.addCircle(130, 260, 20, Path.Direction.CCW);
//----绘制带圆形的路径文本
canvas.drawTextOnPath("PathText", pathCircle, 10, 20, paint);
}
注:以上代码参考自《Android游戏编程之从零开始》得到自定义图片宽高的bitmap(参数一:上下文对象,参数二:资源ID,参数三:自定义宽度,参数四:自定义高度)
[java]
public static Bitmap loadBallView(Context context,int resId,int width,int height) {
Resources resources = context.getResources();
Drawable image = resources.getDrawable(resId);
Bitmap bitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
image.setBounds(0, 0, width,height);
image.draw(canvas);
return bitmap;
}
利用canvas绘制bitmap
[java]
Bitmap bitmap = loadBallView(context,R.drawable.image,100,100);
Paint paint = new Paint();
canvas.drawBitmap(bitmap, startX, startY, paint); //参数一:bitmap资源,参数二:起始X坐标,参数三:起始Y坐标,参数四:画笔paint对象