简体   繁体   中英

Set transparent color in android webview

How to set transparent color in android webview?

<div style="background-color: black" >test</div>

How can I make black color to be transparent for the whole page (like chromakey)?

Had the same problem with the WebView, as it behaved randomly across different OS versions. Finnlay I fixed it with this code this after the loadDataWithBaseURL() call:

if (Build.VERSION.SDK_INT >= 11) {
    webView.setBackgroundColor(0x01000000);
} else {
    webView.setBackgroundColor(0x00000000);
}

I figure this will give the device something to draw, so various caching mechanisms will not kick in. But the result is practically the same as with total transparency, as it is undetectable by average human eye.

No performance penalties noticed as well.

Tested on several devices ranging from 2.2 to 4.2.

Cheers

尝试这个

(YourWebview).setBackgroundColor(0x00000000);

I've found the solution. I've reimplemented OnDraw method of WebView

@Override
    protected void onDraw(android.graphics.Canvas canvas) {

    super.onDraw(canvas);

    Paint p = new Paint();

    p.setARGB(255, 0, 0, 0);
    int removeColor = p.getColor(); 

    p.setAlpha(1); // if Alpha is 0 it doesn't work. I don't know why
    p.setXfermode(new AvoidXfermode(removeColor, 0, AvoidXfermode.Mode.TARGET));

    canvas.drawPaint(p);
}

你可以用它

webView.setBackgroundColor(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM