編輯:Android 開發實例
本文總結了 Android 中實現截屏的幾種方法,包括普通 Activity 截屏、單個 View 截屏,以及 ScrollView、ListView 這類可滾動控件的長截圖,供大家參考。
普通截屏的核心原理是開啟 View 的 DrawingCache,取出緩存位圖後再按需裁剪、保存。示例代碼如下:
public class ScreenShot { // 獲取指定 Activity 的截屏,保存到 png 文件 public static Bitmap takeScreenShot(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache(); // 獲取狀態欄高度 Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; System.out.println(statusBarHeight); // 獲取屏幕長和高 int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay() .getHeight(); // 去掉標題欄 Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); savePic(b, "/sdcard/screen_test.png"); return b; } // 保存到 sdcard public static void savePic(Bitmap b, String strFileName) { FileOutputStream fos = null; try { fos = new FileOutputStream(strFileName); if (null != fos) { b.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.flush(); fos.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** 把 View 對象轉換成 bitmap */ public static Bitmap convertViewToBitmap(View view) { view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; } // 程序入口1 public static void shoot(Activity a) { ScreenShot.savePic(ScreenShot.takeScreenShot(a), "/sdcard/screen_test.png"); } // 程序入口2 public static void shootView(View view) { ScreenShot.savePic(ScreenShot.convertViewToBitmap(view), "sdcard/xx.png"); } public static Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e("TTTTTTTTActivity", "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; } /** 截取 scrollview 的屏幕 **/ public static Bitmap getBitmapByView(ScrollView scrollView) { int h = 0; Bitmap bitmap = null; for (int i = 0; i < scrollView.getChildCount(); i++) { h += scrollView.getChildAt(i).getHeight(); scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3); } bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); scrollView.draw(canvas); FileOutputStream out = null; try { out = new FileOutputStream("/sdcard/screen_test.png"); } catch (FileNotFoundException e) { e.printStackTrace(); } try { if (null != out) { bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); } } catch (IOException e) { } return bitmap; } /** 截圖 listview **/ public static Bitmap getbBitmap(ListView listView) { int h = 0; Bitmap bitmap = null; for (int i = 0; i < listView.getChildCount(); i++) { h += listView.getChildAt(i).getHeight(); } bitmap = Bitmap.createBitmap(listView.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); listView.draw(canvas); FileOutputStream out = null; try { out = new FileOutputStream("/sdcard/screen_test.png"); } catch (FileNotFoundException e) { e.printStackTrace(); } try { if (null != out) { bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); } } catch (IOException e) { } return bitmap; } }
對於 ScrollView 和 ListView 這類可滾動控件,直接截屏只能拍到當前可見區域。要實現「長截圖」,思路是先遍歷所有子項累加出完整高度,再創建一個足夠大的 Bitmap,最後調用控件的 draw(Canvas) 把全部內容繪製上去。需要注意的是,這種方式要求 ListView 的所有 item 都已經被渲染出來,否則可能截不全。
此外,別忘了在清單文件中申請寫入外存權限 WRITE_EXTERNAL_STORAGE,否則保存圖片會失敗。
希望本文所述對大家的 Android 程序設計有所幫助。
Android 學習筆記:圖片非同步載入的實作方法
本文為大家分享 Android 非同步載入圖片的實作方法,值得參考,具體如下:近來在 Android 開發中遇到一個相當棘手的問題——載入圖片時記憶體溢出。我開發的是一款
Android Spinner 載入動畫:按鈕觸發與顯示控制
Spinner 是 Android 中常見的載入提示方式,適合用在不知道完成時間的任務。它通常只是一個持續旋轉的進度圖形,不會顯示百分比。 基本用法 先在版面配置中加入
Android底部菜單欄實戰:TabHost打造帶未讀紅點的通用底欄
上一個項目收尾後,這周難得清閒,我便把過去的項目翻出來整理,想把一些通用模塊封裝起來,以後遇到相似需求就不用反覆造輪子了,也能省下不少開發時間。今天把這個 demo 貼出
Android 應用本地化:讓 App 依語言環境自動切換文字
同一個 App 可能會被安裝在義大利、日本、德國或巴西的裝置上。使用者對「國際化」的期待很簡單:打開 App,看到的就是自己熟悉的語言。要做到這件事,靠的不是在程式碼裡寫