所以我得到了这个背景图片为我的活动。它是一个480x800 PNG。 它有一个渐变,所以有绑扎的危险,这就是为什么我99%的不透明强迫最好的颜色模式。背景图像内存大小
在我的设备上,甚至在宏达魔法这是没有问题的。
但是,在默认1.6模拟器上,出现内存不足错误。该怎么办? 背景被设置在代码:
bgView.setImageResource(R.drawable.baby_pink_solid);
最大VM堆设置为192和设备RAM的大小为256似乎不是一个解决办法。
所以我得到了这个背景图片为我的活动。它是一个480x800 PNG。 它有一个渐变,所以有绑扎的危险,这就是为什么我99%的不透明强迫最好的颜色模式。背景图像内存大小
在我的设备上,甚至在宏达魔法这是没有问题的。
但是,在默认1.6模拟器上,出现内存不足错误。该怎么办? 背景被设置在代码:
bgView.setImageResource(R.drawable.baby_pink_solid);
最大VM堆设置为192和设备RAM的大小为256似乎不是一个解决办法。
尝试访问代码中的位图,然后通过setImageBitmap()
设置它。如果您在代码中解码位图时得到OOM,那么这就是为什么您从setImageResource()
获得它。
我发现Bitmaps在Android上处理是一件棘手的事情,使用它们时必须小心!
也检查@Sadeshkumar Periyasamy的答案,这对解码位图或更大尺寸的设备没有像今天设备那么强大的功能很有用。
试试这个代码是按比例的任何位图:
public class ImageScale
{
/**
* Decodes the path of the image to Bitmap Image.
* @param imagePath : path of the image.
* @return Bitmap image.
*/
public Bitmap decodeImage(String imagePath)
{
Bitmap bitmap=null;
try
{
File file=new File(imagePath);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(file),null,o);
final int REQUIRED_SIZE=200;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true)
{
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=scale;
bitmap=BitmapFactory.decodeStream(new FileInputStream(file), null, options);
}
catch(Exception e)
{
bitmap = null;
}
return bitmap;
}
/**
* Resizes the given Bitmap to Given size.
* @param bm : Bitmap to resize.
* @param newHeight : Height to resize.
* @param newWidth : Width to resize.
* @return Resized Bitmap.
*/
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
Bitmap resizedBitmap = null;
try
{
if(bm!=null)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
// resizedBitmap = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
}
}
catch(Exception e)
{
resizedBitmap = null;
}
return resizedBitmap;
}
}
看起来像在背景中的imageview,而不是一个图像作为背景。你为什么要使用imageview? – njzk2 2012-03-19 13:11:33