Android添加水印功能

  1. import android.app.Activity;

  2. import android.graphics.Canvas;

  3. import android.graphics.ColorFilter;

  4. import android.graphics.Paint;

  5. import android.graphics.PixelFormat;

  6. import android.graphics.drawable.Drawable;

  7. import android.support.annotation.IntRange;

  8. import android.support.annotation.NonNull;

  9. import android.support.annotation.Nullable;

  10. import android.view.ViewGroup;

  11. import android.widget.FrameLayout;

  12.  
  13. /**

  14. * 水印

  15. * <pre>

  16. * author : cc

  17. * version : 1.0, 2020-09-23

  18. * since : 1.0, 2020-09-23

  19. * </pre>

  20. */

  21. public class Watermark {

  22. /**

  23. * 水印文本

  24. */

  25. private String mText;

  26. /**

  27. * 字体颜色,十六进制形式,例如:0xAEAEAEAE

  28. */

  29. private int mTextColor;

  30. /**

  31. * 字体大小,单位为sp

  32. */

  33. private float mTextSize;

  34. /**

  35. * 旋转角度

  36. */

  37. private float mRotation;

  38. private static Watermark sInstance;

  39.  
  40. private Watermark() {

  41. mText = "";

  42. mTextColor = 0xAEAEAEAE;

  43. mTextSize = 18;

  44. mRotation = -25;

  45. }

  46.  
  47. public static Watermark getInstance() {

  48. if (sInstance == null) {

  49. synchronized (Watermark.class) {

  50. sInstance = new Watermark();

  51. }

  52. }

  53. return sInstance;

  54. }

  55.  
  56. /**

  57. * 设置水印文本

  58. *

  59. * @param text 文本

  60. * @return Watermark实例

  61. */

  62. public Watermark setText(String text) {

  63. mText = text;

  64. return sInstance;

  65. }

  66.  
  67. /**

  68. * 设置字体颜色

  69. *

  70. * @param color 颜色,十六进制形式,例如:0xAEAEAEAE

  71. * @return Watermark实例

  72. */

  73. public Watermark setTextColor(int color) {

  74. mTextColor = color;

  75. return sInstance;

  76. }

  77.  
  78. /**

  79. * 设置字体大小

  80. *

  81. * @param size 大小,单位为sp

  82. * @return Watermark实例

  83. */

  84. public Watermark setTextSize(float size) {

  85. mTextSize = size;

  86. return sInstance;

  87. }

  88.  
  89. /**

  90. * 设置旋转角度

  91. *

  92. * @param degrees 度数

  93. * @return Watermark实例

  94. */

  95. public Watermark setRotation(float degrees) {

  96. mRotation = degrees;

  97. return sInstance;

  98. }

  99.  
  100. /**

  101. * 显示水印,铺满整个页面

  102. *

  103. * @param activity 活动

  104. */

  105. public void show(Activity activity) {

  106. show(activity, mText);

  107. }

  108.  
  109. /**

  110. * 显示水印,铺满整个页面

  111. *

  112. * @param activity 活动

  113. * @param text 水印

  114. */

  115. public void show(Activity activity, String text) {

  116. WatermarkDrawable drawable = new WatermarkDrawable();

  117. drawable.mText = text;

  118. drawable.mTextColor = mTextColor;

  119. drawable.mTextSize = mTextSize;

  120. drawable.mRotation = mRotation;

  121.  
  122. ViewGroup rootView = activity.findViewById(android.R.id.content);

  123. FrameLayout layout = new FrameLayout(activity);

  124. layout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,

  125. ViewGroup.LayoutParams.MATCH_PARENT));

  126. layout.setBackground(drawable);

  127. rootView.addView(layout);

  128. }

  129.  
  130. private class WatermarkDrawable extends Drawable {

  131. private Paint mPaint;

  132. /**

  133. * 水印文本

  134. */

  135. private String mText;

  136. /**

  137. * 字体颜色,十六进制形式,例如:0xAEAEAEAE

  138. */

  139. private int mTextColor;

  140. /**

  141. * 字体大小,单位为sp

  142. */

  143. private float mTextSize;

  144. /**

  145. * 旋转角度

  146. */

  147. private float mRotation;

  148.  
  149. private WatermarkDrawable() {

  150. mPaint = new Paint();

  151. }

  152.  
  153. @Override

  154. public void draw(@NonNull Canvas canvas) {

  155. int width = getBounds().right;

  156. int height = getBounds().bottom;

  157. int diagonal = (int) Math.sqrt(width * width + height * height); // 对角线的长度

  158.  
  159. mPaint.setColor(mTextColor);

  160. mPaint.setTextSize(ConvertUtils.spToPx(mTextSize)); // ConvertUtils.spToPx()这个方法是将sp转换成px,ConvertUtils这个工具类在我提供的demo里面有

  161. mPaint.setAntiAlias(true);

  162. float textWidth = mPaint.measureText(mText);

  163.  
  164. canvas.drawColor(0x00000000);

  165. canvas.rotate(mRotation);

  166.  
  167. int index = 0;

  168. float fromX;

  169. // 以对角线的长度来做高度,这样可以保证竖屏和横屏整个屏幕都能布满水印

  170. for (int positionY = diagonal / 10; positionY <= diagonal; positionY += diagonal / 10) {

  171. fromX = -width + (index++ % 2) * textWidth; // 上下两行的X轴起始点不一样,错开显示

  172. for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {

  173. canvas.drawText(mText, positionX, positionY, mPaint);

  174. }

  175. }

  176.  
  177. canvas.save();

  178. canvas.restore();

  179. }

  180.  
  181. @Override

  182. public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {

  183. }

  184.  
  185. @Override

  186. public void setColorFilter(@Nullable ColorFilter colorFilter) {

  187. }

  188.  
  189. @Override

  190. public int getOpacity() {

  191. return PixelFormat.TRANSLUCENT;

  192. }

  193. }
  194. }
  195.  


 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值