我在同一活动中启动了2个AnimationDrawables,但出现了内存错误。这是相当令人惊讶的,因为我减少了图像的大小(宽度=屏幕的宽度),我不能再减少它了,因为质量会降低。例如,如果我使用width = width of the screen/2,它可以工作,但质量很差
在logcat中,它被写成
02-19 10:56:44.769: E/dalvikvm-heap(3118): 383916-byte external allocation too large for this process.
02-19 10:56:44.809: E/GraphicsJNI(3118): VM won't let us allocate 383916 bytes
02-19 10:56:44.809: W/dalvikvm(3118): threadid=1: thread exiting with uncaught exception (group=0x40018560)
02-19 10:56:44.819: E/AndroidRuntime(3118): FATAL EXCEPTION: main
02-19 10:56:44.819: E/AndroidRuntime(3118): java.lang.OutOfMemoryError: bitmap size exceeds VM budget我不认为我有一个内存泄漏,但检查这篇文章http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html我试图检测一个潜在的内存泄漏,但对我来说似乎没有。
我真的需要一些帮助,因为这不是我在这个项目中的第一个内存错误,我总是努力处理它。
下面是我的代码:
private void startAnimation(AnimationDrawable animation1,AnimationDrawable animation2){
int drawables[] = new int[] {R.drawable.fire1,R.drawable.fire2,R.drawable.fire3,R.drawable.fire4,R.drawable.fire5,R.drawable.fire6,R.drawable.fire7,R.drawable.fire8,R.drawable.fire9,R.drawable.fire10down2,R.drawable.fire11down2,R.drawable.fire12down2,R.drawable.fire13down2,R.drawable.fire14down2,R.drawable.fire15down2,R.drawable.fire16down2};
int drawables2[] = new int[] {R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire14down2,R.drawable.fire10up,R.drawable.fire11up,R.drawable.fire12up,R.drawable.fire13up,R.drawable.fire14up,R.drawable.fire15up,R.drawable.fire16up};
bitmapDrawable = new BitmapDrawable[drawables.length];
bitmapDrawable2 = new BitmapDrawable[drawables2.length];
for (int i=0;i<drawables.length;i++) {
bitmapDrawable[i]=new BitmapDrawable(decodeSampledBitmapFromResource(getResources(), drawables[i],width));
Log.w("BITMAP","i="+i);
bitmapDrawable2[i]=new BitmapDrawable(decodeSampledBitmapFromResource(getResources(), drawables2[i],width));
animation1.addFrame(bitmapDrawable[i],50);
animation2.addFrame(bitmapDrawable2[i],50);
}
animation1.setOneShot(true);
animation2.setOneShot(true);
image2.setLayoutParams(params);
image2.setImageDrawable(animation1);
image2.post(new Starter(animation1));
image3.setLayoutParams(params);
image3.setImageDrawable(animation2);
image3.post(new Starter(animation2));
}
class Starter implements Runnable {
AnimationDrawable animation = new AnimationDrawable();
public Starter(AnimationDrawable animation) {
this.animation = animation;
}
public void run() {
animation.start();
}
}如何减轻位图的权重(如果有必要?该问题可能在某些地方else_,并保持图像的质量。我看不出有什么问题,我只用了30张图片,其他的应用程序用的更多!
发布于 2013-02-19 19:56:14
你可以从你的文件中创建ScaledBitmap来减小EX的大小:-
Bitmap scaledBitmap = Bitmap.createScaledBitmap(oldBitmap, 600, 600,
false);这将使其大小减小到600x600,并减小其大小。你可以改变你喜欢的像素。
发布于 2013-02-19 21:14:53
在这里,您将重新调整质量大小。
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
bmpFactoryOptions.inSampleSize = 2;
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);在这里,我使用这个方法从sdcard中获取图像。然后显示出来。
https://stackoverflow.com/questions/14955851
复制相似问题