How to create bitmap of a Layout keeping the same dimensions whatever the density of the screen?

3

I have a layout file and I'm passing it to a bitmap that I then send to a bluetooth printer in bytes.

According to the mobile resolution, it changes font sizes of textviews - the higher the resolution, the larger the font.

How do I generate the same layout regardless of the device and its resolution?

My code:

    layout.setDrawingCacheEnabled(true);
    layout.measure(View.MeasureSpec.makeMeasureSpec(580, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(layout.getMeasuredHeight(), View.MeasureSpec.UNSPECIFIED));
    layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
    layout.buildDrawingCache(false);
    Bitmap sImagem = Bitmap.createBitmap(layout.getDrawingCache(false));
    layout.setDrawingCacheEnabled(false);

The 580 is the width of the print and is correct in the printer. Could someone help me with this?

    
asked by anonymous 21.06.2017 / 16:36

2 answers

1

The number of pixels the Bitmap takes depends on the pixel density of the device screen.

So, in order for the print size to be density-independent, you should resize the Bitmap based on the dimension of the view measured in dp .

Get RelativeLayout dimensions using getWidth() and getHeight() methods.

The values returned by these methods are in pixels, convert them to dp's using this method:

public static int convertPixelsToDp(float pixels, Activity context){

    DisplayMetrics metrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float density = metrics.density;
    return (int) Math.ceil(pixels / density);
}

To get the bitmap use the getViewBitmap() method of this response . Then resize it to the desired dimensions using for example Bitmap.createScaledBitmap () .

It will look something like this:

int WidthDp = convertPixelsToDp(layout.getWidth());
int HeightDp = convertPixelsToDp(layout.getHeight());
Bitmap bitmap = getViewBitmap(layout);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, widthDp, heightDp, false);

The value of WidthDp and HeightDp will always be the same regardless of the density of the device screen. If the dimensions of resizedBitmap are not suitable for printing (because they are small or large), apply a factor to the WidthDp and HeightDp values in order to get the dimensions you want.

    
21.06.2017 / 23:22
0

I used this library to generate a PDF of a view. It has a method that generates a bitmap to be able to reuse, from a view without inflating it. I think it might help the problem:

AbstractViewRenderer page = new AbstractViewRenderer(context, R.layout.page1) {
    private String _text;

    public void setText(String text) {
        _text = text;
    }

    @Override
    protected void initView(View view) {
        TextView tv_hello = (TextView)view.findViewById(R.id.tv_hello);
         tv_hello.setText(_text);
    }
};

page.setReuseBitmap(true);

Reference: link

    
21.06.2017 / 20:41