Android 实现水印背景效果

举报
再见孙悟空_ 发表于 2022/01/14 00:49:44 2022/01/14
【摘要】 项目中有需要加水印的需求,实现完效果图是这样的 什么看不清... 为了让大家看清效果,字体改了一下,正常应该是文章最上面那个的效果。话不多说,直接上代码 import android.annotation.SuppressLint;import android.content.Context;import android....

项目中有需要加水印的需求,实现完效果图是这样的

什么看不清...

为了让大家看清效果,字体改了一下,正常应该是文章最上面那个的效果。话不多说,直接上代码


  
  1. import android.annotation.SuppressLint;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Rect;
  8. import android.graphics.Typeface;
  9. import android.text.TextPaint;
  10. import android.util.AttributeSet;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. import com.hkdc.commonlib.R;
  14. public class WaterMarkView extends View {
  15. private static final String DEFAULT_SEPARATOR = "///";
  16. private TextPaint mTextPaint = new TextPaint();
  17. private String[] mText;
  18. private int mDegrees;
  19. private int mTextColor;
  20. private int mTextSize=35;
  21. private boolean mTextBold;
  22. private int mDx;
  23. private int mDy;
  24. private Paint.Align mAlign;
  25. private boolean mSync;
  26. private int textWidth, textHeight;
  27. public WaterMarkView(Context context) {
  28. this(context, null);
  29. }
  30. public WaterMarkView(Context context, AttributeSet attrs) {
  31. super(context, attrs);
  32. TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WaterMarkView);
  33. mDegrees = typedArray.getInt(R.styleable.WaterMarkView_water_mark_degree, WaterMarkManager.INFO != null ? WaterMarkManager.INFO.getDegrees() : -30);
  34. String text = typedArray.getString(R.styleable.WaterMarkView_water_mark_text);
  35. if (text != null) {
  36. mText = text.split(DEFAULT_SEPARATOR);
  37. }
  38. mTextColor = typedArray.getColor(R.styleable.WaterMarkView_water_mark_textColor, WaterMarkManager.INFO != null ? WaterMarkManager.INFO.getTextColor() : Color.parseColor("#33000000"));
  39. mTextSize = typedArray.getDimensionPixelSize(R.styleable.WaterMarkView_water_mark_textSize, WaterMarkManager.INFO != null ? WaterMarkManager.INFO.getTextSize() : 42);
  40. mTextBold = typedArray.getBoolean(R.styleable.WaterMarkView_water_mark_textBold, WaterMarkManager.INFO != null && WaterMarkManager.INFO.isTextBold());
  41. mDx = typedArray.getDimensionPixelSize(R.styleable.WaterMarkView_water_mark_dx, WaterMarkManager.INFO != null ? WaterMarkManager.INFO.getDx() : 100);
  42. mDy = typedArray.getDimensionPixelSize(R.styleable.WaterMarkView_water_mark_dy, WaterMarkManager.INFO != null ? WaterMarkManager.INFO.getDy() : 240);
  43. int align = typedArray.getInt(R.styleable.WaterMarkView_water_mark_align, WaterMarkManager.INFO != null ? WaterMarkManager.INFO.getAlignInt() : 1);
  44. mAlign = align == 0 ? Paint.Align.LEFT : align == 2 ? Paint.Align.RIGHT : Paint.Align.CENTER;
  45. mSync = typedArray.getBoolean(R.styleable.WaterMarkView_water_mark_sync, true);
  46. typedArray.recycle();
  47. setBackgroundColor(Color.TRANSPARENT);
  48. mTextPaint.setAntiAlias(true);
  49. mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
  50. mTextPaint.setColor(mTextColor);
  51. mTextPaint.setTextSize(mTextSize);
  52. mTextPaint.setTypeface(mTextBold ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
  53. mTextPaint.setTextAlign(mAlign);
  54. mText = mText == null && mSync ? WaterMarkManager.CONTENT : mText;
  55. textWidth = 0;
  56. textHeight = 0;
  57. if (mText != null && mText.length > 0) {
  58. for (String s : mText) {
  59. Rect tvRect = new Rect();
  60. mTextPaint.getTextBounds(s, 0, s.length(), tvRect);
  61. textWidth = textWidth > tvRect.width() ? textWidth : tvRect.width();
  62. textHeight += (tvRect.height() + 10);
  63. }
  64. }
  65. if (mSync) {
  66. WaterMarkManager.LIST.add(this);
  67. }
  68. }
  69. @Override
  70. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  71. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  72. }
  73. @Override
  74. protected void onDraw(Canvas canvas) {
  75. super.onDraw(canvas);
  76. if (mText != null && mText.length > 0) {
  77. int measuredWidth = getMeasuredWidth();
  78. int measuredHeight = getMeasuredHeight();
  79. if (measuredWidth == 0 || measuredHeight == 0) {
  80. return;
  81. }
  82. int canvasLength = measuredWidth > measuredHeight ? measuredWidth : measuredHeight;
  83. canvas.save();
  84. canvas.rotate(mDegrees, measuredWidth / 2, measuredHeight / 2);
  85. canvas.save();
  86. int y = 0;
  87. boolean odd = true;
  88. while (y < canvasLength + textHeight) {
  89. int x = odd ? 0 : -(textWidth + mDx) / 2;
  90. while (x < canvasLength + textWidth) {
  91. drawTexts(mText, mTextPaint, canvas, x, y);
  92. x = x + textWidth + mDx;
  93. }
  94. y = y + textHeight + mDy;
  95. odd = !odd;
  96. }
  97. canvas.restore();
  98. }
  99. }
  100. private void drawTexts(String[] ss, Paint paint, Canvas canvas, int x, int y) {
  101. Paint.FontMetrics fontMetrics = paint.getFontMetrics();
  102. float top = fontMetrics.top;
  103. float bottom = fontMetrics.bottom;
  104. int length = ss.length;
  105. float total = (length - 1) * (bottom - top) + (fontMetrics.descent - fontMetrics.ascent);
  106. float offset = total / 2 - bottom;
  107. for (int i = 0; i < length; i++) {
  108. float yAxis = -(length - i - 1) * (bottom - top) + offset;
  109. canvas.drawText(ss[i], x, y + yAxis + 10, paint);
  110. }
  111. }
  112. /**
  113. * 设置水印文字内容
  114. *
  115. * @param text 文字内容
  116. */
  117. public void setText(String... text) {
  118. mText = text;
  119. textWidth = 0;
  120. textHeight = 0;
  121. if (mText != null && mText.length > 0) {
  122. for (String s : mText) {
  123. Rect tvRect = new Rect();
  124. mTextPaint.getTextBounds(s, 0, s.length(), tvRect);
  125. textWidth = textWidth > tvRect.width() ? textWidth : tvRect.width();
  126. textHeight += (tvRect.height() + 10);
  127. }
  128. }
  129. postInvalidate();
  130. }
  131. /**
  132. * 同步设置水印文字内容
  133. *
  134. * @param text 文字内容
  135. */
  136. void setSyncText(String... text) {
  137. if (mSync) {
  138. setText(text);
  139. }
  140. }
  141. /**
  142. * 设置水印倾斜角度
  143. *
  144. * @param degrees 倾斜角度(默认:-30)
  145. */
  146. public void setDegrees(int degrees) {
  147. mDegrees = degrees;
  148. postInvalidate();
  149. }
  150. /**
  151. * 同步设置水印倾斜角度
  152. *
  153. * @param degrees 倾斜角度(默认:-30)
  154. */
  155. void setSyncDegrees(int degrees) {
  156. if (mSync) {
  157. setDegrees(degrees);
  158. }
  159. }
  160. /**
  161. * 设置水印字体颜色
  162. *
  163. * @param textColor 字体颜色(默认:#33000000)
  164. */
  165. public void setTextColor(int textColor) {
  166. mTextColor = textColor;
  167. mTextPaint.setColor(mTextColor);
  168. postInvalidate();
  169. }
  170. /**
  171. * 同步设置水印字体颜色
  172. *
  173. * @param textColor 字体颜色(默认:#33000000)
  174. */
  175. void setSyncTextColor(int textColor) {
  176. if (mSync) {
  177. setTextColor(textColor);
  178. }
  179. }
  180. /**
  181. * 设置水印字体大小(单位:px)
  182. *
  183. * @param textSize 字体大小(默认:42px)
  184. */
  185. public void setTextSize(int textSize) {
  186. mTextSize = textSize;
  187. mTextPaint.setTextSize(30);
  188. postInvalidate();
  189. }
  190. /**
  191. * 同步设置水印字体大小(单位:px)
  192. *
  193. * @param textSize 字体大小(默认:42px)
  194. */
  195. void setSyncTextSize(int textSize) {
  196. if (mSync) {
  197. setTextSize(30);
  198. }
  199. }
  200. /**
  201. * 设置水印字体是否粗体
  202. *
  203. * @param textBold 是否粗体(默认:false)
  204. */
  205. public void setTextBold(boolean textBold) {
  206. mTextBold = textBold;
  207. mTextPaint.setTypeface(mTextBold ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
  208. postInvalidate();
  209. }
  210. /**
  211. * 同步设置水印字体是否粗体
  212. *
  213. * @param textBold 是否粗体(默认:false)
  214. */
  215. void setSyncTextBold(boolean textBold) {
  216. if (mSync) {
  217. setTextBold(textBold);
  218. }
  219. }
  220. /**
  221. * 设置水印X轴偏移量(单位:px)
  222. *
  223. * @param dx X轴偏移量(默认:100px)
  224. */
  225. public void setDx(int dx) {
  226. this.mDx = dx;
  227. postInvalidate();
  228. }
  229. /**
  230. * 同步设置水印X轴偏移量(单位:px)
  231. *
  232. * @param dx X轴偏移量(默认:100px)
  233. */
  234. void setSyncDx(int dx) {
  235. if (mSync) {
  236. setDx(dx);
  237. }
  238. }
  239. /**
  240. * 设置水印Y轴偏移量(单位:px)
  241. *
  242. * @param dy Y轴偏移量(默认:240px)
  243. */
  244. public void setDy(int dy) {
  245. this.mDy = dy;
  246. postInvalidate();
  247. }
  248. /**
  249. * 同步设置水印Y轴偏移量(单位:px)
  250. *
  251. * @param dy Y轴偏移量(默认:240px)
  252. */
  253. void setSignDy(int dy) {
  254. if (mSync) {
  255. setDy(dy);
  256. }
  257. }
  258. /**
  259. * 设置水印对齐方式
  260. *
  261. * @param align 对齐方式(默认:Center)
  262. */
  263. public void setAlign(Paint.Align align) {
  264. this.mAlign = align;
  265. postInvalidate();
  266. }
  267. /**
  268. * 同步设置水印对齐方式
  269. *
  270. * @param align 对齐方式(默认:Center)
  271. */
  272. void setSignAlign(Paint.Align align) {
  273. if (mSync) {
  274. setAlign(align);
  275. }
  276. }
  277. /**
  278. * 销毁相关页面时调用(切记)
  279. */
  280. public void onDestroy() {
  281. if (mSync) {
  282. WaterMarkManager.LIST.remove(this);
  283. }
  284. }
  285. @Override
  286. public boolean dispatchTouchEvent(MotionEvent event) {
  287. return false;
  288. }
  289. @SuppressLint("ClickableViewAccessibility")
  290. @Override
  291. public boolean onTouchEvent(MotionEvent event) {
  292. return false;
  293. }
  294. }

  
  1. <!--warkmark-->
  2. <declare-styleable name="WaterMarkView">
  3. <attr name="water_mark_degree" format="integer|reference" />
  4. <attr name="water_mark_text" format="string|reference" />
  5. <attr name="water_mark_textColor" format="color|reference" />
  6. <attr name="water_mark_textSize" format="dimension|reference" />
  7. <attr name="water_mark_textBold" format="boolean" />
  8. <attr name="water_mark_dx" format="dimension|reference" />
  9. <attr name="water_mark_dy" format="dimension|reference" />
  10. <attr name="water_mark_align" format="dimension">
  11. <enum name="LEFT" value="0" />
  12. <enum name="CENTER" value="1" />
  13. <enum name="RIGHT" value="2" />
  14. </attr>
  15. <attr name="water_mark_sync" format="boolean" />
  16. </declare-styleable>


  
  1. package com.hkdc.commonlib.warkmark;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.graphics.Paint;
  5. import android.view.LayoutInflater;
  6. import com.hkdc.commonlib.R;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * @author Leon (wshk729@163.com)
  11. * @date 2018/8/24
  12. * <p>
  13. */
  14. public class WaterMarkManager {
  15. static WaterMarkInfo INFO = null;
  16. static String[] CONTENT = null;
  17. static List<WaterMarkView> LIST = new ArrayList<>();
  18. /**
  19. * 设置水印全局配置信息
  20. *
  21. * @param info 配置信息
  22. */
  23. public static void setInfo(WaterMarkInfo info) {
  24. INFO = info;
  25. }
  26. /**
  27. * 获取一个满屏水印View
  28. *
  29. * @param activity activity
  30. */
  31. @SuppressLint("InflateParams")
  32. public static WaterMarkView getView(Activity activity) {
  33. return (WaterMarkView) LayoutInflater.from(activity).inflate(R.layout.view_water_mark, null);
  34. }
  35. /**
  36. * WaterMarkInfo初始化判断
  37. */
  38. private static void assertInitialized() {
  39. if (INFO == null) {
  40. INFO = WaterMarkInfo.create().generate();
  41. }
  42. }
  43. /**
  44. * 同步设置全部水印文字信息
  45. *
  46. * @param content 文字信息
  47. */
  48. public static void setText(String... content) {
  49. assertInitialized();
  50. CONTENT = content;
  51. if (LIST.size() > 0) {
  52. for (WaterMarkView view : LIST) {
  53. if (view != null) {
  54. view.setSyncText(content);
  55. }
  56. }
  57. }
  58. }
  59. /**
  60. * 同步设置全部水印倾斜角度
  61. *
  62. * @param degrees 倾斜角度(默认:-30)
  63. */
  64. public static void setDegrees(int degrees) {
  65. assertInitialized();
  66. INFO.setDegrees(degrees);
  67. if (LIST.size() > 0) {
  68. for (WaterMarkView view : LIST) {
  69. if (view != null) {
  70. view.setSyncDegrees(degrees);
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * 同步设置全部水印字体颜色
  77. *
  78. * @param textColor 字体颜色(默认:#33000000)
  79. */
  80. public static void setTextColor(int textColor) {
  81. assertInitialized();
  82. INFO.setTextColor(textColor);
  83. if (LIST.size() > 0) {
  84. for (WaterMarkView view : LIST) {
  85. if (view != null) {
  86. view.setSyncTextColor(textColor);
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * 同步设置全部水印字体大小(单位:px)
  93. *
  94. * @param textSize 字体大小(默认:42px)
  95. */
  96. public static void setTextSize(int textSize) {
  97. assertInitialized();
  98. INFO.setTextSize(textSize);
  99. if (LIST.size() > 0) {
  100. for (WaterMarkView view : LIST) {
  101. if (view != null) {
  102. view.setSyncTextSize(textSize);
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * 同步设置全部水印字体是否粗体
  109. *
  110. * @param textBold 是否粗体(默认:false)
  111. */
  112. public static void setTextBold(boolean textBold) {
  113. assertInitialized();
  114. INFO.setTextBold(textBold);
  115. if (LIST.size() > 0) {
  116. for (WaterMarkView view : LIST) {
  117. if (view != null) {
  118. view.setSyncTextBold(textBold);
  119. }
  120. }
  121. }
  122. }
  123. /**
  124. * 同步设置全部水印X轴偏移量(单位:px)
  125. *
  126. * @param dx X轴偏移量(默认:100px)
  127. */
  128. public static void setDx(int dx) {
  129. assertInitialized();
  130. INFO.setDx(dx);
  131. if (LIST.size() > 0) {
  132. for (WaterMarkView view : LIST) {
  133. if (view != null) {
  134. view.setSyncDx(dx);
  135. }
  136. }
  137. }
  138. }
  139. /**
  140. * 同步设置全部水印Y轴偏移量(单位:px)
  141. *
  142. * @param dy Y轴偏移量(默认:240px)
  143. */
  144. public static void setDy(int dy) {
  145. assertInitialized();
  146. INFO.setDy(dy);
  147. if (LIST.size() > 0) {
  148. for (WaterMarkView view : LIST) {
  149. if (view != null) {
  150. view.setSignDy(dy);
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. * 同步设置全部水印对齐方式
  157. *
  158. * @param align 对齐方式(默认:Center)
  159. */
  160. public static void setAlign(Paint.Align align) {
  161. assertInitialized();
  162. INFO.setAlign(align);
  163. if (LIST.size() > 0) {
  164. for (WaterMarkView view : LIST) {
  165. if (view != null) {
  166. view.setSignAlign(align);
  167. }
  168. }
  169. }
  170. }
  171. }

view_water_mark.xml


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <com.commonlib.WaterMarkView
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"/>


  
  1. import android.graphics.Color;
  2. import android.graphics.Paint;
  3. /**
  4. * @author Leon (wshk729@163.com)
  5. * @date 2018/8/24
  6. * <p>
  7. */
  8. public class WaterMarkInfo {
  9. private int mDegrees;
  10. private int mTextColor;
  11. private int mTextSize;
  12. private boolean mTextBold;
  13. private int mDx;
  14. private int mDy;
  15. private Paint.Align mAlign;
  16. private WaterMarkInfo(int degrees, int textColor, int textSize, boolean textBold, int dx, int dy, Paint.Align align) {
  17. mDegrees = degrees;
  18. mTextColor = textColor;
  19. mTextSize = textSize;
  20. mTextBold = textBold;
  21. mDx = dx;
  22. mDy = dy;
  23. mAlign = align;
  24. }
  25. public int getDegrees() {
  26. return mDegrees;
  27. }
  28. public int getTextColor() {
  29. return mTextColor;
  30. }
  31. public int getTextSize() {
  32. return mTextSize;
  33. }
  34. public int getDx() {
  35. return mDx;
  36. }
  37. public int getDy() {
  38. return mDy;
  39. }
  40. public Paint.Align getAlign() {
  41. return mAlign;
  42. }
  43. public int getAlignInt() {
  44. switch (mAlign) {
  45. case LEFT:
  46. return 0;
  47. case RIGHT:
  48. return 2;
  49. default:
  50. return 1;
  51. }
  52. }
  53. public boolean isTextBold() {
  54. return mTextBold;
  55. }
  56. void setDegrees(int degrees) {
  57. mDegrees = degrees;
  58. }
  59. void setTextColor(int textColor) {
  60. mTextColor = textColor;
  61. }
  62. void setTextSize(int textSize) {
  63. mTextSize = textSize;
  64. }
  65. void setTextBold(boolean textBold) {
  66. mTextBold = textBold;
  67. }
  68. void setDx(int dx) {
  69. mDx = dx;
  70. }
  71. void setDy(int dy) {
  72. mDy = dy;
  73. }
  74. void setAlign(Paint.Align align) {
  75. this.mAlign = align;
  76. }
  77. public static Builder create() {
  78. return new Builder();
  79. }
  80. public static class Builder {
  81. private int mDegrees;
  82. private int mTextColor;
  83. private int mTextSize;
  84. private boolean mTextBold;
  85. private int mDx;
  86. private int mDy;
  87. private Paint.Align mAlign;
  88. private Builder() {
  89. mDegrees = -30;
  90. mTextColor = Color.parseColor("#33000000");
  91. mTextSize = 35;
  92. mTextBold = false;
  93. mDx = 100;
  94. mDy = 240;
  95. mAlign = Paint.Align.CENTER;
  96. }
  97. /**
  98. * 设置水印文字倾斜度
  99. *
  100. * @param degrees 文字倾斜度(默认:-30)
  101. * @return Builder
  102. */
  103. public Builder setDegrees(int degrees) {
  104. mDegrees = degrees;
  105. return this;
  106. }
  107. /**
  108. * 设置水印文字颜色
  109. *
  110. * @param textColor 文字颜色(默认:#33000000)
  111. * @return Builder
  112. */
  113. public Builder setTextColor(int textColor) {
  114. mTextColor = textColor;
  115. return this;
  116. }
  117. /**
  118. * 设置水印文字大小(单位:px)
  119. *
  120. * @param textSize 文字大小(默认:42px)
  121. * @return Builder
  122. */
  123. public Builder setTextSize(int textSize) {
  124. mTextSize = textSize;
  125. return this;
  126. }
  127. /**
  128. * 设置水印文字是否加粗
  129. *
  130. * @param textBold 文字加粗(默认:false)
  131. * @return Builder
  132. */
  133. public Builder setTextBold(boolean textBold) {
  134. mTextBold = textBold;
  135. return this;
  136. }
  137. /**
  138. * 设置水印文字X轴间距(单位:px)
  139. *
  140. * @param dx 文字X轴间距(默认:100px)
  141. * @return Builder
  142. */
  143. public Builder setDx(int dx) {
  144. mDx = dx;
  145. return this;
  146. }
  147. /**
  148. * 设置水印文字Y轴间距(单位:px)
  149. *
  150. * @param dy 文字Y轴间距(默认:240px)
  151. * @return Builder
  152. */
  153. public Builder setDy(int dy) {
  154. mDy = dy;
  155. return this;
  156. }
  157. /**
  158. * 设置水印文字对齐方式
  159. *
  160. * @param align 对齐方式(默认:Center)
  161. * @return Builder
  162. */
  163. public Builder setAlign(Paint.Align align) {
  164. mAlign = align;
  165. return this;
  166. }
  167. /**
  168. * 生成水印全局配置信息
  169. *
  170. * @return 配置信息
  171. */
  172. public WaterMarkInfo generate() {
  173. return new WaterMarkInfo(mDegrees, mTextColor, mTextSize, mTextBold, mDx, mDy, mAlign);
  174. }
  175. }
  176. }

  
  1. import android.content.Context;
  2. import android.graphics.Canvas;
  3. import android.graphics.Color;
  4. import android.graphics.ColorFilter;
  5. import android.graphics.Paint;
  6. import android.graphics.PixelFormat;
  7. import android.graphics.drawable.Drawable;
  8. import android.support.annotation.IntRange;
  9. import android.support.annotation.NonNull;
  10. import android.support.annotation.Nullable;
  11. import java.util.List;
  12. public class WaterMarkBg extends Drawable {
  13. private Paint paint = new Paint();
  14. private List<String> labels;
  15. private Context context;
  16. private int degress;//角度
  17. private int fontSize;//字体大小 单位sp
  18. /**
  19. * 初始化构造
  20. * @param context 上下文
  21. * @param labels 水印文字列表 多行显示支持
  22. * @param degress 水印角度
  23. * @param fontSize 水印文字大小
  24. */
  25. public WaterMarkBg(Context context, List<String> labels, int degress, int fontSize) {
  26. this.labels = labels;
  27. this.context = context;
  28. this.degress = degress;
  29. this.fontSize = fontSize;
  30. }
  31. @Override
  32. public void draw(@NonNull Canvas canvas) {
  33. int width = getBounds().right;
  34. int height = getBounds().bottom;
  35. canvas.drawColor(Color.parseColor("#40F3F5F9"));
  36. paint.setColor(Color.parseColor("#50AEAEAE"));
  37. paint.setAntiAlias(true);
  38. paint.setTextSize(sp2px(context,fontSize));
  39. canvas.save();
  40. canvas.rotate(degress);
  41. float textWidth = paint.measureText(labels.get(0));
  42. int index = 0;
  43. for (int positionY = height / 10; positionY <= height; positionY += height / 10+80) {
  44. float fromX = -width + (index++ % 2) * textWidth;
  45. for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {
  46. int spacing = 0;//间距
  47. for(String label:labels){
  48. canvas.drawText(label, positionX, positionY+spacing, paint);
  49. spacing = spacing+50;
  50. }
  51. }
  52. }
  53. canvas.restore();
  54. }
  55. @Override
  56. public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
  57. }
  58. @Override
  59. public void setColorFilter(@Nullable ColorFilter colorFilter) {
  60. }
  61. @Override
  62. public int getOpacity() {
  63. return PixelFormat.UNKNOWN;
  64. }
  65. public static int sp2px(Context context, float spValue) {
  66. final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
  67. return (int) (spValue * fontScale + 0.5f);
  68. }
  69. }

xml中 引用


  
  1. <com.commonlib.WaterMarkView
  2. android:singleLine="false"
  3. android:id="@+id/wm"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. app:water_mark_align="CENTER"
  7. app:water_mark_degree="-30"
  8. app:water_mark_dx="100px"
  9. app:water_mark_dy="240px"
  10. app:water_mark_sync="true"
  11. app:water_mark_text="再见孙悟空"
  12. app:water_mark_textBold="false"
  13. app:water_mark_textColor="@color/black"
  14. app:water_mark_textSize="30px" />

activity中


  
  1. private WaterMarkView wm;
  2. wm = (WaterMarkView) findViewById(R.id.wm);
  3. private void water() {
  4. SharedPreferences jobcede = getSharedPreferences("jobcede", MODE_PRIVATE);
  5. String userName = jobcede.getString("username", "");
  6. String name = jobcede.getString("name", "");
  7. wm.setText(name,userName);
  8. }

核心的代码就这些,因为时间关系,完整的demo后续补充。希望对大家有所帮助。欢迎各位小伙伴加入我的qq群:开发一群:454430053 (付费)开发二群:537532956 (付费)开发三群:812695329(免费)这里已经有很多小伙伴在等你了,快来加入我们吧!

文章来源: wukong.blog.csdn.net,作者:再见孙悟空_,版权归原作者所有,如需转载,请联系作者。

原文链接:wukong.blog.csdn.net/article/details/109849305

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。