android 手機(jī)截取長(zhǎng)屏實(shí)例代碼
最近項(xiàng)目遇到一個(gè)需求:把當(dāng)前頁(yè)面保存到手機(jī)相冊(cè)。想了想 我還不會(huì)呢,就百度了下大神的足跡,踏著大神的足跡,一路向前。廢話不說,記錄下,后期學(xué)習(xí)。
public class ScreenUtils { /** * 截取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)建對(duì)應(yī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實(shí)際高度 for (int i = 0; i < listView.getChildCount(); i++) { h += listView.getChildAt(i).getHeight(); } // 創(chuàng)建對(duì)應(yī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)建一個(gè)新文件 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)圖庫(kù) try { MediaStore.Images.Media.insertImage(context.getContentResolver(), outfile.getAbsolutePath(), fname, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知圖庫(kù)更新 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)));
好了,截取成功了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Bitmap的截取及狀態(tài)欄的隱藏和顯示功能
- Android實(shí)現(xiàn)bitmap指定區(qū)域滑動(dòng)截取功能
- 解析Android截取手機(jī)屏幕兩種實(shí)現(xiàn)方案
- Android實(shí)現(xiàn)拍照截取和相冊(cè)圖片截取
- Android個(gè)人中心的頭像上傳,圖片編碼及截取實(shí)例
- Android 仿QQ頭像自定義截取功能
- Android開發(fā)獲取短信的內(nèi)容并截取短信
- Android中截取當(dāng)前屏幕圖片的實(shí)例代碼
- Android截取視頻幀并轉(zhuǎn)化為Bitmap示例
- Android截取指定View為圖片的實(shí)現(xiàn)方法
相關(guān)文章
android隱式意圖激活自定義界面和系統(tǒng)應(yīng)用界面的實(shí)例
下面小編就為大家?guī)硪黄猘ndroid隱式意圖激活自定義界面和系統(tǒng)應(yīng)用界面的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽
本篇文章主要介紹了android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09Android自定義可拖拽的懸浮按鈕DragFloatingActionButton
這篇文章主要介紹了Android自定義可拖拽的懸浮按鈕DragFloatingActionButton,需要的朋友可以參考下2017-06-06Android 自定義ListView實(shí)現(xiàn)QQ空間界面(說說內(nèi)包含圖片、視頻、點(diǎn)贊、評(píng)論、轉(zhuǎn)發(fā)功能)
這篇文章主要介紹了Android 自定義ListView實(shí)現(xiàn)QQ空間界面,qq空間說說內(nèi)包含圖片、視頻、點(diǎn)贊、評(píng)論、轉(zhuǎn)發(fā)功能,需要的朋友可以參考下2019-12-12Core Animation一些Demo總結(jié) (動(dòng)態(tài)切換圖片、大轉(zhuǎn)盤、圖片折疊、進(jìn)度條等動(dòng)畫效果)
這篇文章主要介紹了Core Animation一些Demo總結(jié) (動(dòng)態(tài)切換圖片、大轉(zhuǎn)盤、圖片折疊、進(jìn)度條等動(dòng)畫效果)的相關(guān)資料,需要的朋友可以參考下2016-02-02Android實(shí)現(xiàn)邊錄邊播應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)邊錄邊播應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11