Android實(shí)現(xiàn)合并生成分享圖片功能

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

成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、靈川網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、商城系統(tǒng)網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為靈川等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

Android 自定義View課程表表格

Android實(shí)現(xiàn)合并生成分享圖片功能

看到的是圖片只顯示到11節(jié)處,下面的沒有顯示到 所以用到的 ScrollView

因此截圖節(jié)截取ScrollView View的圖片

一、首先計算出整個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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

文章標(biāo)題:Android實(shí)現(xiàn)合并生成分享圖片功能
當(dāng)前URL:http://muchs.cn/article6/gdecog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、企業(yè)網(wǎng)站制作建站公司、動態(tài)網(wǎng)站網(wǎng)站制作、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管