开发者

Google Charts with Android Webview

I am trying to do the typical thing where you load data into a URL string, call google's chart API with all that date, and then Load it into a Webview.

Question: Does someone have a direct 'right' way to do this?

I have been messing with this all night.

Here is the main problem:

The webview is set to Fill_Parent. Meaning I dont really know the exact width and height. Since google's API requires that you present a width and height, how exactly do I get the webviews width and height so I can send it over? I tried this solution:

final WebView web = (WebView)findViewById(R.id.webviewgraph);
    ViewTreeObserver tree = web.getViewTreeObserver();
    tree.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout() 
        {...code continues

And things seem to be in order, but when I call web.getWidth(), web.getDrawingBounds(), or web.getMeasuredWidth(), I am always returned MORE than what the screen actually holds. Right now I am solving this issue by subtracting a constant offset of 136 pixels... but alas, this is NOT consistent with all devices.

Does someone know either the "correct" way to use google map API's with a fluid webview? Or perhaps does someone know how to get the actual correct width and height of a view?

Thanks

UPDATE I changed my code such that now I am using a custom webview that overrides the OnSizeChanged event. The width and height that are returned are ... unfortunately still the same... Just too large.

UPDATE After analyzing the width and height more, I came up with this:

int w = (int)((float)width * .667f);
int h = (int)((float)height * .667f)开发者_JS百科;

So far this is working on the devices I am testing on. Why would the width and height be returning 1/3 MORE than the actual width? I have no idea.


I haven't had time to double check this, but I find it highly likely that your 0.667f is related to the fact that the webview is scaled according to the density of your devices.

According to http://developer.android.com/guide/webapps/targeting.html#ViewportDensity "the high-density device shows the web page with a default scale factor that is 1.5 times larger than the actual pixel resolution, to match the target density"

You need to find the target density use and use it as the scale:

final float scale = getResources().getDisplayMetrics().density;

// Your code:
int w = (int)(width / scale);
int h = (int)(height / scale);

I'm not too well read on the subject, but I think that you can prevent the scaling using

<meta name="viewport" content="target-densitydpi=device-dpi" />

but don't take my word for it. Also, I'm not sure from which API level the viewport is valid.


Can you try using the callback OnSizeChanged() ? . They say its called after rendering the view so it should hold information about the actual size if the WebView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜