如何在WallpaperService中设置webview的大小?

时间:2015-05-31 09:54:55

标签: android canvas webview size live-wallpaper

我正在尝试将webview设置为动态壁纸,但我在测量它时遇到了问题。

在引擎中,我使用WallpaperService的上下文创建一个webview:

public WallpaperEngine(Context context) {
    webView = new WebView(context);
    ...
}

我把它画到壁纸的画布上:

SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try {
    canvas = holder.lockCanvas();
    if (canvas != null) {
        webView.draw(canvas);
    }
} finally {
    if (canvas != null)
        holder.unlockCanvasAndPost(canvas);
}

但壁纸将是白色和javascript报告,该窗口的大小为0px。

如何设置WebView的大小?

1 个答案:

答案 0 :(得分:1)

使用WindowManager解决它,在LayoutParams中定义大小并将WebView可见性更改为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);