在一些对安全性要求较高的app中,会要求对所有的界面加上水印,防止截屏泄露客户信息,总体思路是获取底层的布局,在布局上add一个自定义的水印View,由于添加View必须要在setContentView之后,所以把添加水印的过程放在onStart中,并加上判断防止多次添加.
自定义水印布局
注:RotateTextView布局转自http://download.csdn.net/download/fangjxl/9485001
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00000000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="horizontal" >
<RotateTextView
android:id="@+id/fragment_tag11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="20dp"
android:text="@string/app_name"
android:textColor="#22666666"
android:textSize="22sp"
app:degree="350dp" />
<RotateTextView
android:id="@+id/fragment_tag12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:text="@string/app_name"
android:textColor="#22666666"
android:textSize="22sp"
app:degree="350dp" />
<RotateTextView
android:id="@+id/fragment_tag13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="20dp"
android:text="@string/app_name"
android:textColor="#22666666"
android:textSize="22sp"
app:degree="350dp" />
<RotateTextView
android:id="@+id/fragment_tag14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="20dp"
android:text="@string/app_name"
android:textColor="#22666666"
android:textSize="22sp"
app:degree="350dp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
attr
<declare-styleable name="RotateTextView">
<attr name="degree" format="dimension" />
</declare-styleable>
java
RotateTextView
public class RotateTextView extends TextView {
@Override
public void setText(CharSequence text, BufferType type) {
// TODO Auto-generated method stub
super.setText(waterMark, type);
}
private static final int DEFAULT_DEGREES = 0;
//水印文字
public static String waterMark = "";
private int mDegrees;
public RotateTextView(Context context) {
super(context, null);
}
public RotateTextView(Context context, AttributeSet attrs) {
super(context, attrs, android.R.attr.textViewStyle);
this.setGravity(Gravity.CENTER);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.RotateTextView);
mDegrees = a.getDimensionPixelSize(R.styleable.RotateTextView_degree,
DEFAULT_DEGREES);
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
canvas.rotate(mDegrees, this.getWidth() / 2f, this.getHeight() / 2f);
super.onDraw(canvas);
canvas.restore();
}
public void setDegrees(int degrees) {
mDegrees = degrees;
}
}
Activity
@Override
protected void onStart() {
//判断是否已经添加过
if(isAdd){
}else {
ViewGroup rootView = getRootView(this);
View framView = LayoutInflater.from(this).inflate(R.layout.fragment_layout, null);
rootView.addView(framView);
isAdd=true;
}
super.onStart();
}
//查找布局的底层
protected static ViewGroup getRootView(Activity context)
{
return (ViewGroup)context.findViewById(android.R.id.content);
}