How to create a Layout bitmap keeping the dimensions equal regardless of the screen density?

Asked

Viewed 182 times

3

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

According to the resolution of the cell phone, it changes the sizes of the sources of textviews - the higher the resolution, the higher the source.

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 print width and is correct on the printer. Could someone help me with this?

2 answers

1


The number of pixels that Bitmap will have is a function of the pixel density of the device screen.

Thus, in order for the print size to be independent of density, you must resize the Bitmap according to the size of the view measured in dp.

Get the dimensions of Relativelayout using methods getWidth() and getHeight().

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 method getViewBitmap() of this reply. Then resize it to the desired dimensions using for example Bitmap.createScaledBitmap().

It will be 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 screen density of the device.
If the dimensions of resizedBitmap are not suitable for printing (because they are small or large) apply a factor to the values WidthDp and HeightDp in order to obtain the dimensions you want.

  • still doesn’t work

  • Then you have to explain the problem better.

  • this link has the application working: https://drive.google.com/open?id=0B0LBOPuhnAPiOWZYM3N1NC10VEk and if it is tested on phones with different resolutions you will be able to view the different bitmap

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 can 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: https://github.com/HendrixString/Android-PdfMyXml

  • I can export pro pdf but at the time of generating bitmap, it continues with the same error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.