将资源加载到可变位图
我正在从这样的资源加载位图:
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);
我想要做的是在将位图绘制到我的 draw 方法中的主画布之前对位图进行一些更改(因为重复似乎很浪费当它不会改变时,我的主循环中有很多绘图)。我正在对位图进行以下更改:
Canvas c = new Canvas(mBackground);
c.drawARGB(...); // etc
所以很自然地我会遇到异常
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
因此,为了避免我制作了位图的副本,以便它是可变的
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image).copy(Bitmap.Config.ARGB_8888, true);
这避免了问题,但有时会导致 OutOfMemoryExceptions,确实知道任何更好的方法实现我想要的?
I am loading a bitmap from a resource like so:
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);
What I want to do is make some changes to the bitmap before It gets drawn to the main canvas in my draw method (As it would seem wasteful to repeat lots of drawing in my main loop when it isn't going to change). I am making the changes to the bitmap with the following:
Canvas c = new Canvas(mBackground);
c.drawARGB(...); // etc
So naturally I get an exception
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
So to avoid that I made a copy of the bitmap so that it is mutable
Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image).copy(Bitmap.Config.ARGB_8888, true);
Which avoid the problem however it sometimes causes OutOfMemoryExceptions, do know any better ways of achieving what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用decodeResource(Resources res, int id, BitmapFactory.Options opts)并在选项中指定inMutable。
http://developer.android.com/reference/android/graphics/BitmapFactory.html
Use
decodeResource(Resources res, int id, BitmapFactory.Options opts)
and specifyinMutable
in the options.http://developer.android.com/reference/android/graphics/BitmapFactory.html
你最好使用RapidDecoder。
适用于 API 级别 8。
You'd better use RapidDecoder.
Works for API level 8.
代替你的:
使用:
Instad of yours:
Use: