Android實(shí)現(xiàn)合并生成分享圖片功能
有時(shí)候分享功能都是很需要分享一個(gè)當(dāng)前屏幕的界面的截圖因,以前做校內(nèi)APP的時(shí)候用到過(guò),拿出來(lái)分享分享, 用以前寫(xiě)過(guò)的自定義課表軟件。
看到的是圖片只顯示到11節(jié)處,下面的沒(méi)有顯示到 所以用到的 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)開(kāi)始畫(huà)入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)開(kāi)始填充課表的Bitmap cv.drawBitmap(san, 0, headHeight + kebiaoheight, null);// 在 0,headHeight + kebiaoheight坐標(biāo)開(kāi)始填充課表的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í)有所幫助,也希望大家多多支持腳本之家。
- android異步生成圖片的示例代碼
- Android栗子の圖片驗(yàn)證碼生成實(shí)例代碼
- Android 實(shí)現(xiàn)圖片生成卷角和圓角縮略圖的方法
- Android實(shí)現(xiàn)用文字生成圖片的示例代碼
- Android 錄制手機(jī)屏幕視頻生成GIF圖片實(shí)例詳解
- Android仿簡(jiǎn)書(shū)長(zhǎng)按文章生成圖片效果
- Android自定義View繪制隨機(jī)生成圖片驗(yàn)證碼
- Android生成帶圓角的Bitmap圖片
- android通過(guò)bitmap生成新圖片關(guān)鍵性代碼
- Android布局生成分享圖片代碼實(shí)例
相關(guān)文章
Android ToolBar 修改邊距的實(shí)現(xiàn)方法
這篇文章主要介紹了Android ToolBar 修改邊距的實(shí)現(xiàn)方法的相關(guān)資料,通過(guò)此文希望能幫助到大家,需要的朋友可以參考下2017-08-08Android DynamicGrid實(shí)現(xiàn)拖曳交換位置功能
這篇文章主要為大家詳細(xì)介紹了Android DynamicGrid實(shí)現(xiàn)拖曳交換位置功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06ExpandableListView實(shí)現(xiàn)簡(jiǎn)單二級(jí)列表
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)簡(jiǎn)單二級(jí)列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android?ViewModel創(chuàng)建不受橫豎屏切換影響原理詳解
這篇文章主要為大家介紹了Android?ViewModel創(chuàng)建不受橫豎屏切換影響原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Filter過(guò)濾器和Listener監(jiān)聽(tīng)器詳解
這篇文章主要介紹了 Filter過(guò)濾器和Listener監(jiān)聽(tīng)器詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04Android 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-01Android實(shí)現(xiàn)簡(jiǎn)單的popupwindow提示框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的popupwindow提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android studio導(dǎo)出APP測(cè)試包和構(gòu)建正式簽名包
大家好,本篇文章主要講的是Android studio導(dǎo)出APP測(cè)試包和構(gòu)建正式簽名包,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2021-12-12