创建到位图图像Android的xml布局

问题描述

Sharechat应用示例,如何实现它[1]:https://i.stack.imgur.com/iUcev.jpg 通过使用此方法,我只能获取图像元素并转换为图像,而不能获取框架布局,这必须在后台完成而又不会膨胀到主视图。

 public static Bitmap createBitmapSafely(int width,int height,Bitmap.Config config,int retryCount) {
        //width and height must be > 0
        if (width <= 0 || height <= 0) {
            return null;
        }
        try {
            return Bitmap.createBitmap(width,height,config);
        } catch (OutOfMemoryError e) {
            e.printstacktrace();
            if (retryCount > 0) {
                System.gc();
                return createBitmapSafely(width,config,retryCount - 1);
            }
            return null;
        }
    }
 public static Bitmap createBitmapFromView(View view,float scale) {
        if (view instanceof ImageView) {
            Drawable drawable = ((ImageView) view).getDrawable();
            if (drawable != null && drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            }
        }
        view.clearFocus();
        int viewHeight = 0;
        if (view instanceof ScrollView) {
            for (int i = 0; i < ((ScrollView) view).getChildCount(); i++) {
                viewHeight += ((ScrollView) view).getChildAt(i).getHeight();
            }
        } else if (view instanceof nestedScrollView) {
            for (int i = 0; i < ((nestedScrollView) view).getChildCount(); i++) {
                viewHeight += ((nestedScrollView) view).getChildAt(i).getHeight();
            }
        } else {
            viewHeight = view.getHeight();
        }

        Bitmap bitmap = createBitmapSafely((int) (view.getWidth() * scale),(int) (viewHeight * scale),Bitmap.Config.ARGB_8888,1);
        if (bitmap != null) {
            synchronized (CANVAS) {
                Canvas canvas = CANVAS;
                canvas.setBitmap(bitmap);
                canvas.save();
                // 防止 View 上面有些区域空白导致最终 Bitmap 上有些区域变黑
                canvas.drawColor(Color.WHITE);
                canvas.scale(scale,scale);
                view.draw(canvas);
                canvas.restore();
                canvas.setBitmap(null);
            }
        }
        return bitmap;
    }


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)