需求是截屏 然后拼接上二維碼進(jìn)行分享操作。這里設(shè)計(jì)到的有截屏功能和拼接圖片功能葫掉。
-
首先上截屏功能代碼:去除狀態(tài)欄后的 topHeight距離頂部高度 bottomHeight距離底部高度
public static Bitmap screenShot(Activity activity, int topHeight, int bottomHeight) {// 獲取windows中最頂層的view View view = activity.getWindow().getDecorView(); view.buildDrawingCache(); // 獲取狀態(tài)欄高度 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; // 允許當(dāng)前窗口保存緩存信息 view.setDrawingCacheEnabled(true); // 去掉狀態(tài)欄 Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeights, widths, heights - statusBarHeights - bottomHeight); // 銷毀緩存信息 view.destroyDrawingCache(); return bmp;
}
很多反應(yīng)說(shuō)可能為null的情況 但是我是沒(méi)有碰到。
-
第二步 把不在屏幕上的布局轉(zhuǎn)換成butmap
1.把布局加載出來(lái)然后測(cè)量大小
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.把測(cè)量好的布局通過(guò)canvas畫布轉(zhuǎn)變成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);
/** 如果不設(shè)置canvas畫布為白色留美,則生成透明 */
v.layout(0, 0, w, h);
v.draw(c);
return bmp;
}
- 第三部把兩個(gè)bitmap拼接
1.網(wǎng)上已經(jīng)有很多資料了大多數(shù)都是根據(jù)長(zhǎng)寬竿音,使用新建一個(gè)bitmap
Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
方法來(lái)進(jìn)行橫向和豎向拼接來(lái)
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
canvas.drawBitmap(second, first.getHeight(), 0, null);
一個(gè)新的bitmap
2.這種方法對(duì)于大部分手機(jī)是可行的冯键,但是對(duì)于小米的劉海屏手機(jī)(mi 8)會(huì)出現(xiàn)右邊和下邊的邊框 具體原因不知 可能和小米獲取屏幕高度的時(shí)候是自動(dòng)去除掉底部操作欄的不知道是處于什么考慮 后來(lái)經(jīng)過(guò)一番查找 找到了另外一種設(shè)置bitmap的方法成功適配
/**- 把兩個(gè)位圖覆蓋合成為一個(gè)位圖菲语,上下拼接
- @param isBaseMax 是否以高度大的位圖為準(zhǔn)妄辩,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;
}
通過(guò)Rect來(lái)確定區(qū)域 然后再來(lái)生成bitmap適配
至此記錄I缴稀Q垡!