记录一下Android 长截屏功能
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                记录一下Android 长截屏功能
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                需求對(duì)webview進(jìn)行截屏,可以大于一屏
代碼:
在setContentView之前調(diào)用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {WebView.enableSlowWholeDocumentDraw();}對(duì)大于5.0的版本處理,防止截屏不全。
public static Bitmap capture(WebView webView) {Picture picture = webView.capturePicture();int width = picture.getWidth();int height = picture.getHeight();Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);picture.draw(canvas);return bitmap;}擴(kuò)展:截屏listview,scrollview
/*** 截取scrollview的屏幕* **/public static Bitmap getScrollViewBitmap(ScrollView scrollView) {int h = 0;Bitmap bitmap;for (int i = 0; i < scrollView.getChildCount(); i++) {h += scrollView.getChildAt(i).getHeight();}// 創(chuàng)建對(duì)應(yīng)大小的bitmapbitmap = Bitmap.createBitmap(scrollView.getWidth(), h,Bitmap.Config.ARGB_8888);final Canvas canvas = new Canvas(bitmap);scrollView.draw(canvas);return bitmap;} //截取超過一屏的listviewpublic static Bitmap shotListView(ListView listview) {ListAdapter adapter = listview.getAdapter();int itemscount = adapter.getCount();int allitemsheight = 0;List<Bitmap> bmps = new ArrayList<Bitmap>();for (int i = 0; i < itemscount; i++) {View childView = adapter.getView(i, null, listview);childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());childView.setDrawingCacheEnabled(true);childView.buildDrawingCache();bmps.add(childView.getDrawingCache());allitemsheight += childView.getMeasuredHeight();}int w = listview.getMeasuredWidth();Bitmap bigbitmap = Bitmap.createBitmap(w, allitemsheight, Bitmap.Config.ARGB_8888);Canvas bigcanvas = new Canvas(bigbitmap);Paint paint = new Paint();int iHeight = 0;for (int i = 0; i < bmps.size(); i++) {Bitmap bmp = bmps.get(i);bigcanvas.drawBitmap(bmp, 0, iHeight, paint);iHeight += bmp.getHeight();bmp.recycle();bmp = null;}return bigbitmap;}截取不超過一屏的listview
/*** 截圖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();}Log.d(TAG, "實(shí)際高度:" + h);Log.d(TAG, "list 高度:" + listView.getHeight());// 創(chuàng)建對(duì)應(yīng)大小的bitmapbitmap = Bitmap.createBitmap(listView.getWidth(), h,Bitmap.Config.ARGB_8888);final Canvas canvas = new Canvas(bitmap);listView.draw(canvas);return bitmap;}總結(jié)
以上是生活随笔為你收集整理的记录一下Android 长截屏功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: javaScript数据类型(包括基本数
- 下一篇: BZOJ_1009_[HNOI2008]
