淺談Android截屏和指定View生成截圖
截取當(dāng)前Activity頁面的截圖,可以通過窗體最底層的decorView進行緩存,然后根據(jù)這個緩存對象生成一張圖片。有的需要不需要狀態(tài)欄,也可以指定生成圖片的寬高,把狀態(tài)欄去除。
/** * 截取當(dāng)前窗體的截圖,根據(jù)[isShowStatusBar]判斷是否包含當(dāng)前窗體的狀態(tài)欄 * 原理是獲取當(dāng)前窗體decorView的緩存生成圖片 */fun captureWindow(activity: Activity, isShowStatusBar: Boolean): Bitmap? { // 獲取當(dāng)前窗體的View對象 val view = activity.window.decorView view.isDrawingCacheEnabled = true // 生成緩存 view.buildDrawingCache() val bitmap = if (isShowStatusBar) {// 繪制整個窗體,包括狀態(tài)欄Bitmap.createBitmap(view.drawingCache, 0, 0, view.measuredWidth, view.measuredHeight) } else {// 獲取狀態(tài)欄高度val rect = Rect()view.getWindowVisibleDisplayFrame(rect)val display = activity.windowManager.defaultDisplay// 減去狀態(tài)欄高度Bitmap.createBitmap(view.drawingCache, 0,rect.top, display.width, display.height - rect.top) } view.isDrawingCacheEnabled = false view.destroyDrawingCache() return bitmap}截取常用的View
截取之前一定要View已經(jīng)繪制完畢了,所以要注意使用post方法確保View繪制完畢。有的分享出去的截圖頁面并不是當(dāng)前展示給用戶的UI樣式,所以可以在當(dāng)前布局中隱藏一個容器,專門用來存放截圖,這個容器不展示給用戶。
/** * View已經(jīng)在界面上展示了,可以直接獲取View的緩存 * 對View進行量測,布局后生成View的緩存 * View為固定大小的View,包括TextView,ImageView,LinearLayout,FrameLayout,RelativeLayout等 * @param view 截取的View,View必須有固定的大小,不然drawingCache返回null * @return 生成的Bitmap */fun captureView(view: View): Bitmap? {view.isDrawingCacheEnabled = trueview.buildDrawingCache()// 重新測量一遍View的寬高view.measure(View.MeasureSpec.makeMeasureSpec(view.width, View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(view.height, View.MeasureSpec.EXACTLY))// 確定View的位置view.layout(view.x.toInt(), view.y.toInt(), view.x.toInt() + view.measuredWidth,view.y.toInt() + view.measuredHeight)// 生成View寬高一樣的Bitmapval bitmap = Bitmap.createBitmap(view.drawingCache, 0, 0, view.measuredWidth,view.measuredHeight)view.isDrawingCacheEnabled = falseview.destroyDrawingCache()return bitmap}截取ScrollView
ScrollView截圖的難點在于ScrollView高度不確定,如果能確定高度,就可以使用ScrollView的緩存生成圖片。ScrollView只有一個子View,所以只需要對子View進行測量,獲取到子View的高度就可以確定ScrollView的高度。
/** * 截取ScrollerView * 原理是獲取scrollView的子View的高度,然后創(chuàng)建一個子View寬高的畫布,將ScrollView繪制在畫布上 * @param scrollView 控件 * @return 返回截圖后的Bitmap */fun captureScrollView(scrollView: ScrollView): Bitmap? { var h = 0 for (i in 0 until scrollView.childCount) { val childView = scrollView.getChildAt(i) // 獲取子View的高度 h += childView.height // 設(shè)置背景顏色,避免布局里未設(shè)置背景顏色,截的圖背景黑色 childView.setBackgroundColor(Color.parseColor('#FFFFFF')) } val bitmap = createBitmap(scrollView.width, h) val canvas = Canvas(bitmap) // 將ScrollView繪制在畫布上 scrollView.draw(canvas) return bitmap}截取ListView
ListView截圖原理是獲取到每一個子View的Bitmap對象,然后根據(jù)子View的高度,使用Paint將子View的截圖拼接繪制到Canvas上,最后生成一張包含所有子View的截圖。
/** * 截取ListView * 原理:獲取到每一個子View,將子View生成的bitmap存入集合,并且累積ListView高度 * 遍歷完成后,創(chuàng)建一個ListView大小的畫布,將集合的Bitmap繪制到畫布上 * @param listView 截圖控件對象 * @return 生成的截圖對象 */fun captureListView(listView: ListView): Bitmap? {val adapter = listView.adapterval itemCount = adapter.countvar allitemsheight = 0val bitmaps = ArrayList<Bitmap>()for (i in 0 until itemCount) { // 獲取每一個子View val childView = adapter.getView(i, null, listView) // 測量寬高 childView.measure( View.MeasureSpec.makeMeasureSpec(listView.width, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)) // 布局位置 childView.layout(0, 0, childView.measuredWidth, childView.measuredHeight) // 設(shè)置背景顏色,避免是黑色的 childView.setBackgroundColor(Color.parseColor('#FFFFFF')) childView.isDrawingCacheEnabled = true // 生成緩存 childView.buildDrawingCache() // 將每一個View的截圖加入集合 bitmaps.add(childView.drawingCache) // 疊加截圖高度 allitemsheight += childView.measuredHeight}// 創(chuàng)建和ListView寬高一樣的畫布val bitmap = createBitmap(listView.measuredWidth, allitemsheight)val canvas = Canvas(bitmap)val paint = Paint()var iHeight = 0ffor (i in bitmaps.indices) { val bmp: Bitmap = bitmaps[i] // 將每一個生成的bitmap繪制在畫布上 canvas.drawBitmap(bmp, 0f, iHeight, paint) iHeight += bmp.height bmp.recycle()}return bitmap}截取RecyclerView
RecyclerView截圖和ListView截圖原理一樣,都是將子View的截圖進行拼接,最后生成一張大的截圖。
/** * 截取RecyclerView * 原理和ListView集合是一樣的,獲取到每一個Holder的截圖放入集合,最后統(tǒng)一繪制到Bitmap上 * @param recyclerView 要截圖的控件 * @return 生成的截圖 */fun captureRecyclerView(recyclerView: RecyclerView): Bitmap? {val adapter = recyclerView.adaptervar bigBitmap: Bitmap? = nullif (adapter != null) { val size = adapter.itemCount var height = 0 val paint = Paint() var iHeight = 0 val maxMemory = (Runtime.getRuntime().maxMemory() / 1024).toInt() // Use 1/8th of the available memory for this memory cache. val cacheSize = maxMemory / 8 val bitmapCache = LruCache<String, Bitmap>(cacheSize) for (i in 0 until size) {val holder = adapter.createViewHolder(recyclerView, adapter.getItemViewType(i))adapter.onBindViewHolder(holder, i)holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(recyclerView.width, View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))holder.itemView.layout(0, 0, holder.itemView.measuredWidth,holder.itemView.measuredHeight)holder.itemView.setBackgroundColor(Color.parseColor('#FFFFFF'))holder.itemView.isDrawingCacheEnabled = trueholder.itemView.buildDrawingCache()val drawingCache = holder.itemView.drawingCacheif (drawingCache != null) { bitmapCache.put(i.toString(), drawingCache)}height += holder.itemView.measuredHeight } bigBitmap = createBitmap(recyclerView.measuredWidth, height) val bigCanvas = Canvas(bigBitmap!!) val lBackground = recyclerView.background if (lBackground is ColorDrawable) {val lColor = lBackground.colorbigCanvas.drawColor(lColor) } for (i in 0 until size) {val bitmap = bitmapCache.get(i.toString())bigCanvas.drawBitmap(bitmap, 0f, iHeight.toFloat(), paint)iHeight += bitmap.heightbitmap.recycle() }}return bigBitmap}截取WebView
WebView可以加載很長很復(fù)雜的頁面,所以進行截圖很容易發(fā)生內(nèi)存溢出,不過一般的需求也不會有那個大的圖片去分享,這里只做簡單的截圖。
/** * 截取WebView,包含WebView的整個長度 * 在WebView渲染之前要加上以下代碼,開啟Html緩存,不然會截屏空白 * if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { * WebView.enableSlowWholeDocumentDraw() * } * WebView的截圖很容易遇到內(nèi)存溢出的問題,因為WebView可以加載很多內(nèi)容,導(dǎo)致生成的圖片特別長,創(chuàng)建Bitmap時容易OOM */fun captureWebView(webView: WebView): Bitmap? {// 重新調(diào)用WebView的measure方法測量實際View的大小(將測量模式設(shè)置為UNSPECIFIED模式也就是需要多大就可以獲得多大的空間)webView.measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))// 調(diào)用layout方法設(shè)置布局(使用新測量的大小)webView.layout(0, 0, webView.measuredWidth, webView.measuredHeight)// 開啟WebView的緩存(當(dāng)開啟這個開關(guān)后下次調(diào)用getDrawingCache()方法的時候會把view繪制到一個bitmap上)webView.isDrawingCacheEnabled = true// 強制繪制緩存(必須在setDrawingCacheEnabled(true)之后才能調(diào)用,否者需要手動調(diào)用destroyDrawingCache()清楚緩存)webView.buildDrawingCache()val bitmap = createBitmap(webView.measuredWidth, webView.measuredHeight)// 已picture為背景創(chuàng)建一個畫布val canvas = Canvas(bitmap) // 畫布的寬高和 WebView 的網(wǎng)頁保持一致val paint = Paint()// 設(shè)置畫筆的定點位置,也就是左上角canvas.drawBitmap(bitmap, 0f, webView.measuredHeight * 1f, paint)// 將WebView繪制在剛才創(chuàng)建的畫板上webView.draw(canvas)webView.isDrawingCacheEnabled = falsewebView.destroyDrawingCache()return bitmap}
基本常用的View都可以進行截圖,但是像WebView這種可以加載很長很復(fù)雜頁面的控件,截圖容易發(fā)生OOM,所以要考慮好用什么控件進行截圖。上面的代碼親測都可以使用。
以上就是淺談Android截屏和指定View生成截圖的詳細內(nèi)容,更多關(guān)于Android截屏和指定View生成截圖的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. AspNetCore&MassTransit Courier實現(xiàn)分布式事務(wù)的詳細過程2. 如何在jsp界面中插入圖片3. JSP數(shù)據(jù)交互實現(xiàn)過程解析4. PHP循環(huán)與分支知識點梳理5. XML基本概念XPath、XSLT與XQuery函數(shù)介紹6. JS中map和parseInt的用法詳解7. jsp實現(xiàn)登錄界面8. 低版本IE正常運行HTML5+CSS3網(wǎng)站的3種解決方案9. ASP.NET泛型三之使用協(xié)變和逆變實現(xiàn)類型轉(zhuǎn)換10. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)
