Android的Canvas.drawBitmap()方法,Paint参数的作用是什么

FingerPaint这个示例中,onDraw()里面调用了这个方法

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

但是好像改变mBitmapPaint的样式对最终画出的图像没有影响,官方里也说Paint参数可以为null,那么传这个参数到底有什么用?

官方文档节选:

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

Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix.

Parameters
bitmap The bitmap to be drawn
left The position of the left side of the bitmap being drawn
top The position of the top side of the bitmap being drawn
paint The paint used to draw the bitmap (may be null)

代码有点长,就改短了贴上来:

private Bitmap  mBitmap;
private Canvas  mCanvas;
private Path    mPath;
private Paint   mBitmapPaint;

mPaint = new Paint();
mPath = new Path();
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);

/*
 * 设置mPaint的样式
 * ......
 */

mBitmapPaint = new Paint(Paint.DITHER_FLAG);//这里不管怎么设置都不影响最终图像

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(0xFFAAAAAA);
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);
}
阅读 21.3k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
101 新手上路
子站问答
访问
宣传栏