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

Android實(shí)現(xiàn)分享長圖并且添加全圖水印

 更新時(shí)間:2017年03月27日 09:50:00   投稿:lqh  
這篇文章主要介紹了Android實(shí)現(xiàn)分享長圖并且添加全圖水印的相關(guān)資料,需要的朋友可以參考下

Android實(shí)現(xiàn)分享長圖并且添加全圖水印

前言:

長圖一般是ScrollView和ListView。

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

分別貼出兩個(gè)控件的長圖獲取方法

 /**
   * 截取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();
    }
    // 創(chuàng)建對(duì)應(yīng)大小的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實(shí)際高度
    for (int i = 0; i < listView.getChildCount(); i++) {
      h += listView.getChildAt(i).getHeight();
    }
listView.getHeight());
    // 創(chuàng)建對(duì)應(yīng)大小的bitmap
    bitmap = Bitmap.createBitmap(listView.getWidth(), h,
        Bitmap.Config.RGB_565);
    final Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.WHITE);
    listView.draw(canvas);
    // 測(cè)試輸出
    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;
  }

奉送個(gè)獲取具體view的顯示圖的方法

  /**
   * 生成某個(gè)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;
  }

再奉送個(gè)生成某個(gè)LinearLayout圖片的方法

/**
   * 生成某個(gè)LinearLayout的圖片
   *
   * @author gengqiquan
   * @date 2017/3/20 上午10:34
   */
  public static Bitmap getLinearLayoutBitmap(LinearLayout linearLayout) {
    int h = 0;
    // 獲取LinearLayout實(shí)際高度
    for (int i = 0; i < linearLayout.getChildCount(); i++) {
      linearLayout.getChildAt(i).measure(0, 0);
      h += linearLayout.getChildAt(i).getMeasuredHeight();
    }
    linearLayout.measure(0, 0);
    // 創(chuàng)建對(duì)應(yīng)大小的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;
  }

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

/**
*拼接圖片
 * @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;
  }

再來個(gè)添加全圖水印的方法

/**
   * @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;
  }

其實(shí)我是想說:由于最近被注入了個(gè)對(duì)象,依賴性比較強(qiáng),所以這段時(shí)間很少寫博客了,后面補(bǔ)上。

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

相關(guān)文章

最新評(píng)論