简体   繁体   中英

Android createbitmap width behaves different on different devices

I have a linear layout (width = fill parent, basically it fills the whole width of the device), this contains 2 child layouts. One linearlayout (let us call it linear1) with width=fill_parent and the 2nd one is a webview with width=fill_parent. By default the webview is defined with visibility=GONE and so it is not displayed on the screen and it doesnt occupy any space.

<ParentLinear> <Linear1 width=fill_parent></Linear1> <Webview width=fill_parent></Webview> </ParentLinear>

I programatically load a surface view into the linear layout linear1 and the webview remains visibility=gone. The surface view occupies full width of the phone on all devices, no issues there. Now under certain conditions I hide linear1 (visibility=gone) programatically and I create a new bitmap using createbitmap / write using canvas, write it as a file and then load it in the webview and make the webview visible. To make the bitmap use just the entire width of the device, not more, not less, what I do is I find the width of linear1 before I hide it using getwidth (and i get the correct value, it is not 0) and then i set that as the width of the bitmap I'm creating.

Now the bitmap is created with no issues,I write it as a file into the device and then it gets loaded into the webview from the file system and all is fine and dandy. It behaves exactly how I want it to behave on HTC Inspire (andriod 2.3 I think..), the bitmap / webview occupies the entire width of the device, and the size of the bitmap is exactly same as the width of linear1. But when I test this on HTC One MIni (android 4.2.2) the width of the bitmap is much larger than the width of the device and the width of linear1, all though I had originally created the bitmap using the width of linear1 which was occupying the entire width of the device. When I test it on Kindle FIre (first version of kindle fire) the width of the bitmap is smaller than the width of linear1. Basically works as expected on HTC Inspire, but differently on HTC One Mini and KIndle Fire. I have not tested on any other devices.

Just to be clear, I'm not referring to the zoom of the webview which I know can be adjusted, and the issue I'm referring to is the size of the bitmap. This might have to do with the resolution of each device , its pixels per pinch etc., but what I fail to understand is since I actually used the width of linear1 to set the width of the bitmap , then why wouldn't it be exactly same. One more thing, if I do a getwidth on the bitmap, the number I get back is the same as that of linear1, but on screen their with don't match, bitmap is visibly bigger or smaller. Please let me know if you can give me some pointers to understand how this works.

Here is the part of the code which I think is relevant.

    int bitmapX =  linear1.getWidth() //this is the value under question
    int bitmapY =  600; // will need to convert from dip to pixel later
    Bitmap myBitMap = Bitmap.createBitmap(bitmapX, bitmapY, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(myBitMap);

-- there is other code which is not shown here to write stuff into the bitmap using canvas -- 

    canvas.save();
    canvas.translate(
    myBitMap.getWidth() / 2 ,
    myBitMap.getHeight() / 2);
    canvas.restore();
    writeBitmap(myBitMap); //writes the bitmap to a file calling a user defined function.
    myBitMap.recycle();

I think you must multiply those amounts by the device density.

Try multiplying those amounts by

this.getResources().getDisplayMetrics().density

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM