有時候分享功能都是很需要分享一個當前屏幕的界面的截圖因,以前做校內APP的時候用到過,拿出來分享分享, 用以前寫過的自定義課表軟件。
Android 自定義View課程表表格

看到的是圖片只顯示到11節處,下面的沒有顯示到 所以用到的 ScrollView
因此截圖節截取ScrollView View的圖片
一、首先計算出整個ScrollView 的高度寬度生成對應大小的的Bitmap 然后把使用Canvas 將ScrollView 的界面繪制上去
// 獲取ScrollView 實際高度
h = 0;
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundResource(android.R.color.white);
}
// 創建對應大小的bitmap
Bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
二、獲取分享的頭部和底部圖片的Bitmap
// BitmapFactory.decodeResource函數直接轉換資源文件為Bitmap Bitmap head = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.share_term_table_header); Bitmap foot = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.share_term_table_footer);
三、合并頭部底部和界面View的截圖
if (head == null) {
return null;
}
int headWidth = head.getWidth();
int kebianwidth = kebiao.getWidth();
int fotwid = san.getWidth();
int headHeight = head.getHeight();
int kebiaoheight = kebiao.getHeight();
int footerheight = san.getHeight();
//生成三個圖片合并大小的Bitmap
Bitmap newbmp = Bitmap.createBitmap(kebianwidth, headHeight + kebiaoheight + footerheight, Bitmap.Config.ARGB_8888);
Canvas cv = new Canvas(newbmp);
cv.drawBitmap(head, 0, 0, null);// 在 0,0坐標開始畫入headBitmap
//因為手機不同圖片的大小的可能小了 就繪制白色的界面填充剩下的界面
if (headWidth < kebianwidth) {
System.out.println("繪制頭");
Bitmap ne = Bitmap.createBitmap(kebianwidth - headWidth, headHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ne);
canvas.drawColor(Color.WHITE);
cv.drawBitmap(ne, headWidth, 0, null);
}
cv.drawBitmap(kebiao, 0, headHeight, null);// 在 0,headHeight坐標開始填充課表的Bitmap
cv.drawBitmap(san, 0, headHeight + kebiaoheight, null);// 在 0,headHeight + kebiaoheight坐標開始填充課表的Bitmap
//因為手機不同圖片的大小的可能小了 就繪制白色的界面填充剩下的界面
if (fotwid < kebianwidth) {
System.out.println("繪制");
Bitmap ne = Bitmap.createBitmap(kebianwidth - fotwid, footerheight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ne);
canvas.drawColor(Color.WHITE);
cv.drawBitmap(ne, fotwid, headHeight + kebiaoheight, null);
}
cv.save(Canvas.ALL_SAVE_FLAG);// 保存
cv.restore();// 存儲
//回收
head.recycle();
kebiao.recycle();
san.recycle();
下載地址
環境Android Studio
csdn下載地址
查看GIT
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。