如何在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);  
    } 
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值