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

看到的是圖片只顯示到11節(jié)處,下面的沒有顯示到 所以用到的 ScrollView
因此截圖節(jié)截取ScrollView View的圖片
一、首先計(jì)算出整個(gè)ScrollView 的高度寬度生成對(duì)應(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)建對(duì)應(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();
//生成三個(gè)圖片合并大小的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();// 存儲(chǔ)
//回收
head.recycle();
kebiao.recycle();
san.recycle();
下載地址
環(huán)境Android Studio
csdn下載地址
查看GIT
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android之PreferenceActivity應(yīng)用詳解(2)
看到很多書中都沒有對(duì)PreferenceActivity做介紹,而我正好又在項(xiàng)目中用到,所以就把自己的使用的在這總結(jié)一下,也方便日后查找2012-11-11
一個(gè)吸頂Item的簡(jiǎn)單實(shí)現(xiàn)方法分享
這篇文章主要給大家介紹了一個(gè)吸頂Item的簡(jiǎn)單實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Android Studio下的APP目錄結(jié)構(gòu)詳解
這篇文章主要介紹了AndroidStudio下的APP目錄結(jié)構(gòu),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Android高級(jí)組件ImageSwitcher圖像切換器使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android高級(jí)組件ImageSwitcher圖像切換器的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android編程之絕對(duì)布局AbsoluteLayout和相對(duì)布局RelativeLayout實(shí)例詳解
這篇文章主要介紹了Android編程之絕對(duì)布局AbsoluteLayout和相對(duì)布局RelativeLayout實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android絕對(duì)布局AbsoluteLayout和相對(duì)布局RelativeLayout的原理與使用技巧,需要的朋友可以參考下2015-12-12

