How to set size of webview in WallpaperService?

雨燕双飞 提交于 2020-01-14 04:33:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!