需求是截屏 然后拼接上二维码进行分享操作。这里设计到的有截屏功能和拼接图片功能。
-
首先上截屏功能代码:去除状态栏后的 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适配
至此记录!!!