Android截屏截图的几种方法总结 Android截屏截图是 Android 开发中常见的需求, Android 截屏截图方法汇总将为您带来多种截屏截图方法的总结,让您更好地掌握 Android 截屏截图技术。在本文中,我们将探讨 Activity、View、ScrollView、ListView、RecycleView、WebView 等多种截屏截图方法。 一、Activity 截屏截图 Activity 截屏截图是 Android 截屏截图中最基本的方法之一。它可以截取 Activity 界面的截屏,包括状态栏和标题栏等。在 Activity 截屏截图中,我们可以使用 View 的 drawing cache 来获取截屏图像。下面是一个基本的 Activity 截屏截图方法: ```java public static Bitmap shotActivity(Activity context) { View view = context.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache(), 0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.setDrawingCacheEnabled(false); view.destroyDrawingCache(); return bitmap; } ``` 此方法可以截取 Activity 界面的截屏,包括状态栏和标题栏等。但是,如果您想去掉状态栏,可以使用以下方法: ```java public Bitmap shotActivityNoStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.buildDrawingCache(); Rect rect = new Rect(); view.getWindowVisibleDisplayFrame(rect); int statusBarHeights = rect.top; Display display = activity.getWindowManager().getDefaultDisplay(); int widths = display.getWidth(); int heights = display.getHeight(); view.setDrawingCacheEnabled(true); Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeights, widths, heights - statusBarHeights); view.destroyDrawingCache(); return bmp; } ``` 二、View 截屏截图 View 截屏截图是一种更灵活的截屏方法,可以截取任意 View 的截屏。下面是一个基本的 View 截屏截图方法: ```java public static Bitmap getViewBitmap(View v) { if (null == v) { return null; } 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(0, 0, v.getWidth(), v.getHeight()); } Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); v.setDrawingCacheEnabled(false); v.destroyDrawingCache(); return bitmap; } ``` 三、ScrollView 截屏截图 ScrollView 截屏截图是一种特殊的截屏方法,主要用于截取 ScrollView 的内容。下面是一个基本的 ScrollView 截屏截图方法: ```java public static Bitmap getScrollViewBitmap(ScrollView scrollView) { int h = 0; Bitmap bitmap = null; for (int i = 0; i < scrollView.getChildCount(); i++) { View childView = scrollView.getChildAt(i); childView.setDrawingCacheEnabled(true); childView.buildDrawingCache(); Bitmap childBitmap = Bitmap.createBitmap(childView.getDrawingCache()); if (bitmap == null) { bitmap = childBitmap; } else { bitmap = combineBitmap(bitmap, childBitmap); } h += childBitmap.getHeight(); childView.setDrawingCacheEnabled(false); childView.destroyDrawingCache(); } return bitmap; } ``` 四、ListView 截屏截图 ListView 截屏截图是一种特殊的截屏方法,主要用于截取 ListView 的内容。下面是一个基本的 ListView 截屏截图方法: ```java public static Bitmap getListViewBitmap(ListView listView) { ListView 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.setDrawingCacheEnabled(true); 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.buildDrawingCache(); bmps.add(Bitmap.createBitmap(childView.getDrawingCache())); allitemsheight += childView.getMeasuredHeight(); } Bitmap bigbitmap = Bitmap.createBitmap(listView.getWidth(), 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; } ``` 五、RecycleView 截屏截图 RecycleView 截屏截图是一种特殊的截屏方法,主要用于截取 RecycleView 的内容。下面是一个基本的 RecycleView 截屏截图方法: ```java public static Bitmap getRecycleViewBitmap(RecyclerView recyclerView) { RecyclerView.Adapter adapter = recyclerView.getAdapter(); int itemCount = adapter.getItemCount(); int allItemsHeight = 0; List<Bitmap> bmps = new ArrayList<Bitmap>(); for (int i = 0; i < itemCount; i++) { RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i); View childView = viewHolder.itemView; childView.setDrawingCacheEnabled(true); childView.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(childView.getDrawingCache()); bmps.add(bitmap); allItemsHeight += bitmap.getHeight(); } Bitmap bigbitmap = Bitmap.createBitmap(recyclerView.getWidth(), 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; } ``` 六、WebView 截屏截图 WebView 截屏截图是一种特殊的截屏方法,主要用于截取 WebView 的内容。下面是一个基本的 WebView 截屏截图方法: ```java public static Bitmap getWebViewBitmap(WebView webView) { webView.measure(View.MeasureSpec.makeMeasureSpec(webView.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight()); webView.setDrawingCacheEnabled(true); webView.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(webView.getDrawingCache()); webView.setDrawingCacheEnabled(false); webView.destroyDrawingCache(); return bitmap; } ``` Android 截屏截图有多种方法,每种方法都有其特点和应用场景。在实际开发中,您可以根据实际需求选择合适的截屏方法。
- 粉丝: 3
- 资源: 910
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助