gdiplus 水印_给图片添加水印字体 - whitemine的个人空间 - OSCHINA - 中文开源技术交流社区...

该博客介绍了如何利用GDI+库在Android中为图片添加文字水印。通过缩放图片适应屏幕尺寸,创建Bitmap对象,然后在Canvas上使用Paint设置抗锯齿和过滤效果,绘制多行文字水印,调整每行文字的位置。最后回收源Bitmap,返回带有水印的新Bitmap。
摘要由CSDN通过智能技术生成

public static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String... strings) {

//根据图片大小和屏幕大小将图片缩放

DisplayMetrics metrics = context.getResources().getDisplayMetrics();

int screenWidth = metrics.widthPixels;

int screenHeight = metrics.heightPixels;

int bitmapWidth = bitmap.getWidth();

int bitmapHeight = bitmap.getHeight();

Matrix matrix = new Matrix();

float widthScale = (float)screenWidth/bitmapWidth; //一定要加上float不然createBitmap的时候会报错

float heightScale = (float)screenHeight/bitmapHeight;

matrix.postScale(widthScale, heightScale);

Bitmap src = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

Bitmap result = src.copy(Bitmap.Config.ARGB_8888, true);

Canvas canvas = new Canvas(result);

Paint paint = new Paint();

paint.setAntiAlias(true);

paint.setFilterBitmap(true);

paint.setTextSize(40);

paint.setColor(Color.WHITE);

int textHeight = 0;

for (int i=0; i

String str = strings[i];

Rect bounds = new Rect();

paint.getTextBounds(str, 0, str.length(), bounds);

if (i == 0) {

textHeight = bounds.height();

}

canvas.drawText(str, 20, result.getHeight() - textHeight*(i+1) - 40*(i+1), paint);

}

canvas.save();

canvas.restore();

bitmap.recycle();

return result;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值