Android給scrollView截圖超過屏幕大小形成長圖
很多的時候,我們想要分享一個界面的所有內(nèi)容,可是內(nèi)容太多,超過了屏幕的大小,簡單的截屏已經(jīng)滿足不了我們的需要,這時候我們就可以根據(jù)布局里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")); } // 創(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)建一個新文件 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));
但是這樣寫的話有時候會因為截取的圖片太長太大而報outofmemory的錯,所以為了避免內(nèi)存溢出,程序崩掉,要注意用Config.RGB_565,會比ARGB_8888少占內(nèi)存。還有就是把圖片壓縮一下,至少我這樣就沒有報oom的錯了,即:
String fname = ScreenShot.savePic(ScreenShot.compressImage(ScreenShot .getBitmapByView(scrollView)));
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的實戰(zhàn)教程
二維碼的應(yīng)用已經(jīng)可以說是非常廣泛了,下面這篇文章主要給大家介紹了關(guān)于zxing二維碼位矩陣轉(zhuǎn)換成Bitmap位圖的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-09-09Android自定義scrollView實現(xiàn)頂部圖片下拉放大
這篇文章主要為大家詳細介紹了Android自定義scrollView實現(xiàn)頂部圖片下拉放大,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android RecycleView 實現(xiàn)左滑上下分層示例代碼(自定義功能)
這篇文章主要介紹了Android RecycleView 實現(xiàn)左滑上下分層示例代碼(自定義功能),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03Android中EditText光標在4.0中的bug及解決方法
這篇文章主要介紹了Android中EditText光標在4.0中的bug及解決方法,簡單分析了Android4.0版本中EditText光標消息的原因及相應(yīng)的解決方法,需要的朋友可以參考下2016-01-01Android中RecyclerView嵌套滑動沖突解決的代碼片段
這篇文章主要為大家詳細介紹了Android中RecyclerView嵌套滑動沖突解決的代碼片段,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12初識Android?PowerManagerService省電模式
這篇文章主要介紹了初識Android?PowerManagerService省電模式,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-08-08Android利用ContentProvider獲取聯(lián)系人信息
這篇文章主要為大家詳細介紹了Android利用ContentProvider獲取聯(lián)系人信息,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11Android 實現(xiàn)永久保存數(shù)據(jù)的方法詳解
本篇文章是對Android實現(xiàn)永久保存數(shù)據(jù)的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06為Retrofit統(tǒng)一添加post請求的默認參數(shù)的方法
這篇文章主要介紹了為Retrofit統(tǒng)一添加post請求的默認參數(shù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04