Android實(shí)現(xiàn)合并生成分享圖片功能
有時候分享功能都是很需要分享一個當(dāng)前屏幕的界面的截圖因,以前做校內(nèi)APP的時候用到過,拿出來分享分享, 用以前寫過的自定義課表軟件。

看到的是圖片只顯示到11節(jié)處,下面的沒有顯示到 所以用到的 ScrollView
因此截圖節(jié)截取ScrollView View的圖片
一、首先計(jì)算出整個ScrollView 的高度寬度生成對應(yīng)大小的的Bitmap 然后把使用Canvas 將ScrollView 的界面繪制上去
// 獲取ScrollView 實(shí)際高度
h = 0;
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundResource(android.R.color.white);
}
// 創(chuàng)建對應(yīng)大小的bitmap
Bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
二、獲取分享的頭部和底部圖片的Bitmap
// BitmapFactory.decodeResource函數(shù)直接轉(zhuǎn)換資源文件為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坐標(biāo)開始畫入headBitmap
//因?yàn)槭謾C(jī)不同圖片的大小的可能小了 就繪制白色的界面填充剩下的界面
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坐標(biāo)開始填充課表的Bitmap
cv.drawBitmap(san, 0, headHeight + kebiaoheight, null);// 在 0,headHeight + kebiaoheight坐標(biāo)開始填充課表的Bitmap
//因?yàn)槭謾C(jī)不同圖片的大小的可能小了 就繪制白色的界面填充剩下的界面
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();
下載地址
環(huán)境Android Studio
csdn下載地址
查看GIT
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android ToolBar 修改邊距的實(shí)現(xiàn)方法
這篇文章主要介紹了Android ToolBar 修改邊距的實(shí)現(xiàn)方法的相關(guān)資料,通過此文希望能幫助到大家,需要的朋友可以參考下2017-08-08
Android DynamicGrid實(shí)現(xiàn)拖曳交換位置功能
這篇文章主要為大家詳細(xì)介紹了Android DynamicGrid實(shí)現(xiàn)拖曳交換位置功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
ExpandableListView實(shí)現(xiàn)簡單二級列表
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)簡單二級列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
Android?ViewModel創(chuàng)建不受橫豎屏切換影響原理詳解
這篇文章主要為大家介紹了Android?ViewModel創(chuàng)建不受橫豎屏切換影響原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法
這篇文章主要介紹了Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法,結(jié)合實(shí)例形式分析了Android5.0以上實(shí)現(xiàn)截圖功能的相關(guān)類、函數(shù)及權(quán)限控制等操作技巧,需要的朋友可以參考下2018-01-01
Android實(shí)現(xiàn)簡單的popupwindow提示框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單的popupwindow提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
Android studio導(dǎo)出APP測試包和構(gòu)建正式簽名包
大家好,本篇文章主要講的是Android studio導(dǎo)出APP測試包和構(gòu)建正式簽名包,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2021-12-12

