代码
有两种水印,一种是一行,一种是多行重复填满。可以添加背景图片。
public class WaterMarkBg extends Drawable {
private Paint paint = new Paint();
private List<String> labels;
/**
* 角度
*/
private int degress;
/**
* 文字大小单位px
*/
private int fontSizePx;
private int drawType = MULTI_LINE;
/**
* 多行文字间隔
*/
private int spacing = 10;
/**
* 重复画多次
*/
public final static int MULTI_LINE = 1;
/**
* 中心画一次
*/
public final static int ONE_LINE = 2;
/**
* 背景颜色
*/
private int backgroundColor = Color.rgb(255, 255, 255);
private Bitmap backgroundPic;
private Paint backGroundPaint;
public WaterMarkBg(List<String> labels, int degress, int fontSizePx) {
this.labels = labels;
this.degress = degress;
this.fontSizePx = fontSizePx;
paint.setColor(Color.argb(125, 200, 200, 200));
paint.setAntiAlias(true);
paint.setTextSize(fontSizePx);
}
public WaterMarkBg(List<String> labels, int degress, int fontSizePx, int drawType) {
this.labels = labels;
this.degress = degress;
this.fontSizePx = fontSizePx;
this.drawType = drawType;
paint.setColor(Color.argb(125, 200, 200, 200));
paint.setAntiAlias(true);
paint.setTextSize(fontSizePx);
}
@Override
public void draw(@NonNull Canvas canvas) {
int width = getBounds().right;
int height = getBounds().bottom;
canvas.drawColor(backgroundColor);
canvas.save();
switch (drawType) {
case MULTI_LINE:
drawMultiLine(canvas, width, height);
break;
case ONE_LINE:
drawOneLine(canvas, width, height);
break;
default:
}
canvas.restore();
}
private void drawMultiLine(Canvas canvas, int width, int height) {
if (backgroundPic != null) {
canvas.drawBitmap(backgroundPic, null,
new RectF(0, 0, width, height), backGroundPaint);
}
canvas.rotate(degress);
int textWidth = getMaxTextWidth();
int textHeight = getAllTextHeight();
int index = 0;
int sHeight = height / 10;
for (int positionY = sHeight; positionY <= height + sHeight; positionY += sHeight + textHeight) {
//间隔交替水印 逆时针旋转绘制范围变大-width
float fromX = -width + (index++ % 2) * textWidth;
for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {
int spacing = 0;
for (String label : labels) {
canvas.drawText(label, positionX, positionY + spacing, paint);
spacing = spacing + fontSizePx + this.spacing;
}
}
}
}
private void drawOneLine(Canvas canvas, int width, int height) {
if (backgroundPic != null) {
canvas.drawBitmap(backgroundPic, null,
new RectF(0, 0, width, height), backGroundPaint);
}
canvas.rotate(degress, width / 2, height / 2);
int textWidth = getMaxTextWidth();
int textHeight = getAllTextHeight();
//计算位置为绘制文字左上角坐标,真正绘制文字时是以第一行左下角坐标开始绘制的所以下降一行高度
int positionY = (height - textHeight) / 2 + fontSizePx;
int positionX = (width - textWidth) / 2;
int spacing = 0;
for (String label : labels) {
canvas.drawText(label, positionX, positionY + spacing, paint);
spacing = spacing + fontSizePx + this.spacing;
}
}
private int getMaxTextWidth() {
int width = 0;
for (String label : labels) {
float textWidth = paint.measureText(label);
if (width < textWidth) {
width = (int) textWidth;
}
}
return width;
}
/**
*fontSizePx近似为文字高度
*/
private int getAllTextHeight() {
return (fontSizePx + spacing) * labels.size();
}
@Override
public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
public WaterMarkBg setDrawType(int drawType) {
this.drawType = drawType;
return this;
}
public WaterMarkBg setFontColor(int color) {
paint.setColor(color);
return this;
}
public WaterMarkBg setBackgroundColor(int color) {
this.backgroundColor = color;
return this;
}
public WaterMarkBg setSpacing(int spacing) {
this.spacing = spacing;
return this;
}
public WaterMarkBg setBackground(Bitmap background) {
this.backgroundPic = background;
backGroundPaint = new Paint();
return this;
}
}
使用
SimpleDateFormat createTimeSdf1 = new SimpleDateFormat("yyyy-MM-dd");
List<String> labels = new ArrayList<>();
labels.add("水印");
labels.add(createTimeSdf1.format(new Date()));
WaterMarkBg waterMarkBg = new WaterMarkBg(labels, -30, ScreenUtils.dip2px(this, 13))
.setDrawType(WaterMarkBg.MULTI_LINE);
view.setBackground(waterMarkBg);