国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android給scrollView截圖超過屏幕大小形成長圖

2019-10-22 18:20:20
字體:
來源:轉載
供稿:網友

很多的時候,我們想要分享一個界面的所有內容,可是內容太多,超過了屏幕的大小,簡單的截屏已經滿足不了我們的需要,這時候我們就可以根據布局里scrollView的高度來截取圖片。

代碼如下:

/**    * 截取scrollview的屏幕    * @param scrollView    * @return    */   public static Bitmap getBitmapByView(ScrollView scrollView) {     int h = 0;     Bitmap bitmap = null;     // 獲取scrollview實際高度     for (int i = 0; i < scrollView.getChildCount(); i++) {       h += scrollView.getChildAt(i).getHeight();       scrollView.getChildAt(i).setBackgroundColor(           Color.parseColor("#ffffff"));     }     // 創建對應大小的bitmap     bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,         Bitmap.Config.RGB_565);     final Canvas canvas = new Canvas(bitmap);     scrollView.draw(canvas);     return bitmap;   }    /**    * 壓縮圖片    * @param image    * @return    */   public static Bitmap compressImage(Bitmap image) {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     // 質量壓縮方法,這里100表示不壓縮,把壓縮后的數據存放到baos中     image.compress(Bitmap.CompressFormat.JPEG, 100, baos);     int options = 100;     // 循環判斷如果壓縮后圖片是否大于100kb,大于繼續壓縮     while (baos.toByteArray().length / 1024 > 100) {       // 重置baos       baos.reset();       // 這里壓縮options%,把壓縮后的數據存放到baos中       image.compress(Bitmap.CompressFormat.JPEG, options, baos);       // 每次都減少10       options -= 10;     }     // 把壓縮后的數據baos存放到ByteArrayInputStream中     ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());     // 把ByteArrayInputStream數據生成圖片     Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);     return bitmap;   }  /**    * 保存到sdcard    * @param b    * @return    */   public static String savePic(Bitmap b) {     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss",         Locale.US);     File outfile = new File("/sdcard/image");     // 如果文件不存在,則創建一個新文件     if (!outfile.isDirectory()) {       try {         outfile.mkdir();       } catch (Exception e) {         e.printStackTrace();       }     }     String fname = outfile + "/" + sdf.format(new Date()) + ".png";     FileOutputStream fos = null;     try {       fos = new FileOutputStream(fname);       if (null != fos) {         b.compress(Bitmap.CompressFormat.PNG, 90, fos);         fos.flush();         fos.close();       }     } catch (FileNotFoundException e) {       e.printStackTrace();     } catch (IOException e) {       e.printStackTrace();     }     return fname;   } 

在需要用到的地方調用getBitmapByView()方法即可:

String fname = ScreenShot.savePic(ScreenShot.getBitmapByView(scrollView)); 

但是這樣寫的話有時候會因為截取的圖片太長太大而報outofmemory的錯,所以為了避免內存溢出,程序崩掉,要注意用Config.RGB_565,會比ARGB_8888少占內存。還有就是把圖片壓縮一下,至少我這樣就沒有報oom的錯了,即:

String fname = ScreenShot.savePic(ScreenShot.compressImage(ScreenShot             .getBitmapByView(scrollView))); 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚顺市| 浮山县| 观塘区| 祁门县| 龙海市| 扎囊县| 措美县| 抚顺县| 铁力市| 义马市| 海南省| 普格县| 河曲县| 云和县| 新宾| 孟州市| 武鸣县| 鹿邑县| 太白县| 墨玉县| 汝城县| 高阳县| 冷水江市| 怀安县| 汉川市| 加查县| 麻江县| 青河县| 涞源县| 特克斯县| 仁化县| 长治市| 秀山| 东城区| 九龙坡区| 浮梁县| 波密县| 乌兰浩特市| 金沙县| 云龙县| 海盐县|