Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:加载图像时出现问题_Android_Performance_Bitmap_Drawable - Fatal编程技术网

Android:加载图像时出现问题

Android:加载图像时出现问题,android,performance,bitmap,drawable,Android,Performance,Bitmap,Drawable,我对decodeSampleBitmap代码和图像的最终大小有很大的问题。我在nodpi drawables文件夹中有我的drawables,用于不将不同文件夹中的相同drawables与不同的大小相乘,并在使用decodeSampleBitmap后调整大小 我有以下用于加载位图的代码: public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeig

我对decodeSampleBitmap代码和图像的最终大小有很大的问题。我在nodpi drawables文件夹中有我的drawables,用于不将不同文件夹中的相同drawables与不同的大小相乘,并在使用decodeSampleBitmap后调整大小

我有以下用于加载位图的代码:

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}
我可以从文档中获得的代码。好的,我想这是为了更有效地加载图像,但我有内存问题。我试着解释这个问题

1-我想加载一个png,2037x1440像素,12.6kb

2-我应用我的代码并加载一个2037x1440像素和11,73MB的png。我不明白我做错了什么。为什么安卓系统会增加1000倍于我的抽屉大小

10-12 12:06:07.842 12405-12822/com.viridesoft.kiseichuu V/control4: width=2037 height=1440 size=11733120
编辑:如果我使用BitmapFactory.decodeResource加载可绘制文件,则大小相同,为1173120

旧问题应该改变。我知道我不能再缩小我的图像大小了,我还是继续处理同样的问题

我加载了大量图像以创建dinamical背景,并对所有角色(我的玩家和僵尸)充电,总共直到屏幕大小为25%px 10%px的111个位图和100%屏幕大小的30个位图。我将所有这些位图加载到一个加载程序活动中,并保存起来以备以后在游戏中使用。我有一个质量很好的图像和一个可绘制的nodpi文件夹,其中有很多图像,以获得我所看到的最佳android分辨率。我的游戏在一些设备(bq aquaris、nexus 5等)中运行流畅,但在其他设备(一些平板电脑、一些中端设备等)中持续清理内存并运行缓慢

新问题是:
我如何才能有效地为一个流畅的游戏体验加载大量图像?

11,73MB到底是多少?你到底在哪里决定了最终的解决方案?您要为样本大小计算哪个值?
reqWidth,reqHeight
您需要哪些值?我为样本1280x720传递这些值,但您可以看到位图的宽度和高度比我的代码返回的宽度和高度高,1173120字节,我需要屏幕宽度和高度,您应该给出准确的数字。告诉我所有的价值观。回答我所有的问题!我仍然不知道这11,73MB是从哪里来的。
如果我用BitmapFactory.decodeResource加载可绘图文件,大小是一样的???您使用的是BitmapFactory.decodeResource()。没有别的了。那你是什么意思?1173MB到底是多少?你到底在哪里决定了最终的解决方案?您要为样本大小计算哪个值?
reqWidth,reqHeight
您需要哪些值?我为样本1280x720传递这些值,但您可以看到位图的宽度和高度比我的代码返回的宽度和高度高,1173120字节,我需要屏幕宽度和高度,您应该给出准确的数字。告诉我所有的价值观。回答我所有的问题!我仍然不知道这11,73MB是从哪里来的。
如果我用BitmapFactory.decodeResource加载可绘图文件,大小是一样的???您使用的是BitmapFactory.decodeResource()。没有别的了。那你是什么意思?