Android截屏截图的几种方法总结
Android截屏
Android截屏的原理:获取具体需要截屏的区域的Bitmap,然后绘制在画布上,保存为图片后进行分享或者其它用途
一、Activity截屏
1、截Activity界面(包含空白的状态栏)
/** *根据指定的Activity截图(带空白的状态栏) * *@paramcontext要截图的Activity *@returnBitmap */ publicstaticBitmapshotActivity(Activitycontext){ Viewview=context.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmapbitmap=Bitmap.createBitmap(view.getDrawingCache(),0,0,view.getMeasuredWidth(),view.getMeasuredHeight()); view.setDrawingCacheEnabled(false); view.destroyDrawingCache(); returnbitmap; }
2、截Activity界面(去除状态栏)
/** *根据指定的Activity截图(去除状态栏) * *@paramactivity要截图的Activity *@returnBitmap */ publicBitmapshotActivityNoStatusBar(Activityactivity){ //获取windows中最顶层的view Viewview=activity.getWindow().getDecorView(); view.buildDrawingCache(); //获取状态栏高度 Rectrect=newRect(); view.getWindowVisibleDisplayFrame(rect); intstatusBarHeights=rect.top; Displaydisplay=activity.getWindowManager().getDefaultDisplay(); //获取屏幕宽和高 intwidths=display.getWidth(); intheights=display.getHeight(); //允许当前窗口保存缓存信息 view.setDrawingCacheEnabled(true); //去掉状态栏 Bitmapbmp=Bitmap.createBitmap(view.getDrawingCache(),0, statusBarHeights,widths,heights-statusBarHeights); //销毁缓存信息 view.destroyDrawingCache(); returnbmp; }
二、View截屏
/** *根据指定的view截图 * *@paramv要截图的view *@returnBitmap */ publicstaticBitmapgetViewBitmap(Viewv){ if(null==v){ returnnull; } v.setDrawingCacheEnabled(true); v.buildDrawingCache(); if(Build.VERSION.SDK_INT>=11){ v.measure(View.MeasureSpec.makeMeasureSpec(v.getWidth(),View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(v.getHeight(),View.MeasureSpec.EXACTLY)); v.layout((int)v.getX(),(int)v.getY(),(int)v.getX()+v.getMeasuredWidth(),(int)v.getY()+v.getMeasuredHeight()); }else{ v.measure(View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED)); v.layout(0,0,v.getMeasuredWidth(),v.getMeasuredHeight()); } Bitmapbitmap=Bitmap.createBitmap(v.getDrawingCache(),0,0,v.getMeasuredWidth(),v.getMeasuredHeight()); v.setDrawingCacheEnabled(false); v.destroyDrawingCache(); returnbitmap; }
三、ScrollView截屏:ScrollView只有一个childView,虽然没有全部显示在界面上,但是已经全部渲染绘制,因此可以直接调用scrollView.draw(canvas)来完成截图
/** *Scrollview截屏 * *@paramscrollView要截图的ScrollView *@returnBitmap */ publicstaticBitmapshotScrollView(ScrollViewscrollView){ inth=0; Bitmapbitmap=null; for(inti=0;i四、ListView截屏:ListView是会回收与重用Item,并且只会绘制在屏幕上显示的ItemView,下面的方法采用一个List来存储Item的视图,这种方案依然不够好,当Item足够多的时候,可能会发生oom。
/** *ListView截图 * *@paramlistView要截图的ListView *@returnBitmap */ publicstaticBitmapshotListView(ListViewlistView){ ListAdapteradapter=listView.getAdapter(); intitemsCount=adapter.getCount(); intallItemsHeight=0; ArrayListbmps=newArrayList<>(); for(inti=0;i 五、RecycleView截屏
/** *RecyclerView截屏 * *@paramview要截图的RecyclerView *@returnBitmap */ publicstaticBitmapshotRecyclerView(RecyclerViewview){ RecyclerView.Adapteradapter=view.getAdapter(); BitmapbigBitmap=null; if(adapter!=null){ intsize=adapter.getItemCount(); intheight=0; Paintpaint=newPaint(); intiHeight=0; finalintmaxMemory=(int)(Runtime.getRuntime().maxMemory()/1024); //Use1/8thoftheavailablememoryforthismemorycache. finalintcacheSize=maxMemory/8; LruCachebitmaCache=newLruCache<>(cacheSize); for(inti=0;i 六、WebView截屏
1、截取webView可视区域的截图
/** *截取webView可视区域的截图 *@paramwebView前提:WebView要设置webView.setDrawingCacheEnabled(true); *@return */ privateBitmapcaptureWebViewVisibleSize(WebViewwebView){ Bitmapbmp=webView.getDrawingCache(); returnbmp; }2、截取webView的整个页面
/** *截取webView快照(webView加载的整个内容的大小) *@paramwebView *@return */ privateBitmapcaptureWebView(WebViewwebView){ PicturesnapShot=webView.capturePicture(); Bitmapbmp=Bitmap.createBitmap(snapShot.getWidth(),snapShot.getHeight(),Bitmap.Config.ARGB_8888); Canvascanvas=newCanvas(bmp); snapShot.draw(canvas); returnbmp; }3、截取手机屏幕,获取界面展示的webview
/** *截屏 * *@paramcontext *@return */ privateBitmapcaptureScreen(Activitycontext){ Viewcv=context.getWindow().getDecorView(); Bitmapbmp=Bitmap.createBitmap(cv.getWidth(),cv.getHeight(),Bitmap.Config.ARGB_8888); Canvascanvas=newCanvas(bmp); cv.draw(canvas); returnbmp; }感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!