Android开发Android开发经验谈

格式Bitmap.Config中ARGB_4444 无效

2023-09-07  本文已影响0人  smart_dev

参考文章 https://cloud.tencent.com/developer/article/1946218

Demo验证,去加载565/4444/8888 发现4444与8888 内存count一致

Bitmap.Options 中标记4444已经过时,且最终会用8888替代这种格式

原来在KITKAT,即Android 19之后,这个参数就彻底无效了,只用这个参数会被默认替换为ARGB_8888,所以内存大小没有变化。

    /**
     * Each pixel is stored on 2 bytes. The three RGB color channels
     * and the alpha channel (translucency) are stored with a 4 bits
     * precision (16 possible values.)
     *
     * This configuration is mostly useful if the application needs
     * to store translucency information but also needs to save
     * memory.
     *
     * It is recommended to use {@link #ARGB_8888} instead of this
     * configuration.
     *
     * Note: as of {@link android.os.Build.VERSION_CODES#KITKAT},
     * any bitmap created with this configuration will be created
     * using {@link #ARGB_8888} instead.
     *
     * @deprecated Because of the poor quality of this configuration,
     *             it is advised to use {@link #ARGB_8888} instead.
     */
    @Deprecated
    ARGB_4444   (4),

那native层是如何强制使用8888的呢?

static jobject Bitmap_creator(JNIEnv* env, jobject, jintArray jColors,
                              jint offset, jint stride, jint width, jint height,
                              jint configHandle, jboolean isMutable,
                              jlong colorSpacePtr) {
    SkColorType colorType = GraphicsJNI::legacyBitmapConfigToColorType(configHandle);
    if (NULL != jColors) {
        size_t n = env->GetArrayLength(jColors);
        if (n < SkAbs32(stride) * (size_t)height) {
            doThrowAIOOBE(env);
            return NULL;
        }
    }
   // 注释1
    // ARGB_4444 is a deprecated format, convert automatically to 8888
    if (colorType == kARGB_4444_SkColorType) {
        colorType = kN32_SkColorType;
    }


    sk_sp<SkColorSpace> colorSpace;
    if (colorType == kAlpha_8_SkColorType) {
        colorSpace = nullptr;
    } else {
        colorSpace = GraphicsJNI::getNativeColorSpace(colorSpacePtr);
    }

    SkBitmap bitmap;
    bitmap.setInfo(SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType,
                colorSpace));

    sk_sp<Bitmap> nativeBitmap = Bitmap::allocateHeapBitmap(&bitmap);
    if (!nativeBitmap) {
        ALOGE("OOM allocating Bitmap with dimensions %i x %i", width, height);
        doThrowOOME(env);
        return NULL;
    }

    if (jColors != NULL) {
        GraphicsJNI::SetPixels(env, jColors, offset, stride, 0, 0, width, height, &bitmap);
    }

    return createBitmap(env, nativeBitmap.release(), getPremulBitmapCreateFlags(isMutable));
}   

Glide 图片格式无4444

public enum DecodeFormat {
  PREFER_ARGB_8888,
  PREFER_RGB_565;
  /** The default value for DecodeFormat. */
  public static final DecodeFormat DEFAULT = PREFER_ARGB_8888;
}
上一篇下一篇

猜你喜欢

热点阅读