Android:位图大小超出 VM 预算。将图像加载到画布中
我试图在像我这样的其他问题中找到解决方案,但找不到任何东西。
我正在将一堆图像加载到画布中。但是,我收到 java.lang.OutOfMemoryError: 位图大小超出 VM 预算。我正在使用真实的手机进行测试(Nexus One)。应用程序崩溃。它在我的三星振动上运行良好。
这是我的代码:
CustomDrawableView.java
package com.images.testimage;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
public class CustomDrawableView extends View {
private Drawable canada;
private Drawable usa;
private Drawable mexico;
public CustomDrawableView(Context context) {
super(context);
Resources res = context.getResources();
canada = res.getDrawable(R.drawable.canada);
canada.setBounds(0, 0, 0 + canada.getMinimumWidth(), 0 + canada.getMinimumHeight());
usa = res.getDrawable(R.drawable.usa);
usa.setBounds(0,0,usa.getMinimumWidth(),
usa.getMinimumHeight());
mexico = res.getDrawable(R.drawable.mexico);
mexico.setBounds(0,0,mexico.getMinimumWidth(),
mexico.getMinimumHeight());
}
protected void onDraw(Canvas canvas) {
canada.draw(canvas);
usa.draw(canvas);
mexico.draw(canvas);
}
}
加拿大、美国和墨西哥是 png。每个是 5kb。
主要代码如下:
TestImageActivity.java
package com.images.testimage;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class TestImagesActivity extends Activity {
/** Called when the activity is first created. */
CustomDrawableView mCustomDrawableView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCustomDrawableView = new CustomDrawableView(this);
setContentView(mCustomDrawableView);
setRequestedOrientation(0);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
}
当我只加载美国和加拿大等 2 个图像时,它可以在我的 Nexus One 上运行。不是 3。我应该加载 20 个以上的国家。请帮忙?
这是一个示例 canada.png 图像...它是白色的..如果突出显示并拖动它,您将看到该图像...
I tried to find solutions in other questions like mine but couldn't find anything.
I am loading a bunch of images into a canvas. However, I am getting a java.lang.OutOfMemoryError: bitmap size exceeds VM budget. I am using a real phone for testing (Nexus One). The app crashes. It works fine on my Samsung Vibrate.
Here's my code:
CustomDrawableView.java
package com.images.testimage;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
public class CustomDrawableView extends View {
private Drawable canada;
private Drawable usa;
private Drawable mexico;
public CustomDrawableView(Context context) {
super(context);
Resources res = context.getResources();
canada = res.getDrawable(R.drawable.canada);
canada.setBounds(0, 0, 0 + canada.getMinimumWidth(), 0 + canada.getMinimumHeight());
usa = res.getDrawable(R.drawable.usa);
usa.setBounds(0,0,usa.getMinimumWidth(),
usa.getMinimumHeight());
mexico = res.getDrawable(R.drawable.mexico);
mexico.setBounds(0,0,mexico.getMinimumWidth(),
mexico.getMinimumHeight());
}
protected void onDraw(Canvas canvas) {
canada.draw(canvas);
usa.draw(canvas);
mexico.draw(canvas);
}
}
canada, usa and mexico are pngs. Each is 5kb.
Here's the main code:
TestImageActivity.java
package com.images.testimage;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class TestImagesActivity extends Activity {
/** Called when the activity is first created. */
CustomDrawableView mCustomDrawableView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCustomDrawableView = new CustomDrawableView(this);
setContentView(mCustomDrawableView);
setRequestedOrientation(0);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
}
It works on my Nexus One when I only load 2 images like usa and canada. Not with 3. I am suppose to load like 20 more countries like those. Help please?
Here's a sample canada.png image... Its white coloured.. if you highlight and drag it, you will see the image...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能的解决方案之一是降低图像分辨率。不幸的是没有其他好的解决办法。
One of the possible solution is reduce image resolution. Unfortunately there is no other good work around.