Android中添加背景水印,含WebView

绝密资料,必须加上水印背景以免外传,在Android中如何实现呢?分为普通背景水印和加载网页中添加水印。当然网页本身可以直接加,WebView加载下来直接就有了,这种方案我们不讨论,只讨论在WebView中追加。
先上效果图:
普通背景水印
普通背景水印
网页水印

网页水印

普通背景水印

创建一个水印Bitmap,为rootView设置背景。so easy,直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watermark);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
findViewById(android.R.id.content).setBackground(background("水印"));
}else{
findViewById(android.R.id.content).setBackgroundDrawable(background("水印"));
}
}
BitmapDrawable background(String watermark){
Bitmap bitmap = Bitmap.createBitmap(240, 240, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.GRAY);
paint.setAlpha(80);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(40);
Path path = new Path();
path.moveTo(30, 150);
path.lineTo(300, 0);
canvas.drawTextOnPath(watermark, path, 0, 30, paint);

BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
bitmapDrawable.setDither(true);
return bitmapDrawable;
}

网页水印

网页背景水印
在网页加载完成后插入javascript添加div水印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
String addWaterMarker(String watermark) {
String js = "var newscript = document.createElement(\"script\");";
js += "var bbTextNode = document.createTextNode(\"";
js += "var can = document.createElement('canvas');";
js +=
"var body = document.body;body.appendChild(can);can.width=400; can.height=200;can.style.display='none';"
+ "var cans = can.getContext('2d');cans.rotate(-20*Math.PI/180);cans.font = '16px Microsoft JhengHei';"
+ "cans.fillStyle = 'rgba(17, 17, 17, 0.50)';cans.textAlign = 'left';"
+ "cans.textBaseline = 'Middle';cans.fillText('watermark',can.width/3,can.height/2);"
+ "body.style.backgroundImage='url('+can.toDataURL('image/png')+')';"
;
js += "\");";
js += "newscript.appendChild(bbTextNode);";
js += "var t = document.createTextNode(\"alert('action');\");";
js += "newscript.appendChild(t);";
//js += "newscript.onload=function(){xxx();};"; //xxx()代表js中某方法
js += "document.body.appendChild(newscript);";

return js;
}

String addWaterMarkerDiv(String watermark) {
String js = "var newscript = document.createElement(\"script\");";

js += "var waterMarkDiv = document.createTextNode(\""
//+"if(document.getElementById('waterMark') != null) return;"
+ "var m = 'waterMark';"
+ "var newMark = document.createElement('div');"
+ "newMark.id = m;"
+ "newMark.style.position = 'absolute';"
+ "newMark.style.zIndex = '9527';"
+ "newMark.style.top = '0px';"
+ "newMark.style.left = '0px';"
+ "newMark.style.width = '800px';"
+ "newMark.style.height = '400px';"
+ "alert('newmark');"
+ "\");"
;

js += "newscript.appendChild(waterMarkDiv);";
js += "var bbTextNode = document.createTextNode(\"";
js += "var can = document.createElement('canvas');";
js +=
"var body = document.body;body.appendChild(can);can.width=400; can.height=200;can.style.display='none';"
+ "var cans = can.getContext('2d');cans.rotate(-20*Math.PI/180);cans.font = '16px Microsoft JhengHei';"
+ "cans.fillStyle = 'rgba(17, 17, 17, 0.50)';cans.textAlign = 'left';"
+ "cans.textBaseline = 'Middle';cans.fillText('watermark',can.width/3,can.height/2);"
+ "newMark.style.backgroundImage='url('+can.toDataURL('image/png')+')';"
+ "newMark.style.filter = 'alpha(opacity=50)';"
+ "document.body.appendChild(newMark);"
;
js += "\");";
js += "newscript.appendChild(bbTextNode);";
js += "document.body.appendChild(newscript);";

return js;
}

完整demo例子

引用

【Android】webview javascript 注入方法
Javascript 网页水印(非图片水印)实现代码
html如何添加水印?

转载请标明出处:
http://blog.520wa.com/
本文出自Tu’s blog