新聞中心
這篇文章給大家分享的是有關(guān)Android如何實(shí)現(xiàn)給scrollView截圖超過屏幕大小形成長圖的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括黎平網(wǎng)站建設(shè)、黎平網(wǎng)站制作、黎平網(wǎng)頁制作以及黎平網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,黎平網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到黎平省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
很多的時(shí)候,我們想要分享一個(gè)界面的所有內(nèi)容,可是內(nèi)容太多,超過了屏幕的大小,簡單的截屏已經(jīng)滿足不了我們的需要,這時(shí)候我們就可以根據(jù)布局里scrollView的高度來截取圖片。
代碼如下:
/** * 截取scrollview的屏幕 * @param scrollView * @return */ public static Bitmap getBitmapByView(ScrollView scrollView) { int h = 0; Bitmap bitmap = null; // 獲取scrollview實(shí)際高度 for (int i = 0; i < scrollView.getChildCount(); i++) { h += scrollView.getChildAt(i).getHeight(); scrollView.getChildAt(i).setBackgroundColor( Color.parseColor("#ffffff")); } // 創(chuàng)建對應(yīng)大小的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(); // 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; // 循環(huán)判斷如果壓縮后圖片是否大于100kb,大于繼續(xù)壓縮 while (baos.toByteArray().length / 1024 > 100) { // 重置baos baos.reset(); // 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, options, baos); // 每次都減少10 options -= 10; } // 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); // 把ByteArrayInputStream數(shù)據(jù)生成圖片 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"); // 如果文件不存在,則創(chuàng)建一個(gè)新文件 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; }
在需要用到的地方調(diào)用getBitmapByView()方法即可:
String fname = ScreenShot.savePic(ScreenShot.getBitmapByView(scrollView));
但是這樣寫的話有時(shí)候會因?yàn)榻厝〉膱D片太長太大而報(bào)outofmemory的錯(cuò),所以為了避免內(nèi)存溢出,程序崩掉,要注意用Config.RGB_565,會比ARGB_8888少占內(nèi)存。還有就是把圖片壓縮一下,至少我這樣就沒有報(bào)oom的錯(cuò)了,即:
String fname = ScreenShot.savePic(ScreenShot.compressImage(ScreenShot .getBitmapByView(scrollView)));
感謝各位的閱讀!關(guān)于“Android如何實(shí)現(xiàn)給scrollView截圖超過屏幕大小形成長圖”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
文章名稱:Android如何實(shí)現(xiàn)給scrollView截圖超過屏幕大小形成長圖
文章鏈接:http://www.dlmjj.cn/article/iechii.html