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

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

问题描述

我是从像这样的资源加载位图:

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

这避免此问题但它有时会导致OutOfMemoryExceptions,知道达到我想要的任何更好的办法?

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

推荐答案

有几种方法来创建它的一个副本。这个线程可以帮助你:<一href="http://www.anddev.org/how_to_modify_the_image_file-t513.html">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天全站免登陆