您好,登錄后才能下訂單哦!
Android屏幕及view的截圖實例詳解
屏幕可見區域的截圖
整個屏幕截圖的話可以用View view = getWindow().getDecorView();
public static Bitmap getNormalViewScreenshot(View view) { view.setDrawingCacheEnabled(true); view.buildDrawingCache(); return view.getDrawingCache(); }
scrollview的整體截屏
public static Bitmap getWholeScrollViewToBitmap(View view) { view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; }
webview的整體截圖
public static Bitmap getWholeWebViewToBitmap(WebView webView) { Picture snapShot = webView.capturePicture(); Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); snapShot.draw(canvas); return bmp; }
listview的整體截圖
public static Bitmap getWholeListViewItemsToBitmap(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(MeasureSpec.makeMeasureSpec(listview.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight()); childView.setDrawingCacheEnabled(true); childView.buildDrawingCache(); bmps.add(childView.getDrawingCache()); allitemsheight += childView.getMeasuredHeight(); } Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), 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; }
需要多次截圖的話,需要用到 view.destroyDrawingCache();
Bitmap normalViewScreenshot = ScreenShotUtils.getNormalViewScreenshot(mFrameContent); if (normalViewScreenshot != null) { Bitmap b = Bitmap.createBitmap(normalViewScreenshot); mImageResult.setImageBitmap(b); mFrameContent.destroyDrawingCache(); }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。