OutOfMemoryError而在游戏中有许多小图片 [英] OutOfMemoryError in game with many small images

查看:157
本文介绍了OutOfMemoryError而在游戏中有许多小图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种纸牌游戏为Android。我有一个OutOfMemoryError异常,当我尝试加载一些图像。我已阅读关于Android的这一普遍和常见的问题,很多答案,但所有的人都处理当图像加载是非常大的。我管理的屏幕旋转由我自己,所以我不重新启动旋转一个新的活动。 我已阅读此问题:

I am developing a card game for android. I have a OutOfMemoryError when i try to load some images. I have read many answers about this common and frequent problem in android, BUT all of them deal when the image to load is very big. I manage screen rotation by myself so I dont restart a new activity on rotation. I have already read this questions:

<一个href="http://stackoverflow.com/questions/5321579/out-of-memory-exception-due-to-large-bitmap-size">Out内存异常,由于大的位图尺寸

<一个href="http://stackoverflow.com/questions/3238388/android-out-of-memory-exception-in-gallery">Android:在库内存异常

<一个href="http://stackoverflow.com/questions/3724082/android-handling-out-of-memory-exception-on-image-processing">Android处理内存溢出异常图像处理

这是我不明白的一件事是,如果我的所有图像(PNG文件)的资源文件夹和的大小约为800 KB。但在logcat中我注意到,在创建游戏(装卡图像)的大小会迅速超过20 MB。 我用:这code载入图像

One thing that I don't understand that, is that if sum the size of all my images (png files) in res folder is about 800 kb. But in Logcat I notice that the size on creating the game(loading card images) goes rapidly over 20 mb. I use: this code to load images

Resources r = this.getContext().getResources();
CardView.loadCardImages(CardView.EAlone.CARDS, 1027, 615, r.getDrawable(R.drawable.cards));
CardView.loadCardImages(CardView.EAlone.GREEN, mCardWidth, mCardHeight, r.getDrawable(R.drawable.green));
CardView.loadCardImages(CardView.EAlone.RED, mCardWidth, mCardHeight, r.getDrawable(R.drawable.red));
CardView.loadCardImages(CardView.EAlone.WHITE, mCardWidth, mCardHeight, r.getDrawable(R.drawable.white));

CardView.LoadCardImages:

CardView.LoadCardImages:

public static void loadCardImages(EAlone mode, int widthPixels, int heightPixels, Drawable tile) {

    Canvas canvas;
    switch (mode)
    {
    case GREEN:
        mAloneGreen = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mAloneGreen);
        break;
    case RED:
        mAloneRed = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mAloneRed);
        break;
    case WHITE:
        mAloneWhite = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mAloneWhite);
        break;
    default:
        mcardImages = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(mcardImages);
    }
    tile.setBounds(0, 0, widthPixels, heightPixels);
    tile.draw(canvas);
    tile.setCallback(null);
}

mCardimages包含所有的卡面。然后我使用这个文件通过裁剪所需的卡从文件创建单独的卡。 在此之后的负载我在父视图装载有胶片一些其他的图像。

mCardimages contains all the card faces. Then I use this file to create the single cards by cropping the needed card from the file. After this loadings I load some other images that have transparencies in my parent view.

dealerSign = new ImageView(getContext());
dealerSign.setImageResource(R.drawable.dealer);
dealerSign.setAdjustViewBounds(true);
dealerSign.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

我得到的错误,在setImageResource行。

I get the error at setImageResource line.

04-09 22:04:37.226: E/AndroidRuntime(722): FATAL EXCEPTION: main
04-09 22:04:37.226: E/AndroidRuntime(722): java.lang.OutOfMemoryError
04-09 22:04:37.226: E/AndroidRuntime(722):  at    android.graphics.Bitmap.nativeCreate(Native Method)
04-09 22:04:37.226: E/AndroidRuntime(722):  at    android.graphics.Bitmap.createBitmap(Bitmap.java:605)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:767)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.content.res.Resources.loadDrawable(Resources.java:1937)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.content.res.Resources.getDrawable(Resources.java:664)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.widget.ImageView.resolveUri(ImageView.java:537)
04-09 22:04:37.226: E/AndroidRuntime(722):  at android.widget.ImageView.setImageResource(ImageView.java:310)
04-09 22:04:37.226: E/AndroidRuntime(722):  at game.spathi.GameView.init(GameView.java:261)
04-09 22:04:37.226: E/AndroidRuntime(722):  at game.spathi.Game.onCreate(Game.java:92)
...

我在做什么错在这里?

What am I doing wrong here?

感谢您!

推荐答案

试试吧 -

Bitmap yourBitmap;      
ByteArrayOutputStream baos = new ByteArrayOutputStream();


offerImage.compress(Bitmap.CompressFormat.PNG, 0, baos);
byte[] img = shrinkBitmap(baos.toByteArray());

//convert this byte image to bitmap and load it...

private byte[] shrinkBitmap(byte[] data){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2; //try to decrease decoded image
    options.inPurgeable = true; //purgeable to disk
    Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(data, 0, data.length, options), 100, 100, true);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 20, buf);

    return(buf.toByteArray());
}

这篇关于OutOfMemoryError而在游戏中有许多小图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆