先看效果图,左侧是原图,中间是添加文字水印,右侧是添加图片水印。
实现方法:
1、载入原始图片
2、载入水印图片
3、保存带有水印的图片
实现原理:
获取原始图片的宽高,然后,新建一个同样宽高的bitmap,将这个新的bitmap作为画布,接着,就在这个画布上面画原图,画水印图片,有文字就接着画文字。
工具类:
public class ImageUtil {
/**
* 设置水印图片在左上角
*
* @param src
* @param watermark
* @param paddingLeft
* @param paddingTop
* @return
*/
public static Bitmap createWaterMaskLeftTop(Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) {
return createWaterMaskBitmap(src, watermark, dp2px(paddingLeft), dp2px(paddingTop));
}
/**
* 设置水印图片在右下角
*
* @param src
* @param watermark
* @param paddingRight
* @param paddingBottom
* @return
*/
public static Bitmap createWaterMaskRightBottom(Bitmap src, Bitmap watermark, int paddingRight, int paddingBottom) {
return createWaterMaskBitmap(src, watermark, src.getWidth() - watermark.getWidth() - dp2px(paddingRight), src.getHeight() - watermark.getHeight() - dp2px(paddingBottom));
}
/**
* 设置水印图片到右上角
*
* @param src
* @param watermark
* @param paddingRight
* @param paddingTop
* @return
*/
public static Bitmap createWaterMaskRightTop(Bitmap src, Bitmap watermark, int paddingRight, int paddingTop) {
return createWaterMaskBitmap(src, watermark, src.getWidth() - watermark.getWidth() - dp2px(paddingRight), dp2px(paddingTop));
}
/**
* 设置水印图片到左下角
*
* @param src
* @param watermark
* @param paddingLeft
* @param paddingBottom
* @return
*/
public static Bitmap createWaterMaskLeftBottom(Bitmap src, Bitmap watermark, int paddingLeft, int paddingBottom) {
return createWaterMaskBitmap(src, watermark, dp2px(paddingLeft), src.getHeight() - watermark.getHeight() - dp2px(paddingBottom));
}
/**
* 设置水印图片到中间
*
* @param src
* @param watermark
* @return
*/
public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
return createWaterMaskBitmap(src, watermark, (src.getWidth() - watermark.getWidth()) / 2, (src.getHeight() - watermark.getHeight()) / 2);
}
private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) {
if (src == null) {
return null;
}
int width = src.getWidth();
int height = src.getHeight();
//创建一个bitmap
Bitmap newb = Bitmap.createBitmap(width, height, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
//将该图片作为画布
Canvas canvas = new Canvas(newb);
//在画布 0,0坐标上开始绘制原始图片
canvas.drawBitmap(src, 0, 0, null);
//在画布上绘制水印图片
canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
// 保存
canvas.save(Canvas.ALL_SAVE_FLAG);
// 存储
canvas.restore();
return newb;
}
/**
* 将View转成Bitmap
*
* @param view
* @return
*/
public static Bitmap viewToBitmap(View view) {
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
/**
* dp转pix
*
* @param dp
* @return
*/
public static int dp2px(float dp) {
final float scale = Resources.getSystem().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
}
工具类使用方法Demo代码:
添加一个布局文件activity_water_mask
Activity代码
public class WaterMaskActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water_mask);
initView();
}
private void initView() {
ImageView mSourImage = (ImageView) findViewById(R.id.img);
//获取原始图片
Bitmap sourBitmap = ((BitmapDrawable) mSourImage.getDrawable()).getBitmap();
//水印图片
Bitmap waterBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Bitmap watermarkBitmap = ImageUtil.createWaterMaskCenter(sourBitmap, waterBitmap);
watermarkBitmap = ImageUtil.createWaterMaskLeftBottom(watermarkBitmap, waterBitmap, 0, 0);
watermarkBitmap = ImageUtil.createWaterMaskRightBottom(watermarkBitmap, waterBitmap, 0, 0);
watermarkBitmap = ImageUtil.createWaterMaskLeftTop(watermarkBitmap, waterBitmap, 0, 0);
watermarkBitmap = ImageUtil.createWaterMaskRightTop(watermarkBitmap, waterBitmap, 0, 0);
mSourImage.setImageBitmap(watermarkBitmap);
}
}
Demo中只写了添加图片水印,怎么实现文字水印呢?在工具类中有一个方法viewToBitmap将View转成Bitmap,我的实现思路是将TextView转化为Bitmap,然后实现方法就和添加图片水印一样。