如何在WallpaperService中设置Webview的大小? [英] How to set size of webview in WallpaperService?

查看:264
本文介绍了如何在WallpaperService中设置Webview的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将webview设置为动态壁纸,但是在调整大小方面存在问题。

I'm trying to set webview as a live wallpaper, but I have a problem with sizing it.

在引擎中,我使用WallpaperService的Context创建了一个webview:

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);
}

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

But the wallpaper will be white and javascript reports, that window's size is 0px.

如何设置WebView的大小?

How to set size of the WebView?

推荐答案

解决了它通过使用WindowManager,在LayoutParams中定义尺寸并将WebView可见性更改为GONE。

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);

这篇关于如何在WallpaperService中设置Webview的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆