Android 水印工具类

Android 水印工具

代码

有两种水印,一种是一行,一种是多行重复填满。可以添加背景图片。

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);
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值