如何在Android所有界面上實現水印

在一些對安全性要求較高的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);  
    } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章