android ImageView長圖不顯示

 

同一張長圖,在ImageView上 加載時,部分手機不顯示。提示如下異常:

W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (1080x18151, max=16384x16384)

原因:

當APP開啓硬件加速的時候,GPU對於openglRender 渲染有一個限制值,超過了這個限制值,就無法渲染,不同的手機會有不同的限制值;

 

解決:

 

獲取openglRender的限制值

    public static int getOpenglRenderLimitValue() {
        int maxsize;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            maxsize = getOpenglRenderLimitEqualAboveLollipop();
        } else {
            maxsize = getOpenglRenderLimitBelowLollipop();
        }
        return maxsize == 0 ? 4096 : maxsize;
    }

    private static int getOpenglRenderLimitBelowLollipop() {
        int[] maxSize = new int[1];
        GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
        return maxSize[0];
    }

    private static int getOpenglRenderLimitEqualAboveLollipop() {
        EGL10 egl = (EGL10) EGLContext.getEGL();
        EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
        int[] vers = new int[2];
        egl.eglInitialize(dpy, vers);
        int[] configAttr = {
                EGL10.EGL_COLOR_BUFFER_TYPE, EGL10.EGL_RGB_BUFFER,
                EGL10.EGL_LEVEL, 0,
                EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,
                EGL10.EGL_NONE
        };

        EGLConfig[] configs = new EGLConfig[1];
        int[] numConfig = new int[1];
        egl.eglChooseConfig(dpy, configAttr, configs, 1, numConfig);
        if (numConfig[0] == 0) {// TROUBLE! No config found.
        }

        EGLConfig config = configs[0];
        int[] surfAttr = {
                EGL10.EGL_WIDTH, 64,
                EGL10.EGL_HEIGHT, 64,
                EGL10.EGL_NONE
        };

        EGLSurface surf = egl.eglCreatePbufferSurface(dpy, config, surfAttr);

        final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;// missing in EGL10

        int[] ctxAttrib = {
                EGL_CONTEXT_CLIENT_VERSION, 1,
                EGL10.EGL_NONE
        };

        EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, ctxAttrib);
        egl.eglMakeCurrent(dpy, surf, surf, ctx);
        int[] maxSize = new int[1];
        GLES10.glGetIntegerv(GLES10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
        egl.eglMakeCurrent(dpy, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
                EGL10.EGL_NO_CONTEXT);
        egl.eglDestroySurface(dpy, surf);
        egl.eglDestroyContext(dpy, ctx);
        egl.eglTerminate(dpy);
        return maxSize[0];
    }

根據限制值,對圖片縮小,保證圖片不超過openglRender的限制:

RequestOptions bigRequestOptions;
public void loadBigImage(String url, ImageView imageView, int width, int height) {
        if (height > ImageUtils.getOpenglRenderLimitValue()) {
            width = width * ImageUtils.getOpenglRenderLimitValue() / height;
            height = ImageUtils.getOpenglRenderLimitValue();
            bigRequestOptions = new RequestOptions()
                    .placeholder(R.drawable.moren)
                    .error(R.drawable.moren).override(width, height);
            
        } else {
            bigRequestOptions = new RequestOptions()
                    .placeholder(R.drawable.moren)
                    .error(R.drawable.moren).override(width, height);
        }

            Glide.with(mContext).load(url).apply(bigRequestOptions).into(imageView);

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章