现在市面上,有很多APP在一些关键页面上会有水印,水印一般都是一些身份相关的信息,这样可以保证截屏后的图片具有识别性,能够知道这张截图是谁截取的。
为了能够方便地在任意页面上加水印,所以应该采取用Java代码来实现,而不是在XML布局文件里面写。大概思路是,首先获取当前页面的底层布局,然后在布局最上层添加一个全屏的带有水印的FrameLayout布局。这个全屏的带有水印的FrameLayout布局,我们可以在XML写。很多博客文章都是介绍这种方式来写水印,不过大多数都没做适配,特别是横屏显示时,水印效果很不好。所以我这里用Java代码来画水印。自定义一个Drawable,把它当作水印,然后设置到FrameLayout布局的背景,这样就行了。相关代码如下所示:
- import android.app.Activity;
- import android.graphics.Canvas;
- import android.graphics.ColorFilter;
- import android.graphics.Paint;
- import android.graphics.PixelFormat;
- import android.graphics.drawable.Drawable;
-
- import android.view.ViewGroup;
- import android.widget.FrameLayout;
-
- import androidx.annotation.Nullable;
-
- public class Watermark {
- /*** 水印文本*/
- private String mText;
- /*** 字体颜色,十六进制形式,例如:0xAEAEAEAE*/
- private int mTextColor;
- /*** 字体大小,单位为sp*/
- private float mTextSize;
- /*** 旋转角度*/
- private float mRotation;
- private static Watermark sInstance;
-
- private Watermark() {
- mText = "";
- mTextColor = 0xAEAEAEAE;
- mTextSize = 18;
- mRotation = -25;
- }
-
- public static Watermark getInstance() {
- if (sInstance == null) {
- synchronized (Watermark.class) {
- sInstance = new Watermark();
- }
- }
- return sInstance;
- }
-
- /*** 设置水印文本** @param text 文本* @return Watermark实例*/
- public Watermark setText(String text) {
- mText = text;
- return sInstance;
- }
-
- /*** 设置字体颜色** @param color 颜色,十六进制形式,例如:0xAEAEAEAE* @return Watermark实例*/
- public Watermark setTextColor(int color) {
- mTextColor = color;
- return sInstance;
- }
-
- /*** 设置字体大小** @param size 大小,单位为sp* @return Watermark实例*/
- public Watermark setTextSize(float size) {
- mTextSize = size;
- return sInstance;
- }
-
- /*** 设置旋转角度** @param degrees 度数* @return Watermark实例*/
- public Watermark setRotation(float degrees) {
- mRotation = degrees;
- return sInstance;
- }
-
- /*** 显示水印,铺满整个页面** @param activity 活动*/
- public void show(Activity activity) {
- show(activity, mText);
- }
-
- /*** 显示水印,铺满整个页面** @param activity 活动* @param text 水印*/
- public void show(Activity activity, String text) {
- WatermarkDrawable drawable = new WatermarkDrawable();
- drawable.mText = text;
- drawable.mTextColor = mTextColor;
- drawable.mTextSize = mTextSize;
- drawable.mRotation = mRotation;
- ViewGroup rootView = activity.findViewById(android.R.id.content);
- FrameLayout layout = new FrameLayout(activity);
- layout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
- layout.setBackground(drawable);
- rootView.addView(layout);
- }
-
-
- class WatermarkDrawable extends Drawable {
- private Paint mPaint;
- /*** 水印文本*/
- private String mText;
- /*** 字体颜色,十六进制形式,例如:0xAEAEAEAE*/
- private int mTextColor;
- /*** 字体大小,单位为sp*/
- private float mTextSize;
- /*** 旋转角度*/
- private float mRotation;
-
- WatermarkDrawable() {
- mPaint = new Paint();
- }
-
- @Override
- public void draw(Canvas canvas) {
- int width = getBounds().right;
- int height = getBounds().bottom;
- int diagonal = (int) Math.sqrt(width * width + height * height); // 对角线的长度mPaint.setColor(mTextColor);mPaint.setTextSize(ConvertUtils.spToPx(mTextSize)); // ConvertUtils.spToPx()这个方法是将sp转换成px,ConvertUtils这个工具类在我提供的demo里面有mPaint.setAntiAlias(true);float textWidth = mPaint.measureText(mText);canvas.drawColor(0x00000000);canvas.rotate(mRotation);int index = 0;float fromX;// 以对角线的长度来做高度,这样可以保证竖屏和横屏整个屏幕都能布满水印for (int positionY = diagonal / 10; positionY <= diagonal; positionY += diagonal / 10) {fromX = -width + (index++ % 2) * textWidth; // 上下两行的X轴起始点不一样,错开显示for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {canvas.drawText(mText, positionX, positionY, mPaint);}}canvas.save();canvas.restore();}@Overridepublic void setAlpha(@IntRange(from = 0, to = 255) int alpha) {}@Overridepublic void setColorFilter(@Nullable ColorFilter colorFilter) {}@Overridepublic int getOpacity() {return PixelFormat.TRANSLUCENT;}}
- }
-
- @Override
- public void setAlpha(int alpha) {
-
- }
-
- @Override
- public void setColorFilter(@Nullable ColorFilter colorFilter) {
-
- }
-
- @Override
- public int getOpacity() {
- return PixelFormat.UNKNOWN;
- }
- }
-
- }
为了保证能够在当前页面的最顶层加水印,应该在 setContentView() 后面添加水印
- @Overrideprotected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Watermark.getInstance().show(this, "Fantasy BlogDemo");// 可以自定义水印文字颜色、大小和旋转角度
- Watermark.getInstance().setText("Fantasy BlogDemo").setTextColor(0xAE000000) .setTextSize(16).setRotation(-30).show(this);}