![](/img/trans.png)
[英]Android: How to use a VirtualDisplay to host a WebView in a WallpaperService?
[英]How to set size of webview in WallpaperService?
我正在嘗試將webview設置為動態壁紙,但是在調整尺寸時遇到問題。
在引擎中,我使用WallpaperService的上下文創建一個Web視圖:
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的大小?
通過使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.