I need to draw a WebView with a Canvas. I'm now looking for an answer for two days but can't find anything doing what i need.
At the moment I'm able to show the WebView like this
WebView wv = new WebView(this);
String htmlContent = foo_getHtmlContent();
wv.loadData(htmlContent, "text/html", "UTF-8");
setContentView(wv);
which works pretty fine but starts the view at pos 0,0. Also I'm able to draw a view (inflated from XML) with a Canvas like this
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.layout_test, null);
v.measure(MeasureSpec.getSize(v.getMeasuredWidth()), MeasureSpec.getSize(v.getMeasuredHeight()));
v.layout(400, 400, 400, 400);
canvas.translate(100, 100);
v.draw(canvas);
which is exactly the way i want to draw but with the wrong view. I can't find a way to combine these two ways to draw the view from the first code in the way of second code (or any similar way)
Does anyone know how to draw a WebView with a Canvas?
EDIT:
Does no one has any idea?
Now i've tried this but i get a NullPointerException because of the missing attributes.
LayoutInflater li =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
WebView wv = null;
try
{
wv = (WebView) li.createView("WebView", "android.webkit.", null);
wv.loadData(htmlContent, "text/html", "UTF-8");
wv.measure(MeasureSpec.getSize(wv.getMeasuredWidth()),MeasureSpec.getSize(wv.getMeasuredHeight()));
wv.layout(100, 100, 400, 400);
canvas.translate(100, 100);
wv.draw(canvas);
}catch (InflateException e)
{
Log.e("Meassure", "InflateException: " + e);
} catch (ClassNotFoundException e)
{
Log.e("Meassure", "ClassNotFoundException: " + e);
}
does anyone knows how to create fitting attributes?
EDIT2:
Me again.
I've tried something else and found a way which is working with a TextView but not with a web view.
This
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.layout_test, null);
TextView textView = (TextView) v.findViewById(R.id.textView1);
textView.setText("Hello World");
v.measure(MeasureSpec.getSize(v.getMeasuredWidth()), MeasureSpec.getSize(v.getMeasuredHeight()));
v.layout(400, 400, 400, 400);
canvas.translate(100, 100);
v.draw(canvas);
works pretty fine and does everything a TextView is supposed to do but this
View v = li.inflate(R.layout.web_layout, null);
WebView wv = (WebView) v.findViewById(R.id.webView);
wv.loadData(htmlContent, "text/html", "UTF-8");
v.measure(MeasureSpec.getSize(v.getMeasuredWidth()), MeasureSpec.getSize(v.getMeasuredHeight()));
v.layout(400, 400, 400, 400);
canvas.translate(100, 100);
v.draw(canvas);//if i substitute this line by setContentView(v); it works but without positioning
does absolutely nothing(except throwing funny stuff to LogCat).
The LogCat-output is the following(repeating about every 10ms):
06-17 14:04:06.210: D/webview(19424): [InitTabEffectPivot] >> nScreenWidth = 720
06-17 14:04:06.210: D/webview(19424): [InitTabEffectPivot] >> nScreenHeight = 1280
06-17 14:04:06.220: D/skia(19424): notifyPluginsOnFrameLoad not postponed
I also tried to call Thread.sleep(10) after line wv.loadData because i've read somewhere that a WebView needs some time to load its content but this doesn't work too. Does at least anyone know what the logcat-output means?