Posts /

Making bitmaps from layouts in background

17 Jul 2016

Synthesize lets you make bitmaps without inflating them and in background.

Drawing the exact image shown in the layout can be a difficult task on Canvas. Defining the margins, colors, typeface, text wrapping, etc. is a huge task to accomplish manually. To draw on a canvas you must also define the styles. Generating bitmap from the layouts gives you freedom from the headaches drawing on the canvas.

Contents

Here comes Synthesize to the rescue. All you have to do is initialize the layout XML as you would do in any activity or fragment using Synthesize, the only difference is that you can now do it in background.

Setup


    //Project Level gradle
    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

    //Module Level gradle
    dependencies{
        compile "com.prashantsolanki:Synthesize:0.1.5"
    }

Usage


    Synthesize s= new Synthesize(this,R.layout.resource_id);
    //Required
    s.setParams(1024,1080);
    //Required
    s.generateBitmap(new OnBitmapGenerationListener(){
            @Override
            void onSuccess(Bitmap bitmap){

            }
            @Override
            void onError(Exception e){

            }
        })
    //Optional
    s.saveImage();

Required methods
Optional methods
Pro-tip