尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Android实现截屏与截长图功能

Android实现截屏与截长图功能 本文实例为大家分享了Android实现截屏与截长图功能展示的具体代码供大家参考具体内容如下Demo在GitHub的地址ScreenShootDemo在CSDN上的下载地址Android实现截屏与截长图功能在Android开发中有时候会遇到需要截屏分享到朋友圈或者QQ截屏有截取当前屏幕也有需要截取不仅一个屏幕可能会很长。截取当前屏幕并保存到内存卡的方法代码语言javascript// 获取指定Activity的截屏保存到png文件publicstaticBitmaptakeScreenShot(Activity activity){// View是你需要截图的ViewView viewactivity.getWindow().getDecorView();view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap b1view.getDrawingCache();// 获取状态栏高度Rect framenewRect();activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);int statusBarHeightframe.top;System.out.println(statusBarHeight);// 获取屏幕长和高int widthactivity.getWindowManager().getDefaultDisplay().getWidth();int heightactivity.getWindowManager().getDefaultDisplay().getHeight();// 去掉标题栏// Bitmap b Bitmap.createBitmap(b1, 0, 25, 320, 455);Bitmap bBitmap.createBitmap(b1,0,statusBarHeight,width,height-statusBarHeight);view.destroyDrawingCache();returnb;}// 保存到sdcardpublicstaticvoidsavePic(Bitmap b,String strFileName){FileOutputStream fosnull;try{fosnewFileOutputStream(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();}}// 程序入口 截取当前屏幕publicstaticvoidshootLoacleView(Activity a,String picpath){ScreenShot.savePic(ScreenShot.takeScreenShot(a),picpath);}当视图超过一个屏幕的时候可能是listview也可能是Scrollview这时候其实截图就是对listview或者Scrollview进行截图代码语言javascript/** * 截取scrollview的屏幕 * **/publicstaticBitmapgetScrollViewBitmap(ScrollView scrollView,String picpath){int h0;Bitmap bitmap;// 获取listView实际高度for(int i0;iscrollView.getChildCount();i){hscrollView.getChildAt(i).getHeight();}Log.d(TAG,实际高度:h);Log.d(TAG, 高度:scrollView.getHeight());// 创建对应大小的bitmapbitmapBitmap.createBitmap(scrollView.getWidth(),h,Bitmap.Config.ARGB_8888);final Canvas canvasnewCanvas(bitmap);scrollView.draw(canvas);// 测试输出FileOutputStream outnull;try{outnewFileOutputStream(picpath);}catch(FileNotFoundException e){e.printStackTrace();}try{if(null!out){bitmap.compress(Bitmap.CompressFormat.PNG,100,out);out.flush();out.close();}}catch(IOException e){}returnbitmap;}privatestaticStringTAGListview and ScrollView item 截图:;/** * 截图listview * **/publicstaticBitmapgetListViewBitmap(ListView listView,String picpath){int h0;Bitmap bitmap;// 获取listView实际高度for(int i0;ilistView.getChildCount();i){hlistView.getChildAt(i).getHeight();}Log.d(TAG,实际高度:h);Log.d(TAG,list 高度:listView.getHeight());// 创建对应大小的bitmapbitmapBitmap.createBitmap(listView.getWidth(),h,Bitmap.Config.ARGB_8888);final Canvas canvasnewCanvas(bitmap);listView.draw(canvas);// 测试输出FileOutputStream outnull;try{outnewFileOutputStream(picpath);}catch(FileNotFoundException e){e.printStackTrace();}try{if(null!out){bitmap.compress(Bitmap.CompressFormat.PNG,100,out);out.flush();out.close();}}catch(IOException e){}returnbitmap;}// 程序入口 截取ScrollViewpublicstaticvoidshootScrollView(ScrollView scrollView,String picpath){ScreenShot.savePic(getScrollViewBitmap(scrollView,picpath),picpath);}// 程序入口 截取ListViewpublicstaticvoidshootListView(ListView listView,String picpath){ScreenShot.savePic(getListViewBitmap(listView,picpath),picpath);}截长图的效果图以上就是本文的全部内容希望对大家的学习有所帮助。更多Android进阶指南 可以扫码 解锁《Android十大板块文档》1.Android车载应用开发系统学习指南附项目实战2.Android Framework学习指南助力成为系统级开发高手3.2024最新Android中高级面试题汇总解析告别零offer4.企业级Android音视频开发学习路线项目实战附源码5.Android Jetpack从入门到精通构建高质量UI界面6.Flutter技术解析与实战跨平台首要之选7.Kotlin从入门到实战全方面提升架构基础8.高级Android插件化与组件化含实战教程和源码9.Android 性能优化实战360°全方面性能调优10.Android零基础入门到精通高手进阶之路敲代码不易关注一下吧。ღ( ´ᴗ )
返回列表