广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现分享长图并且添加全图水印
  • 925
分享到

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

Android 2022-06-06 01:06:49 925人浏览 泡泡鱼
摘要

Android实现分享长图并且添加全图水印 前言: 长图一般是ScrollView和ListView。 我们需要取得这两个控件的完整显示的图片。原理很简单,搞一张和控件长宽

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

前言:

长图一般是ScrollView和ListView。

我们需要取得这两个控件的完整显示的图片。原理很简单,搞一张和控件长宽一致的画布(就是创建一个高宽相等的bitmap)。然后调用控件的draw方法把自己画到画布上去。

分别贴出两个控件的长图获取方法


 
  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;
  }

 
  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的显示图的方法


  
  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图片的方法



  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图片的,嗯。好人做到低,再送个拼接图片的方法



  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;
  }

再来个添加全图水印的方法



  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给任何view添加全屏倾斜水印Android 给图片加上水印的示例代码(支持logo+文字)Android 图片添加水印的实现方法Android给图片加文字和图片水印实例代码android使用ItemDecoration给RecyclerView 添加水印Android实现为图片添加水印Android添加水印的正确方法 只要三步!Android视频处理之动态时间水印效果android实现文字水印效果 支持多行水印


--结束END--

本文标题: Android实现分享长图并且添加全图水印

本文链接: https://www.lsjlt.com/news/21966.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作