国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現分享長圖并且添加全圖水印

2019-12-12 03:16:43
字體:
來源:轉載
供稿:網友

Android實現分享長圖并且添加全圖水印

前言:

長圖一般是ScrollView和ListView。

我們需要取得這兩個控件的完整顯示的圖片。原理很簡單,搞一張和控件長寬一致的畫布(就是創建一個高寬相等的bitmap)。然后調用控件的draw方法把自己畫到畫布上去。

分別貼出兩個控件的長圖獲取方法

 /**   * 截取scrollview的屏幕   **/  public static Bitmap getScrollViewBitmap(ScrollView scrollView) {    int h = 0;    Bitmap bitmap;    for (int i = 0; i < scrollView.getChildCount(); i++) {      h += scrollView.getChildAt(i).getHeight();    }    // 創建對應大小的bitmap    bitmap = Bitmap.createBitmap(ScreenUtils.getScreenWidth(scrollView.getContext()), h,        Bitmap.Config.ARGB_4444);    final Canvas canvas = new Canvas(bitmap);    canvas.drawColor(Color.parseColor("#f2f7fa"));    scrollView.draw(canvas);    return bitmap;  }
 /**   * 截圖listview   **/  public static Bitmap getListViewBitmap(ListView listView, String picpath) {    int h = 0;    Bitmap bitmap;    // 獲取listView實際高度    for (int i = 0; i < listView.getChildCount(); i++) {      h += listView.getChildAt(i).getHeight();    }listView.getHeight());    // 創建對應大小的bitmap    bitmap = Bitmap.createBitmap(listView.getWidth(), h,        Bitmap.Config.RGB_565);    final Canvas canvas = new Canvas(bitmap);    canvas.drawColor(Color.WHITE);    listView.draw(canvas);    // 測試輸出    FileOutputStream out = null;    try {      out = new FileOutputStream(picpath);    } catch (FileNotFoundException e) {      e.printStackTrace();    }    try {      if (null != out) {        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);        out.flush();        out.close();      }    } catch (IOException e) {    }    return bitmap;  }

奉送個獲取具體view的顯示圖的方法

  /**   * 生成某個view的圖片   *   * @author gengqiquan   * @date 2017/3/20 上午10:34   */  public static Bitmap getViewDrawingCacheBitmap(View view) {    view = view.getRootView();    if (!view.isDrawingCacheEnabled()) {      view.setDrawingCacheEnabled(true);    }    view.destroyDrawingCache();    view.buildDrawingCache();    Bitmap bm = view.getDrawingCache();    view.setDrawingCacheEnabled(false);    return bm;  }

再奉送個生成某個LinearLayout圖片的方法

/**   * 生成某個LinearLayout的圖片   *   * @author gengqiquan   * @date 2017/3/20 上午10:34   */  public static Bitmap getLinearLayoutBitmap(LinearLayout linearLayout) {    int h = 0;    // 獲取LinearLayout實際高度    for (int i = 0; i < linearLayout.getChildCount(); i++) {      linearLayout.getChildAt(i).measure(0, 0);      h += linearLayout.getChildAt(i).getMeasuredHeight();    }    linearLayout.measure(0, 0);    // 創建對應大小的bitmap    Bitmap bitmap = Bitmap.createBitmap(linearLayout.getMeasuredWidth(), h,        Bitmap.Config.RGB_565);    final Canvas canvas = new Canvas(bitmap);    canvas.drawColor(Color.WHITE);    linearLayout.draw(canvas);    return bitmap;  }

完了產品肯定會讓你在下面或者上面加上公司的logo圖片的,嗯。好人做到低,再送個拼接圖片的方法

/***拼接圖片 * @param first 分享的長圖 * @param second 公司logo圖*@author gengqiquan*@date 2017/3/25 下午4:56*/  public static Bitmap add2Bitmap(Bitmap first, Bitmap second) {    float scale = ((float) first.getWidth()) / second.getWidth();    second = ImageUtil.scaleImg(second, scale);    int width = first.getWidth();    int height = first.getHeight() + second.getHeight();    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);    Canvas canvas = new Canvas(result);    canvas.drawBitmap(first, 0, 0, null);    canvas.drawBitmap(second, 0, first.getHeight(), null);    return result;  }

再來個添加全圖水印的方法

/**   * @param first 原始圖   * @param mark 水印圖   * @author gengqiquan   * @date 2017/3/25 下午4:58   */  public static Bitmap waterMark(Bitmap first, Bitmap mark) {    float scale = ((float) first.getWidth()) / mark.getWidth();    mark = ImageUtil.scaleImg(mark, scale);    int width = first.getWidth();    int height = first.getHeight();    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);    Canvas canvas = new Canvas(result);    canvas.drawBitmap(first, 0, 0, null);    int h = 0;    while (h < height + mark.getHeight()) {      canvas.drawBitmap(mark, 0, h, null);      h = h + mark.getHeight();    }    return result;  }

坑爹的chrome有bug。一在這個位置點換行接向上按鍵就崩潰。寫個博客崩潰了七八次。最近真的是運氣不好。

其實我是想說:由于最近被注入了個對象,依賴性比較強,所以這段時間很少寫博客了,后面補上。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开江县| 若尔盖县| 民和| 青龙| 仪征市| 凤冈县| 亳州市| 游戏| 中卫市| 汝阳县| 拉萨市| 西乌珠穆沁旗| 崇义县| 伊通| 怀化市| 封开县| 资源县| 博白县| 庄河市| 凤翔县| 娱乐| 兴业县| 沽源县| 南陵县| 玉门市| 类乌齐县| 秭归县| 鹤岗市| 扬州市| 新晃| 长海县| 万载县| 资阳市| 陵水| 甘南县| 永寿县| 子洲县| 左权县| 化德县| 浦城县| 甘谷县|