Converting a view to Bitmap without displaying it in Android?

前端 未结 9 1725
渐次进展
渐次进展 2020-11-22 07:36

I will try to explain what exactly I need to do.

I have 3 separate screens say A,B,C. There is another screen called say HomeScreen where all the 3 screens bitmap sh

相关标签:
9条回答
  • 2020-11-22 08:14

    There is a great Kotlin extension function in Android KTX: View.drawToBitmap(Bitmap.Config)

    0 讨论(0)
  • 2020-11-22 08:15

    Layout or view to bitmap:

     private Bitmap createBitmapFromLayout(View tv) {      
        int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        tv.measure(spec, spec);
        tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
        Bitmap b = Bitmap.createBitmap(tv.getMeasuredWidth(), tv.getMeasuredWidth(),
                Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        c.translate((-tv.getScrollX()), (-tv.getScrollY()));
        tv.draw(c);
        return b;
    }
    

    Calling Method:

    Bitmap src = createBitmapFromLayout(View.inflate(this, R.layout.sample, null)/* or pass your view object*/);
    
    0 讨论(0)
  • 2020-11-22 08:17

    I think this is a bit better :

    /**
     * draws the view's content to a bitmap. code initially based on :
     * http://nadavfima.com/android-snippet-inflate-a-layout-draw-to-a-bitmap/
     */
    @Nullable
    public static Bitmap drawToBitmap(final View viewToDrawFrom, int width, int height) {
        boolean wasDrawingCacheEnabled = viewToDrawFrom.isDrawingCacheEnabled();
        if (!wasDrawingCacheEnabled)
            viewToDrawFrom.setDrawingCacheEnabled(true);
        if (width <= 0 || height <= 0) {
            if (viewToDrawFrom.getWidth() <= 0 || viewToDrawFrom.getHeight() <= 0) {
                viewToDrawFrom.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                width = viewToDrawFrom.getMeasuredWidth();
                height = viewToDrawFrom.getMeasuredHeight();
            }
            if (width <= 0 || height <= 0) {
                final Bitmap bmp = viewToDrawFrom.getDrawingCache();
                final Bitmap result = bmp == null ? null : Bitmap.createBitmap(bmp);
                if (!wasDrawingCacheEnabled)
                    viewToDrawFrom.setDrawingCacheEnabled(false);
                return result;
            }
            viewToDrawFrom.layout(0, 0, width, height);
        } else {
            viewToDrawFrom.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
            viewToDrawFrom.layout(0, 0, viewToDrawFrom.getMeasuredWidth(), viewToDrawFrom.getMeasuredHeight());
        }
        final Bitmap drawingCache = viewToDrawFrom.getDrawingCache();
        final Bitmap bmp = ThumbnailUtils.extractThumbnail(drawingCache, width, height);
        final Bitmap result = bmp == null || bmp != drawingCache ? bmp : Bitmap.createBitmap(bmp);
        if (!wasDrawingCacheEnabled)
            viewToDrawFrom.setDrawingCacheEnabled(false);
        return result;
    }
    

    Using the above code, you don't have to specify the size of the bitmap (use 0 for width&height) if you want to use the one of the view itself.

    Also, if you wish to convert special views (SurfaceView, Surface or Window, for example) to a bitmap, you should consider using PixelCopy class instead. It requires API 24 and above though. I don't know how to do it before.

    0 讨论(0)
提交回复
热议问题