首先自定义一个textview 主要为了水印的倾斜 如果不要这个功能 可以直接用textView
public class RotateTextView extends TextView{
public RotateTextView(Context context) {
super(context);
}
public RotateTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
//倾斜度45,上下左右居中
canvas.rotate(-45, getMeasuredWidth()/2, getMeasuredHeight()/2);
super.onDraw(canvas);
}
写个水印工具类
public class WaterMaskUtil {
/**
* 顶部
*/
private static final int TOP = 1;
/**
* 底部
*/
private static final int BOTTOM = 2;
/**
* 中间
*/
private static final int CENTER_HORIZONTAL = 3;
/**
* 左边
*/
private static final int LEFT = 4;
/**
* 右边
*/
private static final int RIGHT = 5;
/**
* 蓝色
*/
private static final int BLUE = 1;
/**
* 黑色
*/
private static final int BLACK = 2;
/**
* 红色
*/
private static final int RED = 3;
/**
* 绿色
*/
private static final int GREEN = 4;
/**
* 黄色
*/
private static final int YELLOW = 5;
/**
* 展示水印
* @param context 上下文
* @param text 展示的文本信息
* @param position 水印在界面的位置 1:顶部 ; 2:底部; 3:中间; 4:左边; 5:右边; 默认在底部
* @param color 水印文本颜色 1:蓝色; 2:黑色; 3:红色; 4:绿色; 5:黄色; 默认蓝色
*/
@SuppressLint("NewApi")
public static void showWaterMask(Context context ,Drawable background , String text , int position , int color){
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackground(background);
RotateTextView textView = new RotateTextView(context);
textView.setText(text);
textView.setTextSize((float) 23.5);
textView.setGravity(Gravity.CENTER);
if (color == BLUE) {
textView.setTextColor(Color.BLUE);
}else if (color == BLACK) {
textView.setTextColor(Color.BLACK);
}else if (color == RED) {
textView.setTextColor(Color.RED);
}else if (color == GREEN) {
textView.setTextColor(Color.GREEN);
}else if (color == YELLOW) {
textView.setTextColor(Color.YELLOW);
}else {
textView.setTextColor(Color.BLUE);
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
params.gravity = Gravity.CENTER;
linearLayout.addView(textView,params);
linearLayout.setAlpha((float) 0.5);
PopupWindow mpopWindow = new PopupWindow(linearLayout, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);
mpopWindow.setContentView(linearLayout);
// 显示PopupWindow
mpopWindow.setFocusable(false);
mpopWindow.setOutsideTouchable(false);
mpopWindow.setTouchable(false);
if (position == TOP) {
mpopWindow.showAtLocation(linearLayout, Gravity.TOP, 0, 300);
}else if (position == BOTTOM) {
mpopWindow.showAtLocation(linearLayout, Gravity.BOTTOM, 0, 200);
}else if (position == CENTER_HORIZONTAL) {
mpopWindow.showAtLocation(linearLayout, Gravity.CENTER_HORIZONTAL, 0, 0);
}else if (position == LEFT) {
mpopWindow.showAtLocation(linearLayout, Gravity.LEFT, 100, 0);
}else if (position == RIGHT) {
mpopWindow.showAtLocation(linearLayout, Gravity.RIGHT, 100, 0);
}else {
mpopWindow.showAtLocation(linearLayout, Gravity.BOTTOM, 0, 200);
}
}
}
主要看 showWaterMask 这个方法 里面的参数就好 有注释
注意 在activity 里面 防止重复show水印方法 记得加下面那个 ture false
boolean inited = false;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && !inited) {
inited = true;
WaterMaskUtil.showWaterMask(MainActivity.this ,null , userPin , 3 , 2);
}
}
效果图