美文网首页
关于小米手机中bitmap图片拼接黑边问题(截屏拼接二维码分享)

关于小米手机中bitmap图片拼接黑边问题(截屏拼接二维码分享)

作者: 则卷滔滔 | 来源:发表于2019-03-29 15:09 被阅读0次

需求是截屏 然后拼接上二维码进行分享操作。这里设计到的有截屏功能和拼接图片功能。

  • 首先上截屏功能代码:去除状态栏后的 topHeight距离顶部高度 bottomHeight距离底部高度
    public static Bitmap screenShot(Activity activity, int topHeight, int bottomHeight) {

      // 获取windows中最顶层的view
      View view = activity.getWindow().getDecorView();
      view.buildDrawingCache();
      // 获取状态栏高度
      Rect rect = new Rect();
      view.getWindowVisibleDisplayFrame(rect);
      int statusBarHeights = rect.top + topHeight;
      DisplayMetrics metric = new DisplayMetrics();
      activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
      // 获取屏幕宽和高
      int widths = metric.widthPixels;
      int heights = metric.heightPixels;
      // 允许当前窗口保存缓存信息
      view.setDrawingCacheEnabled(true);
      // 去掉状态栏
      Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeights, widths, heights - statusBarHeights - bottomHeight);
      // 销毁缓存信息
      view.destroyDrawingCache();
      return bmp;
    

    }

很多反应说可能为null的情况 但是我是没有碰到。

  • 第二步 把不在屏幕上的布局转换成butmap
    1.把布局加载出来然后测量大小
    public static Bitmap createCodeBitmap(Activity context) {

      DisplayMetrics metric = new DisplayMetrics();
      context.getWindowManager().getDefaultDisplay().getMetrics(metric);
      int width = metric.widthPixels;
      View view = LayoutInflater.from(context).inflate(R.layout.item_share_k, null, false);
      LinearLayout ll = view.findViewById(R.id.itemShareK);
      layoutView(view, width, StringUtils.dip2px(context, 84f));
      return loadBitmapFromView(ll);
    

    }

2.把测量好的布局通过canvas画布转变成bitmap
public static Bitmap loadBitmapFromView(View v) {

    int w = v.getWidth();
    int h = v.getHeight();

    Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmp);

    c.drawColor(Color.WHITE);
    /** 如果不设置canvas画布为白色,则生成透明 */

    v.layout(0, 0, w, h);
    v.draw(c);

    return bmp;
}
  • 第三部把两个bitmap拼接
    1.网上已经有很多资料了大多数都是根据长宽,使用新建一个bitmap
    Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    方法来进行横向和竖向拼接来
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(first, 0, 0, null);
    canvas.drawBitmap(second, first.getHeight(), 0, null);
    一个新的bitmap
    2.这种方法对于大部分手机是可行的,但是对于小米的刘海屏手机(mi 8)会出现右边和下边的边框 具体原因不知 可能和小米获取屏幕高度的时候是自动去除掉底部操作栏的不知道是处于什么考虑 后来经过一番查找 找到了另外一种设置bitmap的方法成功适配
    /**
    • 把两个位图覆盖合成为一个位图,上下拼接
    • @param isBaseMax 是否以高度大的位图为准,true则小图等比拉伸,false则大图等比压缩
    • @return
      */
      public static Bitmap composeCodeBitmap(Bitmap topBitmap, Bitmap bottomBitmap, boolean isBaseMax) {
    if (topBitmap == null || topBitmap.isRecycled()
            || bottomBitmap == null || bottomBitmap.isRecycled()) {
        return null;
    }
    int width = 0;
    if (isBaseMax) {
        width = topBitmap.getWidth() > bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
    } else {
        width = topBitmap.getWidth() < bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
    }
    Bitmap tempBitmapT = topBitmap;
    Bitmap tempBitmapB = bottomBitmap;

    if (topBitmap.getWidth() != width) {
        tempBitmapT = Bitmap.createScaledBitmap(topBitmap, width, (int) (topBitmap.getHeight() * 1f / topBitmap.getWidth() * width), false);
    } else if (bottomBitmap.getWidth() != width) {
        tempBitmapB = Bitmap.createScaledBitmap(bottomBitmap, width, (int) (bottomBitmap.getHeight() * 1f / bottomBitmap.getWidth() * width), false);
    }

    int height = tempBitmapT.getHeight() + tempBitmapB.getHeight();

    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Rect topRect = new Rect(0, 0, tempBitmapT.getWidth(), tempBitmapT.getHeight());
    Rect bottomRect = new Rect(0, 0, tempBitmapB.getWidth(), tempBitmapB.getHeight());

    Rect bottomRectT = new Rect(0, tempBitmapT.getHeight(), width, height);

    canvas.drawBitmap(tempBitmapT, topRect, topRect, null);
    canvas.drawBitmap(tempBitmapB, bottomRect, bottomRectT, null);
    return bitmap;
}

通过Rect来确定区域 然后再来生成bitmap适配

至此记录!!!

相关文章

  • 关于小米手机中bitmap图片拼接黑边问题(截屏拼接二维码分享)

    需求是截屏 然后拼接上二维码进行分享操作。这里设计到的有截屏功能和拼接图片功能。 首先上截屏功能代码:去除状态栏后...

  • 2020-04-01

    关于小米mix2及部分大屏手机图片拼接(拼合)出现白色线条的问题! 问题描述:在做前端开发时,经常会遇到需要将多张...

  • 关于小米mix2及部分大屏手机图片拼接(拼合)出现白色线条的问题

    关于小米mix2及部分大屏手机图片拼接(拼合)出现白色线条的问题! 问题描述:在做前端开发时,经常会遇到需要将多张...

  • 截屏、图片裁剪、拼接

    我们在开发中经常需要使用截屏功能或者把某个view生成一张图片的功能,还有可能需要拼接在一起组成一张大图,另外有可...

  • RN 中截屏react-native-view-shot

    应用场景: 对应用中的某一个界面进行截屏并保存(Android 和 IOS) (特殊情况是对该截屏进行图片拼接,本...

  • 手机、平板无线投屏液晶拼接大屏

    手机、平板无线投屏液晶拼接大屏 今天跟大家介绍一种无线投屏屏幕拼接方案,主要应用在拼接屏的多屏显示上。拼接屏本身就...

  • 2018-03-22

    每天把日程安排以图片的方式发上来比较费事,手机截屏并不能显示当天全部日程,分两次截屏就需要把图片拼接起来,增加了工...

  • 安康液晶拼接屏价格

    想要理解安康液晶拼接屏价格,先来认识下液晶拼接屏概述: 液晶拼接屏别称液晶拼接大屏、拼接屏、落地款液晶拼接屏等。可...

  • 安徽液晶拼接屏厂家经销商大全

    安徽查找液晶拼接屏厂家经销商大全,建议深圳宇扬液晶拼接屏,主营各种异形液晶拼接屏、液晶拼接屏、拼接屏、工业液晶拼接...

  • 湘潭液晶拼接屏供应商

    在湘潭打听液晶拼接屏供应商,建议深圳宇扬液晶拼接屏,主营各种工业液晶拼接屏、拼接大屏、异形液晶拼接屏、视频拼接墙等...

网友评论

      本文标题:关于小米手机中bitmap图片拼接黑边问题(截屏拼接二维码分享)

      本文链接:https://www.haomeiwen.com/subject/pzflbqtx.html