将资源加载到可变位图 [英] Loading a resource to a mutable bitmap

查看:30
本文介绍了将资源加载到可变位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从这样的资源加载位图:

I am loading a bitmap from a resource like so:

 Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image);

我想要做的是在位图在我的 draw 方法中被绘制到主画布之前对其进行一些更改(因为在我的主循环中重复大量绘制似乎是浪费时间它不会改变).我正在对位图进行以下更改:

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

哪个避免了这个问题,但它有时会导致 OutOfMemoryExceptions,你知道实现我想要的更好的方法吗?

Which avoid the problem however it sometimes causes OutOfMemoryExceptions, do know any better ways of achieving what I want?

推荐答案

有几种方法可以创建它的副本.此线程可能对您有所帮助:http://www.anddev.org/how_to_modify_the_image_file-t513.html

There are several ways to create a copy of it. This thread might help you: http://www.anddev.org/how_to_modify_the_image_file-t513.html

这篇关于将资源加载到可变位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆