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

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

  • 首先上截屏功能代码:去除状态栏后的 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适配

至此记录!!!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容