java - Android的Canvas.drawBitmap()方法,Paint参数的作用是什么
PHP中文网
PHP中文网 2017-04-17 11:52:25
[Java讨论组]

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);
}
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
怪我咯

自问自答 大部分情况下可以设置为null
stackoverflow上的回答

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号