Android 实现水印背景效果,移动开发者社区

本文介绍了如何在Android中创建水印背景效果,包括设置文字内容、倾斜角度、字体颜色、大小、粗细以及位置偏移等方法。通过WaterMarkView类的详细方法解释,帮助移动开发者实现自定义水印功能。
摘要由CSDN通过智能技术生成

}

private void drawTexts(String[] ss, Paint paint, Canvas canvas, int x, int y) {

Paint.FontMetrics fontMetrics = paint.getFontMetrics();

float top = fontMetrics.top;

float bottom = fontMetrics.bottom;

int length = ss.length;

float total = (length - 1) * (bottom - top) + (fontMetrics.descent - fontMetrics.ascent);

float offset = total / 2 - bottom;

for (int i = 0; i < length; i++) {

float yAxis = -(length - i - 1) * (bottom - top) + offset;

canvas.drawText(ss[i], x, y + yAxis + 10, paint);

}

}

/**

  • 设置水印文字内容

  • @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);

}

}

/**

    <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值