带有 Android Webview 的 Google 图表
我正在尝试执行典型的操作,将数据加载到 URL 字符串中,使用所有该日期调用 google 的图表 API,然后将其加载到 Web 视图中。
问题:有人有直接的“正确”方法来做到这一点吗?
我整晚都在搞乱这个问题。
主要问题是:
webview 设置为 Fill_Parent。这意味着我真的不知道确切的宽度和高度。由于谷歌的API要求您提供宽度和高度,那么我到底如何获取网络视图的宽度和高度以便将其发送过来?我尝试了这个解决方案:
final WebView web = (WebView)findViewById(R.id.webviewgraph);
ViewTreeObserver tree = web.getViewTreeObserver();
tree.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{...code continues
事情似乎按顺序进行,但是当我调用 web.getWidth()、web.getDrawingBounds() 或 web.getMeasuredWidth() 时,我返回的总是比屏幕实际容纳的更多。现在我正在通过减去 136 像素的恒定偏移来解决这个问题......但可惜的是,这与所有设备不一致。
有人知道使用 google 地图 API 和流畅的 webview 的“正确”方法吗?或者也许有人知道如何获得视图的实际正确宽度和高度?
谢谢
更新 我更改了代码,现在我使用自定义 webview 来重写 OnSizeChanged 事件。返回的宽度和高度...不幸的是仍然相同...只是太大了。
更新 在对宽度和高度进行了更多分析之后,我得出了这样的结论:
int w = (int)((float)width * .667f);
int h = (int)((float)height * .667f);
到目前为止,这在我正在测试的设备上有效。为什么返回的宽度和高度会比实际宽度多 1/3?我不知道。
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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有时间仔细检查这一点,但我发现您的 0.667f 很可能与 webview 根据设备的密度进行缩放这一事实有关。
根据
http://developer.android.com/guide/webapps/targeting.html#ViewportDensity
“高密度设备以比实际像素分辨率大1.5倍的默认比例因子显示网页,以匹配目标密度”
您需要找到目标密度用途并使用它作为比例:
我对这个主题不太了解,但我认为你可以防止使用缩放
,但不要相信我的话。另外,我不确定视口在哪个 API 级别上是有效的。
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:
I'm not too well read on the subject, but I think that you can prevent the scaling using
but don't take my word for it. Also, I'm not sure from which API level the viewport is valid.
您可以尝试使用回调 OnSizeChanged() 吗? 。他们说它在渲染视图后调用,因此它应该保存有关 WebView 的实际大小的信息。
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.