android 手機截取長屏實例代碼
最近項目遇到一個需求:把當前頁面保存到手機相冊。想了想 我還不會呢,就百度了下大神的足跡,踏著大神的足跡,一路向前。廢話不說,記錄下,后期學習。
public class ScreenUtils { /** * 截取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)建對應大小的bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.RGB_565); final Canvas canvas = new Canvas(bitmap); scrollView.draw(canvas); return bitmap; } /** * 截圖listview * **/ public static Bitmap getListViewBitmap(ListView listView,String picpath) { int h = 0; Bitmap bitmap; // 獲取listView實際高度 for (int i = 0; i < listView.getChildCount(); i++) { h += listView.getChildAt(i).getHeight(); } // 創(chuàng)建對應大小的bitmap bitmap = Bitmap.createBitmap(listView.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); listView.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)判斷如果壓縮后圖片是否大于250K,大于繼續(xù)壓縮 while (baos.toByteArray().length / 1024 > 1024 && options >10) { // 重置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(Context context, Bitmap b) { File outfile = new File("/sdcard/image"); // 如果文件不存在,則創(chuàng)建一個新文件 if (!outfile.isDirectory()) { try { outfile.mkdir(); } catch (Exception e) { e.printStackTrace(); } } String fname = outfile + "/" + System.currentTimeMillis() + ".jpg"; FileOutputStream fos = null; try { fos = new FileOutputStream(fname); if (null != fos) { b.compress(Bitmap.CompressFormat.JPEG, 90, fos); fos.flush(); fos.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 其次把文件插入到系統(tǒng)圖庫 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), outfile.getAbsolutePath(), fname, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知圖庫更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fname))); return fname; } }
以上為百度的工具類。
使用方法:
ScreenUtils .savePic(XXXActivity.this,ScreenUtils.compressImage(ScreenUtils .getBitmapByView(XXXScrollView)));
好了,截取成功了!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
android隱式意圖激活自定義界面和系統(tǒng)應用界面的實例
下面小編就為大家?guī)硪黄猘ndroid隱式意圖激活自定義界面和系統(tǒng)應用界面的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06android中Glide實現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽
本篇文章主要介紹了android中Glide實現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽,具有一定的參考價值,有興趣的可以了解一下2017-09-09Android自定義可拖拽的懸浮按鈕DragFloatingActionButton
這篇文章主要介紹了Android自定義可拖拽的懸浮按鈕DragFloatingActionButton,需要的朋友可以參考下2017-06-06Android 自定義ListView實現(xiàn)QQ空間界面(說說內(nèi)包含圖片、視頻、點贊、評論、轉(zhuǎn)發(fā)功能)
這篇文章主要介紹了Android 自定義ListView實現(xiàn)QQ空間界面,qq空間說說內(nèi)包含圖片、視頻、點贊、評論、轉(zhuǎn)發(fā)功能,需要的朋友可以參考下2019-12-12Core Animation一些Demo總結(jié) (動態(tài)切換圖片、大轉(zhuǎn)盤、圖片折疊、進度條等動畫效果)
這篇文章主要介紹了Core Animation一些Demo總結(jié) (動態(tài)切換圖片、大轉(zhuǎn)盤、圖片折疊、進度條等動畫效果)的相關資料,需要的朋友可以參考下2016-02-02