加水印代码
public Bitmap addWaterMark(Bitmap src, String water, Context context){
Bitmap tarBitmap = src.copy(Config.ARGB_8888, true);
int w = tarBitmap.getWidth();
int h = tarBitmap.getHeight();
Canvas canvas = new Canvas(tarBitmap);
//启用抗锯齿和使用设备的文本字距
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
//字体的相关设置
textPaint.setTextSize(35.0f);//字体大小
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setColor(Color.BLACK);
textPaint.setShadowLayer(3f, 1, 1,context.getResources().getColor(android.R.color.background_dark));
//图片上添加水印的位置,这里设置的是中下部3/4处
canvas.drawText(water, (float)(w*0.05), (float) (h*0.9), textPaint);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return tarBitmap;
}
水印代码换行
事实上对于canvas.drawText()是不能实现自动换行的。
在string参数里加入/r/n,都是无效的,所以同TextPaint类
具体代码如下:
public Bitmap addWaterMark1(Bitmap src, String water, Context context){
Bitmap tarBitmap = src.copy(Config.ARGB_8888, true);
int w = tarBitmap.getWidth();
int h = tarBitmap.getHeight();
Canvas canvas = new Canvas(tarBitmap);
//启用抗锯齿和使用设备的文本字距
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
//字体的相关设置
textPaint.setTextSize(35.0f);//字体大小
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setColor(Color.BLACK);
textPaint.setShadowLayer(3f, 1, 1,context.getResources().getColor(android.R.color.background_dark));
StaticLayout layout = new StaticLayout(water, textPaint, 500,
Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
canvas.save();
canvas.translate((float)(w*0.05), (float) (h*0.9));//从20,20开始画
layout.draw(canvas);
canvas.save(Canvas.ALL_SAVE_FLAG);
return tarBitmap;
}
其中StaticLayout中的参数500,是来设置多长之后 开始换行。
时间: 2024-10-08 01:41:30