Android 实现水印背景效果

  • @param text 文字内容

*/

public void setText(String… text) {

mText = text;

textWidth = 0;

textHeight = 0;

if (mText != null && mText.length > 0) {

for (String s : mText) {

Rect tvRect = new Rect();

mTextPaint.getTextBounds(s, 0, s.length(), tvRect);

textWidth = textWidth > tvRect.width() ? textWidth : tvRect.width();

textHeight += (tvRect.height() + 10);

}

}

postInvalidate();

}

/**

  • 同步设置水印文字内容

  • @param text 文字内容

*/

void setSyncText(String… text) {

if (mSync) {

setText(text);

}

}

/**

  • 设置水印倾斜角度

  • @param degrees 倾斜角度(默认:-30)

*/

public void setDegrees(int degrees) {

mDegrees = degrees;

postInvalidate();

}

/**

  • 同步设置水印倾斜角度

  • @param degrees 倾斜角度(默认:-30)

*/

void setSyncDegrees(int degrees) {

if (mSync) {

setDegrees(degrees);

}

}

/**

  • 设置水印字体颜色

  • @param textColor 字体颜色(默认:#33000000)

*/

public void setTextColor(int textColor) {

mTextColor = textColor;

mTextPaint.setColor(mTextColor);

postInvalidate();

}

/**

  • 同步设置水印字体颜色

  • @param textColor 字体颜色(默认:#33000000)

*/

void setSyncTextColor(int textColor) {

if (mSync) {

setTextColor(textColor);

}

}

/**

  • 设置水印字体大小(单位:px)

  • @param textSize 字体大小(默认:42px)

*/

public void setTextSize(int textSize) {

mTextSize = textSize;

mTextPaint.setTextSize(30);

postInvalidate();

}

/**

  • 同步设置水印字体大小(单位:px)

  • @param textSize 字体大小(默认:42px)

*/

void setSyncTextSize(int textSize) {

if (mSync) {

setTextSize(30);

}

}

/**

  • 设置水印字体是否粗体

  • @param textBold 是否粗体(默认:false)

*/

public void setTextBold(boolean textBold) {

mTextBold = textBold;

mTextPaint.setTypeface(mTextBold ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);

postInvalidate();

}

/**

  • 同步设置水印字体是否粗体

  • @param textBold 是否粗体(默认:false)

*/

void setSyncTextBold(boolean textBold) {

if (mSync) {

setTextBold(textBold);

}

}

/**

  • 设置水印X轴偏移量(单位:px)

  • @param dx X轴偏移量(默认:100px)

*/

public void setDx(int dx) {

this.mDx = dx;

postInvalidate();

}

/**

  • 同步设置水印X轴偏移量(单位:px)

  • @param dx X轴偏移量(默认:100px)

*/

void setSyncDx(int dx) {

if (mSync) {

setDx(dx);

}

}

/**

  • 设置水印Y轴偏移量(单位:px)

  • @param dy Y轴偏移量(默认:240px)

*/

public void setDy(int dy) {

this.mDy = dy;

postInvalidate();

}

/**

  • 同步设置水印Y轴偏移量(单位:px)

  • @param dy Y轴偏移量(默认:240px)

*/

void setSignDy(int dy) {

if (mSync) {

setDy(dy);

}

}

/**

  • 设置水印对齐方式

  • @param align 对齐方式(默认:Center)

*/

public void setAlign(Paint.Align align) {

this.mAlign = align;

postInvalidate();

}

/**

  • 同步设置水印对齐方式

  • @param align 对齐方式(默认:Center)

*/

void setSignAlign(Paint.Align align) {

if (mSync) {

setAlign(align);

}

}

/**

  • 销毁相关页面时调用(切记)

*/

public void onDestroy() {

if (mSync) {

WaterMarkManager.LIST.remove(this);

}

}

@Override

public boolean dispatchTouchEvent(MotionEvent event) {

return false;

}

@SuppressLint(“ClickableViewAccessibility”)

@Override

public boolean onTouchEvent(MotionEvent event) {

return false;

}

}

package com.hkdc.commonlib.warkmark;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.graphics.Paint;

import android.view.LayoutInflater;

import com.hkdc.commonlib.R;

import java.util.ArrayList;

import java.util.List;

/**

  • @author Leon (wshk729@163.com)

  • @date 2018/8/24

*/

public class WaterMarkManager {

static WaterMarkInfo INFO = null;

static String[] CONTENT = null;

static List LIST = new ArrayList<>();

/**

  • 设置水印全局配置信息

  • @param info 配置信息

*/

public static void setInfo(WaterMarkInfo info) {

INFO = info;

}

/**

  • 获取一个满屏水印View

  • @param activity activity

*/

@SuppressLint(“InflateParams”)

public static WaterMarkView getView(Activity activity) {

return (WaterMarkView) LayoutInflater.from(activity).inflate(R.layout.view_water_mark, null);

}

/**

  • WaterMarkInfo初始化判断

*/

private static void assertInitialized() {

if (INFO == null) {

INFO = WaterMarkInfo.create().generate();

}

}

/**

  • 同步设置全部水印文字信息

  • @param content 文字信息

*/

public static void setText(String… content) {

assertInitialized();

CONTENT = content;

if (LIST.size() > 0) {

for (WaterMarkView view : LIST) {

if (view != null) {

view.setSyncText(content);

}

}

}

}

/**

  • 同步设置全部水印倾斜角度

  • @param degrees 倾斜角度(默认:-30)

*/

public static void setDegrees(int degrees) {

assertInitialized();

INFO.setDegrees(degrees);

if (LIST.size() > 0) {

for (WaterMarkView view : LIST) {

if (view != null) {

view.setSyncDegrees(degrees);

}

}

}

}

/**

  • 同步设置全部水印字体颜色

  • @param textColor 字体颜色(默认:#33000000)

*/

public static void setTextColor(int textColor) {

assertInitialized();

INFO.setTextColor(textColor);

if (LIST.size() > 0) {

for (WaterMarkView view : LIST) {

if (view != null) {

view.setSyncTextColor(textColor);

}

}

}

}

/**

  • 同步设置全部水印字体大小(单位:px)

  • @param textSize 字体大小(默认:42px)

*/

public static void setTextSize(int textSize) {

assertInitialized();

INFO.setTextSize(textSize);

if (LIST.size() > 0) {

for (WaterMarkView view : LIST) {

if (view != null) {

view.setSyncTextSize(textSize);

}

}

}

}

/**

  • 同步设置全部水印字体是否粗体

  • @param textBold 是否粗体(默认:false)

*/

public static void setTextBold(boolean textBold) {

assertInitialized();

INFO.setTextBold(textBold);

if (LIST.size() > 0) {

for (WaterMarkView view : LIST) {

if (view != null) {

view.setSyncTextBold(textBold);

}

}

}

}

/**

  • 同步设置全部水印X轴偏移量(单位:px)

  • @param dx X轴偏移量(默认:100px)

*/

public static void setDx(int dx) {

assertInitialized();

INFO.setDx(dx);

if (LIST.size() > 0) {

for (WaterMarkView view : LIST) {

if (view != null) {

view.setSyncDx(dx);

}

}

}

}

/**

  • 同步设置全部水印Y轴偏移量(单位:px)

  • @param dy Y轴偏移量(默认:240px)

*/

public static void setDy(int dy) {

assertInitialized();

INFO.setDy(dy);

if (LIST.size() > 0) {

for (WaterMarkView view : LIST) {

if (view != null) {

view.setSignDy(dy);

}

}

}

}

/**

  • 同步设置全部水印对齐方式

  • @param align 对齐方式(默认:Center)

*/

public static void setAlign(Paint.Align align) {

assertInitialized();

INFO.setAlign(align);

if (LIST.size() > 0) {

for (WaterMarkView view : LIST) {

if (view != null) {

view.setSignAlign(align);

}

}

}

}

}

view_water_mark.xml

<?xml version="1.0" encoding="utf-8"?>

<com.commonlib.WaterMarkView

xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”/>

import android.graphics.Color;

import android.graphics.Paint;

/**

  • @author Leon (wshk729@163.com)

  • @date 2018/8/24

*/

public class WaterMarkInfo {

private int mDegrees;

private int mTextColor;

private int mTextSize;

private boolean mTextBold;

private int mDx;

private int mDy;

private Paint.Align mAlign;

private WaterMarkInfo(int degrees, int textColor, int textSize, boolean textBold, int dx, int dy, Paint.Align align) {

mDegrees = degrees;

mTextColor = textColor;

mTextSize = textSize;

mTextBold = textBold;

mDx = dx;

mDy = dy;

mAlign = align;

}

public int getDegrees() {

return mDegrees;

}

public int getTextColor() {

return mTextColor;

}

public int getTextSize() {

return mTextSize;

}

public int getDx() {

return mDx;

}

public int getDy() {

return mDy;

}

public Paint.Align getAlign() {

return mAlign;

}

public int getAlignInt() {

switch (mAlign) {

case LEFT:

return 0;

case RIGHT:

return 2;

default:

return 1;

}

}

public boolean isTextBold() {

return mTextBold;

}

void setDegrees(int degrees) {

mDegrees = degrees;

}

void setTextColor(int textColor) {

mTextColor = textColor;

}

void setTextSize(int textSize) {

mTextSize = textSize;

}

void setTextBold(boolean textBold) {

mTextBold = textBold;

}

void setDx(int dx) {

mDx = dx;

}

void setDy(int dy) {

mDy = dy;

}

void setAlign(Paint.Align align) {

this.mAlign = align;

}

public static Builder create() {

return new Builder();

}

public static class Builder {

private int mDegrees;

private int mTextColor;

private int mTextSize;

private boolean mTextBold;

private int mDx;

private int mDy;

private Paint.Align mAlign;

private Builder() {

mDegrees = -30;

mTextColor = Color.parseColor(“#33000000”);

mTextSize = 35;

mTextBold = false;

mDx = 100;

mDy = 240;

mAlign = Paint.Align.CENTER;

}

/**

  • 设置水印文字倾斜度

  • @param degrees 文字倾斜度(默认:-30)

  • @return Builder

*/

public Builder setDegrees(int degrees) {

mDegrees = degrees;

return this;

}

/**

  • 设置水印文字颜色

  • @param textColor 文字颜色(默认:#33000000)

  • @return Builder

*/

public Builder setTextColor(int textColor) {

mTextColor = textColor;

return this;

}

/**

  • 设置水印文字大小(单位:px)

  • @param textSize 文字大小(默认:42px)

  • @return Builder

*/

public Builder setTextSize(int textSize) {

mTextSize = textSize;

return this;

}

/**

  • 设置水印文字是否加粗

  • @param textBold 文字加粗(默认:false)

  • @return Builder

*/

public Builder setTextBold(boolean textBold) {

mTextBold = textBold;

return this;

}

/**

  • 设置水印文字X轴间距(单位:px)

  • @param dx 文字X轴间距(默认:100px)

  • @return Builder

*/

public Builder setDx(int dx) {

mDx = dx;

return this;

}

/**

  • 设置水印文字Y轴间距(单位:px)

  • @param dy 文字Y轴间距(默认:240px)

  • @return Builder

*/

public Builder setDy(int dy) {

mDy = dy;

return this;

}

最后

代码真的是重质不重量,质量高的代码,是当前代码界提倡的,当然写出高质量的代码肯定需要一个相当高的专业素养,这需要在日常的代码书写中逐渐去吸收掌握,谁不是每天都在学习呀,目的还不是为了一个,为实现某个功能写出高质量的代码。

所以,长征路还长,大家还是好好地做个务实的程序员吧。

最后,小编这里有一系列Android提升学习资料,有兴趣的小伙伴们可以来看下哦~
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 26
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中实现照片水印效果可以使用Java的Graphics2D类来实现。Graphics2D类是Java 2D API中的一个类,它提供了各种绘制2D图形的方法。 下面是一个简单的Java程序,演示如何在图片上添加水印: ```java import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class Watermark { public static void main(String[] args) throws Exception { // 读取原始图片 BufferedImage sourceImage = ImageIO.read(new File("source.jpg")); // 创建Graphics2D对象 Graphics2D g2d = sourceImage.createGraphics(); // 设置水印文字 String watermarkText = "Watermark"; // 设置水印字体 Font font = new Font("Arial", Font.BOLD, 24); g2d.setFont(font); // 设置水印颜色 g2d.setColor(Color.RED); // 设置水印位置 int x = sourceImage.getWidth() - 150; int y = sourceImage.getHeight() - 50; // 添加水印 g2d.drawString(watermarkText, x, y); // 释放资源 g2d.dispose(); // 输出图片 ImageIO.write(sourceImage, "jpg", new File("watermarked.jpg")); } } ``` 在这个程序中,我们首先使用`ImageIO.read`方法读取原始图片。然后,我们创建一个`Graphics2D`对象,该对象是通过调用`createGraphics`方法从原始图片中创建的。接下来,我们设置水印文字、字体、颜色和位置,并使用`drawString`方法将水印添加到图片中。最后,我们释放`Graphics2D`对象,并使用`ImageIO.write`方法将带有水印的图片输出到文件中。 注意:在实际应用中,为了避免原始图片被覆盖,应该将带有水印的图片输出到另一个文件中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值