Android实现分享长图并且添加全图水印

所属分类: 软件编程 / Android 阅读数: 33
收藏 0 赞 0 分享

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。一在这个位置点换行接向上按键就崩溃。写个博客崩溃了七八次。最近真的是运气不好。

其实我是想说:由于最近被注入了个对象,依赖性比较强,所以这段时间很少写博客了,后面补上。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多