仅绘制可绘制/位图的一部分
我想知道是否可以在将位图加载到内存后仅绘制其一部分而不创建新的位图。我看到 Drawable 有一个 setBounds 方法,但我不确定它是否只绘制区域集或只是调整整个图像的大小。谢谢。
I was wondering if it is possible to draw only a portion of a bitmap after it is loaded into memory without creating a new Bitmap. I see Drawable has a setBounds method but im not sure if it only draws the area set or just resizes the entire image. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有一个要绘制的主画布,则可以使用 Canvas 类的drawBitmap 方法之一来绘制已加载位图的子集。
公共无效drawBitmap(位图位图,矩形src,矩形dst,Paint绘画)
Assuming you have a main canvas to draw to, you can use one of the drawBitmap methods of the Canvas class to draw a subset of the loaded bitmap.
public void drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)
我搜索了这个问题的答案,以便能够为图像缓存重用现有位图并避免内存碎片(以及随后的 OutOfMemoryError...),这是由分配在内存不同部分的大量位图引起的空间。
结果,我创建了简单的专用“BitmapSubsetDrawable”,它将自身公开为带下划线的位图的任意部分(该部分由 scrRect 确定)。
现在,我一次分配一组足够大的位图,然后重用它们( canvas.drawBitmap(sourceBitmap, 0 , 0, null); 在它们上...)来存储不同的位图。
该类的主要代码如下,参见BitmapSubsetDrawable.java 实际使用。
I searched for an answer to exactly this question in order to be able to reuse existing bitmaps for my image cache and to avoid memory fragmentation (and subsequent OutOfMemoryError...), which was caused by lots of bitmaps allocated in different parts of a memory space.
As a result I created simple specialized "BitmapSubsetDrawable", which exposes itself as an arbitrary part of the underlined Bitmap (the part is determined by scrRect).
Now I allocate a set of large enough Bitmaps once, and then reuse them ( canvas.drawBitmap(sourceBitmap, 0 , 0, null); on them...) for storage of different bitmaps.
The main code of the class is below, see BitmapSubsetDrawable.java for actual usage.