简体   繁体   English

参数'Paint'在Android的Canvas.drawBitmap()方法中的作用

[英]Effect of parameter ‘Paint’ in Android‘s Canvas.drawBitmap() method

I was trying to read sample FingerPaint in Android SDK where the method canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint) called within onDraw() . 我正在尝试阅读Android SDK中的示例FingerPaint,其中在onDraw()调用了canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint) onDraw()

I was confused by the parameter mBitmapPaint . 我对参数mBitmapPaint感到困惑。 It seemed to have no effect when I changed the settings of mBitmapPaint . 当我更改mBitmapPaint的设置时,它似乎没有任何作用。 The style of the bitmap painted on the screen only depends on the settings of mPaint defined somewhere else. 屏幕上绘制的位图样式仅取决于在其他mPaint定义的mPaint设置。 The Google's Android Reference says that the parameter paint may be null, does it mean paint here is useless? Google的Android参考说参数paint可以为null,这是否意味着这里的paint没有用? Why put a paint parameter in this method? 为什么要在此方法中放置绘画参数?

It's declared as: 声明为:

public void drawBitmap(Bitmap bitmap,float left,float top,Paint paint) 公共无效drawBitmap(位图位图,向左浮动,顶部浮动,绘画颜料)

Draw the specified bitmap, with its top/left corner at (x,y), using the specified paint, transformed by the current matrix. 使用指定的绘制绘制指定的位图,其左上角在(x,y)处,并通过当前矩阵进行变换。

Parameters 参数

  • bitmap The bitmap to be drawn 图要绘制的位图
  • left The position of the left side of the bitmap being drawn left绘制的位图左侧的位置
  • top The position of the top side of the bitmap being drawn top绘制的位图顶侧的位置
  • paint The paint used to draw the bitmap (may be null) paint用于绘制位图的Paint(可以为null)

Here is part of the code: 这是代码的一部分:

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);

/*
 * Set the styles of mPaint here
 * ......
 */

// Settings of mBitmapPaint seem to make no difference
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);
}

The paint for a Bitmap is pretty much useless, I think the only parameter of the paint object that applies for a Bitmap is the AntiAliasing mode wich is turned on by default. 位图的绘制几乎没有用,我认为适用于位图的绘制对象的唯一参数是默认打开的“抗锯齿”模式。 I think you can also use the paint for a color filter such as black and white, but for most cases you can just use null as well. 我认为您也可以将颜料用于黑白之类的彩色滤光片,但在大多数情况下,也可以使用null

It is mainly used to set antialiasing and bilinear sampling: 它主要用于设置抗混叠和双线性采样:

Actually, Canvas.drawBitmap(Bitmap, Rect, Rect, Paint) works properly with paint, BUT only if Bitmap is in Bitmap.Config.ALPHA_8 format. 实际上,仅当Bitmap的格式为Bitmap.Config.ALPHA_8Canvas.drawBitmap(Bitmap, Rect, Rect, Paint)才能与paint, BUT一起正常使用。 It can be easily converted on the go with Bitmap.copy(Bitmap.Config.ALPHA_8, true/false) . 可以使用Bitmap.copy(Bitmap.Config.ALPHA_8, true/false)轻松进行转换。 It's especially useful with Paint, that has a BitmapShader applied. 这对于应用了BitmapShader的Paint尤其有用。

It's not mentioned enough, so I thought I'll add an answer to this old question. 提及的还不够,所以我想我将为这个旧问题添加一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM