过去几天我尝试和尝试这些但是我无法让我的wodcloud以我想要的方式打印,这是因为我对javascript的知识很少,而且我对如何能够知之甚少将javascriptInterface合并到webview中。
我正在使用3个文件 - d3.html,d3.layout.cloud.js,d3.js.我主要编辑d3.html,因为它是通过loadUrl
方法加载到我的webview中的。另外两个.js
文件来自Json Davies D3 Word Cloud生成器。这是我的d3.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
</head>
<h1></h1>
<script src="d3.js"></script>
<script src="d3.layout.cloud.js"></script>
<script type="text/javascript">
function getWebViewSize(){
return Android.getWebViewSize();
}
</script>
<script>(function() {
var fill = d3.scale.category20();
var layout = d3.layout.cloud()
.size([getWebViewSize()[0], getWebViewSize()[1]])
.words([
"Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean",
"KitKat", "Lollipop", "Marshmallow"].map(function(d) {
return {text: d, size: 10 + Math.random() * 40};
}))
.padding(5)
.rotate(0)
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw);
layout.start();
function draw(words) {
d3.select("body").append("svg")
.attr("width", layout.size()[0])
.attr("height", layout.size()[1])
.append("g")
.attr("transform", "translate(" + layout.size()[0] / 2 + "," + layout.size()[1] / 2 + ")")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}
})();</script>
<body/>
我面临的问题:
我无法在javascript界面中获得webview的宽度和高度。 .size(
方法只接受我认为整数,我无法得到我正在加载它的webview的大小。我尝试使用metrics
并从document
获取,我不知道它是如何工作的,但它不起作用。
我无法更改单词的颜色,每当我编辑任何样式时,它都不会加载到webview中。我也不能在单词上添加阴影。
正如您所看到的那些单词仍然是静态的,我怎样才能获得此function
然后在将其加载到webview之前动态更改单词和大小?
编辑:
我尝试了这个以获得宽度和高度,但我得到以下日志,并对第一个感到紧张
10-04 10:14:56.234 4550-4550/[package] I/Choreographer: Skipped 35 frames! The application may be doing too much work on its main thread.
10-04 10:15:11.935 30842-30842/[package] I/chromium: [INFO:CONSOLE(20)]
"Uncaught TypeError: Cannot read property '0' of undefined", source: file:///android_asset/wordcloud_html/d3.html (20)
10-04 10:15:14.478 30842-30842/[package] W/art: Attempt to remove local handle scope entry from IRT, ignoring
10-04 10:15:14.478 30842-30842/[package] W/art: Attempt to remove local handle scope entry from IRT, ignoring
10-04 10:15:14.524 30842-30842/[package] W/cr.BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 30842
10-04 10:15:14.537 30842-30842/[package] I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2d741dbf time:1235333
10-04 10:15:14.842 30842-30842/[package] I/chromium: [INFO:CONSOLE(20)] "Uncaught TypeError: Cannot read property '0' of undefined", source: file:///android_asset/wordcloud_html/d3.html (20)
这是我的JavascriptInterface
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public int[] getWebViewSize()
{
int[] size = new int[2];
size[0] = webview.getWidth();
size[1] = webview.getHeight();
return size;
}
}
以下是加载webview的代码
webView = view.getWebView();
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
JSInterface = new JavaScriptInterface(context);
webView.setWebViewClient(new InternalWebViewClient());
webView.setBackgroundColor(fragment.getResources().getColor(R.color.analysis_complete_bg));
webView.loadUrl("file:///android_asset/wordcloud_html/d3.html");
webView.addJavascriptInterface(JSInterface, "Android");
答案 0 :(得分:1)
对于你的第一个问题,我相信你只能返回原语(没有arrarys)。此外,变量webview
如何创建/可访问?也许您可以尝试将界面用作简单的传递。对于你的第三个问题,来自哪里的话?这是回到你的Android Java吗?再次,您可以将其作为通过线连接。
public class JavaScriptInterface {
Context mContext;
int mWidth, mHeight;
string mJSON;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c, int w, int h, string json) {
mContext = c;
mWidth = w;
mHeight = h;
mJSON = json;
}
@JavascriptInterface
public int getWidth()
{
return wWidth;
}
@JavascriptInterface
public int getHeight()
{
return mHeight;
}
@JavascriptInterface
public string getWords()
{
return mJSON;
}
}
并将其创建为:
new JavaScriptInterface(AppilyApplication.appContext,
webview.getWidth(),
webview.getHeight(),
"[{\"text\":\"Hello\",\"size\":42},{\"text\":\"world\",\"size\":96}]"
);
并在JavaScript中使用它:
.size([Android.getWidth(), Android.getHeight()])
.words(JSON.parse(Android.getWords())