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

首頁 > 系統 > Android > 正文

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

2019-12-12 01:28:56
字體:
來源:轉載
供稿:網友

很多的時候,我們想要分享一個界面的所有內容,可是內容太多,超過了屏幕的大小,簡單的截屏已經滿足不了我們的需要,這時候我們就可以根據布局里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))); 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九龙县| 扎赉特旗| 敖汉旗| 图们市| 乌兰县| 普陀区| 札达县| 甘肃省| 西贡区| 洪雅县| 隆林| 平远县| 化德县| 九龙坡区| 卓资县| 牙克石市| 东阳市| 武威市| 梁山县| 兰考县| 彝良县| 大方县| 宁德市| 五家渠市| 菏泽市| 山阳县| 呼玛县| 肇东市| 台前县| 资溪县| 望城县| 修武县| 绍兴市| 平湖市| 沁水县| 澄城县| 望都县| 沾化县| 永善县| 怀安县| 博乐市|