Blogger Scott

          Android給scrollView截圖超過(guò)屏幕大小形成長(zhǎng)圖

          原文轉(zhuǎn)自:http://www.infoword.cn/archives/55

          最近在研究Android屏幕取詞取圖,網(wǎng)上搜到這個(gè)通過(guò)生成Scollview長(zhǎng)圖的方式,可能是一個(gè)思路,摘抄如下:
          根據(jù)布局里scrollView的高度來(lái)截取圖片。
          代碼如下:
           1 /**
           2      * 截取scrollview的屏幕
           3      * @param scrollView
           4      * @return
           5      */
           6     public static Bitmap getBitmapByView(ScrollView scrollView) {
           7         int h = 0;
           8         Bitmap bitmap = null;
           9         // 獲取scrollview實(shí)際高度
          10         for (int i = 0; i < scrollView.getChildCount(); i++) {
          11             h += scrollView.getChildAt(i).getHeight();
          12             scrollView.getChildAt(i).setBackgroundColor(
          13                     Color.parseColor("#ffffff"));
          14         }
          15         // 創(chuàng)建對(duì)應(yīng)大小的bitmap
          16         bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
          17                 Bitmap.Config.RGB_565);
          18         final Canvas canvas = new Canvas(bitmap);
          19         scrollView.draw(canvas);
          20         return bitmap;
          21     }
          22  
          23     /**
          24      * 壓縮圖片
          25      * @param image
          26      * @return
          27      */
          28     public static Bitmap compressImage(Bitmap image) {
          29         ByteArrayOutputStream baos = new ByteArrayOutputStream();
          30         // 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
          31         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
          32         int options = 100;
          33         // 循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮
          34         while (baos.toByteArray().length / 1024 > 100) {
          35             // 重置baos
          36             baos.reset();
          37             // 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
          38             image.compress(Bitmap.CompressFormat.JPEG, options, baos);
          39             // 每次都減少10
          40             options -= 10;
          41         }
          42         // 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中
          43         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
          44         // 把ByteArrayInputStream數(shù)據(jù)生成圖片
          45         Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);
          46         return bitmap;
          47     }
          48  
          49 /**
          50      * 保存到sdcard
          51      * @param b
          52      * @return
          53      */
          54     public static String savePic(Bitmap b) {
          55         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss",
          56                 Locale.US);
          57         File outfile = new File("/sdcard/image");
          58         // 如果文件不存在,則創(chuàng)建一個(gè)新文件
          59         if (!outfile.isDirectory()) {
          60             try {
          61                 outfile.mkdir();
          62             } catch (Exception e) {
          63                 e.printStackTrace();
          64             }
          65         }
          66         String fname = outfile + "/" + sdf.format(new Date()) + ".png";
          67         FileOutputStream fos = null;
          68         try {
          69             fos = new FileOutputStream(fname);
          70             if (null != fos) {
          71                 b.compress(Bitmap.CompressFormat.PNG, 90, fos);
          72                 fos.flush();
          73                 fos.close();
          74             }
          75         } catch (FileNotFoundException e) {
          76             e.printStackTrace();
          77         } catch (IOException e) {
          78             e.printStackTrace();
          79         }
          80         return fname;
          81     }

          在需要用到的地方調(diào)用getBitmapByView()方法即可。
          但是這樣寫(xiě)的話有時(shí)候會(huì)因?yàn)榻厝〉膱D片太長(zhǎng)太大而報(bào)outofmemory的錯(cuò),所以為了避免內(nèi)存溢出,程序崩掉,要注意用Config.RGB_565,會(huì)比ARGB_8888少占內(nèi)存。還有就是把圖片壓縮一下,至少我這樣就沒(méi)有報(bào)oom的錯(cuò)了,即:
          1 String fname = ScreenShot.savePic(ScreenShot.compressImage(ScreenShot
          2                         .getBitmapByView(scrollView)));

          posted on 2015-04-15 11:08 江天部落格 閱讀(1294) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): AndroidJava

          主站蜘蛛池模板: 游戏| 穆棱市| 涟水县| 汉寿县| 上思县| 宣恩县| 启东市| 贵溪市| 镇宁| 尚志市| 南澳县| 湖北省| 庄浪县| 工布江达县| 肥东县| 山东省| 喀喇沁旗| 长子县| 赤峰市| 读书| 延庆县| 永登县| 全椒县| 麟游县| 开封县| 台中县| 黑河市| 蒲江县| 滦平县| 浦江县| 常德市| 布尔津县| 六安市| 眉山市| 庆阳市| 察哈| 尖扎县| 大宁县| 宽甸| 金平| 罗平县|