美文网首页
关于Android Bitmap的创建和初始化

关于Android Bitmap的创建和初始化

作者: 沉默的小象 | 来源:发表于2023-06-12 01:06 被阅读0次

Glide的图片加载显示的大致流程是,根据图片URL,建立网络链接,通过InputStream,生成Bitmap的对象,转为Drawable,传递给ImageView。所以在搞清楚Glide的图片加载显示流程之前,有必要先弄清楚Bitmap。

JDK中有没有Bitmap类?

没有。Bitmap类是Android特有的。

Bitmap的初始化有哪几种?

两类,很多种。

Bitmap类的createBitmap()方法

createBitmap方法重载了很多,下面是其中一个:

    public static Bitmap createBitmap(int width, int height, @NonNull Config config) {
        return createBitmap(width, height, config, true);
    }

    public static Bitmap createBitmap(int width, int height,
            @NonNull Config config, boolean hasAlpha) {
        return createBitmap(null, width, height, config, hasAlpha);
    }

    public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height,
            @NonNull Config config, boolean hasAlpha) {
        return createBitmap(display, width, height, config, hasAlpha,
                ColorSpace.get(ColorSpace.Named.SRGB));
    }

    public static Bitmap createBitmap(@Nullable DisplayMetrics display, int width, int height,
            @NonNull Config config, boolean hasAlpha, @NonNull ColorSpace colorSpace) {
        if (width <= 0 || height <= 0) {
            throw new IllegalArgumentException("width and height must be > 0");
        }
        if (config == Config.HARDWARE) {
            throw new IllegalArgumentException("can't create mutable bitmap with Config.HARDWARE");
        }
        if (colorSpace == null && config != Config.ALPHA_8) {
            throw new IllegalArgumentException("can't create bitmap without a color space");
        }

        Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true,
                colorSpace == null ? 0 : colorSpace.getNativeInstance());

        if (display != null) {
            bm.mDensity = display.densityDpi;
        }
        bm.setHasAlpha(hasAlpha);
        if ((config == Config.ARGB_8888 || config == Config.RGBA_F16) && !hasAlpha) {
            nativeErase(bm.mNativePtr, 0xff000000);
        }
        // No need to initialize the bitmap to zeroes with other configs;
        // it is backed by a VM byte array which is by definition preinitialized
        // to all zeroes.
        return bm;
    }

BitmapFactory类的decodeXXX()方法

    public static Bitmap decodeByteArray(byte[] data, int offset, int length) {
        return decodeByteArray(data, offset, length, null);
    }

    public static Bitmap decodeFile(String pathName) {
        return decodeFile(pathName, null);
    }

    public static Bitmap decodeFileDescriptor(FileDescriptor fd) {
        return decodeFileDescriptor(fd, null, null);
    }

    public static Bitmap decodeResource(Resources res, int id) {
        return decodeResource(res, id, null);
    }

    public static Bitmap decodeStream(InputStream is) {
        return decodeStream(is, null, null);
    }

Android:安卓学习笔记之Bitmap的简单理解和使用

生成Bitmap的方法为什么叫解码decode?

编码是指把Bitmap对象生成流。
解码是指把流转为Bitmap对象。

相关文章

网友评论

      本文标题:关于Android Bitmap的创建和初始化

      本文链接:https://www.haomeiwen.com/subject/dovfydtx.html