在 Android 画布上绘制位图的一部分
如何将位图的非矩形(例如椭圆形)部分传输到 Android 上的画布中?
考虑一下如何位图的矩形部分: canvas.DrawBitmap(src,src_rect,dest_rect,paint)
。遗憾的是,对于非矩形区域没有相应的方法。
有四种方法(也许您知道第五种?):
将要位图传输的矩形边界复制到中间位图中,然后将不想位图传输的像素设置为透明,然后绘制该位图< /p>
制作一个掩码位图 - 有多种方法可以使用 blit一个单独的掩码?
使用
BitmapShader
和 < a href="http://developer.android.com/reference/android/graphics/Canvas.html#drawArc%28android.graphics.RectF,%20float,%20float,%20boolean,%20android.graphics.Paint%29" rel="nofollow">drawArc()/drawCircle()
;但是,我不知道如何使矩阵正确对齐;您将如何初始化此操作的矩阵?使用非常非常复杂的剪切区域
其中,选项 3 是我最想使用的选项;但是,我不知道该怎么做;你可以吗?
How can you blit a non-rectangular (e.g. oval) part of a bitmap into a canvas on Android?
Consider how you'd blit a rectangular part of a bitmap:canvas.DrawBitmap(src,src_rect,dest_rect,paint)
. Sadly there is no corresponding methods for non-rectangular regions.
Four approaches present themselves (maybe you know a fifth?):
copy the rectangular bounds you want to blit into an intermediate bitmap, and go setting the pixels you don't want to blit to be transparent, then draw that bitmap
make a mask bitmap - there are ways to blit with a separate mask?
use a
BitmapShader
withdrawArc()/drawCircle()
; however, I can't work out how to get the matrix to be properly aligned; how would you initialize the matrix for this operation?use a very very complicated clipping region
Of these, option 3 is the one that I would most like to work; however, I cannot work out how to do so; can you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用选项#3,它可能是最简单的。另一种方法是在中间位图中绘制要剪辑的形状 (
ARGB8888
),然后使用DstIn
或DstOut
绘制原始位图> xfer 模式。You can use option number #3, it's probably the easiest. Another way is to draw the shape you want to clip with in an intermediate Bitmap (
ARGB8888
), then draw your original Bitmap using aDstIn
orDstOut
xfermode.