为什么带有inPreferredConfig = RGB565的BitmapFactory.decodeResource会扩展我的图片x2?

时间:2014-03-11 14:06:48

标签: java android

我有一张资源图片。如果我在没有实际解码的情况下读取它的尺寸,一切都很好(即使要求RGB565)。如果我解码为RGB565颜色格式,Android API会为我提供太宽和太高的尺寸。

这是Android中的错误吗?

private static Bitmap decodeBitmapAsRGB565( Resources res, int resId ) {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    int origW, origH;

    opt.inJustDecodeBounds= true;       // don't decode contents, just get the dimensions
    {
        // Get original picture's dimensions
        //
        BitmapFactory.decodeResource( res, resId, opt );
        Log.v("", String.format("*** Original: %d x %d", opt.outWidth, opt.outHeight));     // 1532x2048

        origW= opt.outWidth;
        origH= opt.outHeight;

        opt.inPreferredConfig= Bitmap.Config.RGB_565;

        BitmapFactory.decodeResource( res, resId, opt );
        Log.v("", String.format("*** RGB565: %d x %d", opt.outWidth, opt.outHeight));       // 1532x2048
    }
    opt.inJustDecodeBounds= false;

    opt.inPreferredConfig= Bitmap.Config.RGB_565;       // needed for 'EFFECT_REDEYES'

    final Bitmap bmp= BitmapFactory.decodeResource( res, resId, opt );

    // Check that we got the size of the original
    //
    int w= bmp.getWidth();
    int h= bmp.getHeight();

    if ((w!=origW) || (h!=origH)) {
        String s; Log.wtf("", s= String.format("Bitmap dimensions screwed: (%d,%d) vs. (%d,%d)", w,h, origW,origH));
        throw new RuntimeException(s);
    }

    return bmp;
}

1 个答案:

答案 0 :(得分:0)

Resources会根据设备的屏幕密度进行缩放。如果要避免扩展,可以将资源放在drawable-nodpi目录中。