问题
I'm trying to set webview as a live wallpaper, but I have a problem with sizing it.
In the Engine I create a webview with WallpaperService's Context:
public WallpaperEngine(Context context) {
webView = new WebView(context);
...
}
And I draw it to wallpaper's canvas:
SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
webView.draw(canvas);
}
} finally {
if (canvas != null)
holder.unlockCanvasAndPost(canvas);
}
But the wallpaper will be white and javascript reports, that window's size is 0px.
How to set size of the WebView?
回答1:
Solved it by using WindowManager, defining size in LayoutParams and changing WebView visibility to GONE.
webView = new WebView(context);
webView.setVisibility(webView.GONE);
webView.loadUrl("http://example.com/");
wm = (WindowManager)context.getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT
);
params.width = //width
params.height = //height
wm.addView(webView, params);
来源:https://stackoverflow.com/questions/30556091/how-to-set-size-of-webview-in-wallpaperservice