列于Android的textview.setbackground资源存储异常 [英] Out of memory exception in textview.setbackground resource in android
问题描述
我想一个图像放置在我TextView的背景。我想要的形象得到由pressing按钮改变。在第一个图像被加载,但是当我尝试下一次它说的OutOfMemoryError。
I am trying to place an image as background in my TextView. I want the image to get changed by pressing a button. At the first image gets loaded but when I try for next time it says OutOfMemoryError.
我的code:
int [] Quo = {R.drawable.wallpaper10,R.drawable.wallpaper2,R.drawable.wallpaper3,R.drawable.wallpaper4};
txtView.setBackgroundResource(Quo[j] );
它完美,如果我给
It works perfectly if i give
txtView.setBackgroundResource(Quo[0]);
我是新android开发,请帮助我解决问题。
I am new to android development please help me resolve issue.
推荐答案
图像内存的使用是基于多个方面:
image memory usage is based on multiple things:
- 的图像的分辨率。
- 映像文件的位置(该文件夹绘制的密度预选赛)。
- 您使用的位图格式。
- 图像是否是9片图像。
相比于其上屏幕密度的应用程序正在运行的
因此,举例来说,如果你有一个100×100的图像,该文件是在绘-MDPI但它运行的xhdpi设备上,和位图格式是默认的,这不是一个9片图像,位图将需要:
so , for example, if you have a 100x100 image, and the file is in "drawable-mdpi" yet it runs on an xhdpi device , and the bitmap format is the default one, and it's not a 9-patch image, the bitmap would take:
100 * 100 * 4 * 4 = 160000字节。
100*100 * 4 *4 = 160,000 bytes.
100 * 100是因为,宽度和高度的像素数的
the 100*100 is because of the number of pixels for width and height.
的前4个是因为上的默认位图格式中,每个像素需要4个字节。
the first 4 is because on the default bitmap format, each pixel takes 4 bytes.
第二个4是因为密度为宽度和高度(2 * 2 = 4)。
the second 4 is because the density is 2 times larger in both width and height (2*2=4).
因此,如果这是唯一的code告诉你,我想你使用,因为您的图片太多内存。
so, if this is the only code you show, i think you use too much memory because of your images.
一件事,你可以做的是下采样他们。
one thing that you could do is to downsample them.
这篇关于列于Android的textview.setbackground资源存储异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!