Bitmap bmp = BitmapFactory.decodeFile(path).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setDither(true);
paint.setFilterBitmap(true);
int width = bmp.getWidth();
int height = bmp.getHeight();
Rect src = new Rect(0, 0, width, height);
Rect dst = new Rect(0, 0, width, height);
canvas.drawBitmap(bmp, src, dst, paint);
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
textPaint.setAntiAlias(true);
textPaint.setTextSize(DensityUtil.sp2px(context,16));
Typeface typeface1 = Typeface.create("微软雅黑", Typeface.NORMAL);
// 采用默认的宽度
textPaint.setTypeface(typeface1);
textPaint.setColor(Color.WHITE);
textPaint.setTextAlign(Paint.Align.LEFT);
textPaint.clearShadowLayer();
Paint backPaint = new Paint();
backPaint.setColor(context.getResources().getColor(R.color.toast_background_frame));
canvas.drawRect(DensityUtil.dip2px(context, 8),height-DensityUtil.dip2px(context, 38),(msg.length()*DensityUtil.sp2px(context, 16))+DensityUtil.dip2px(context, 8),height-DensityUtil.dip2px(context, 8),backPaint);
canvas.drawText(msg, DensityUtil.dip2px(context, 10), height-DensityUtil.dip2px(context, 18), textPaint);
canvas.save();
canvas.restore();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (!bmp.isRecycled()) {
bmp.recycle();
}
return path;
实现效果在图片底部生成白色文字水印并带有阴影