欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android添加水印的正確方法 只要三步!

 更新時(shí)間:2020年04月18日 10:36:18   作者:柒號(hào)公園  
這篇文章主要介紹了Android添加水印的正確方法,僅僅三步輕松實(shí)現(xiàn)為圖片添加水印功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

開門見山,添加水印的方法非常簡(jiǎn)單,其實(shí)就只有3個(gè)步驟

1、載入原始圖片
2、載入水印圖片
3、保存帶有水印的圖片

實(shí)現(xiàn)的原理就是:獲取原始圖片的寬高,然后,新建一個(gè)同樣寬高的bitmap,將這個(gè)新的bitmap作為畫布,接著,就在這個(gè)畫布上面畫原圖,畫水印圖片,有文字就接著畫文字。
上面哪個(gè)順序一定不能亂,不然你可能就看不到水印,或則文字了,因?yàn)楫嬙谠瓐D下面去了

繪制水印的代碼如下:

 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();
  //創(chuàng)建一個(gè)bitmap
  Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);// 創(chuàng)建一個(gè)新的和SRC長(zhǎng)度寬度一樣的位圖
  //將該圖片作為畫布
  Canvas canvas = new Canvas(newBitmap);
  //在畫布 0,0坐標(biāo)上開始繪制原始圖片
  canvas.drawBitmap(src, 0, 0, null);
  //在畫布上繪制水印圖片
  canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
  // 保存
  canvas.save(Canvas.ALL_SAVE_FLAG);
  // 存儲(chǔ)
  canvas.restore();
  return newBitmap;
 }

繪制文字的代碼如下:

/**
  * 繪制文字到中間
  *
  * @param context
  * @param bitmap
  * @param text
  * @param size
  * @param color
  * @return
  */
 public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,
           int size, int color) {
  Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  paint.setColor(color);
  paint.setTextSize(dp2px(context, size));
  Rect bounds = new Rect();
  paint.getTextBounds(text, 0, text.length(), bounds);
  return drawTextToBitmap(context, bitmap, text, paint, bounds,
    (bitmap.getWidth() - bounds.width()) / 2,
    (bitmap.getHeight() + bounds.height()) / 2);
 }

 /**
  * 圖片上繪制文字
  */
 private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,
           Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
  Config bitmapConfig = bitmap.getConfig();

  paint.setDither(true); // 獲取跟清晰的圖像采樣
  paint.setFilterBitmap(true);// 過(guò)濾一些
  if (bitmapConfig == null) {
   bitmapConfig = Config.ARGB_8888;
  }
  bitmap = bitmap.copy(bitmapConfig, true);
  Canvas canvas = new Canvas(bitmap);

  canvas.drawText(text, paddingLeft, paddingTop, paint);
  return bitmap;
 }

效果圖如下:

github地址為:https://github.com/chenguo4930/Watermark
git地址為:https://github.com/chenguo4930/Watermark.git

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論