发布网友
共1个回答
热心网友
截取 view 的根层可见屏幕部分的视图 * @param activity
* @return */ public static Bitmap getRootViewBitmap(View view) { return shotViewBitmap(view.getRootView()); } /** * 截取可见屏幕部分的 view 视图 * @param activity * @return */ public static Bitmap shotViewBitmap(View v) { v.clearFocus(); v.setPressed(false); Bitmap bmp = null; try { v.layout(0, 0, v.getWidth(), v.getHeight()); // 允许当前窗口保存缓存信息,这样 getDrawingCache()方法才会返回一个 Bitmap v.setDrawingCacheEnabled(true); v.buildDrawingCache(); bmp = Bitmap.createBitmap(v.getDrawingCache()); } catch (Exception e) { e.printStackTrace(); } return bmp; } /** * convert view to integral bitmap and return it * 获取 view 的完整视图图片(即使没有显示出来的部分) * @param view : view or layout * @return */ public static Bitmap convertBitmap(View view){ return convertViewToBitmap(view, view.getWidth(), view.getHeight()); } /** * convert measured view to integral bitmap and return it * 通过计算的方法宽高后,获取 view 的完整视图图片(即使没有显示出来的部分) * @param view : view or layout * @return */
public static Bitmap convertMeasureBitmap(View view){ view.measure(MeasureSpec.makeMeasureSpec(view.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(view.getHeight(), MeasureSpec.AT_MOST)); return convertViewToBitmap(view, view.getMeasuredWidth(), view.getMeasuredHeight()); } /** * convert view to bitmap according to with and height * @param view * @param width * @param height * @return */ public static Bitmap convertViewToBitmap(View view, int width, int height){ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); view.draw(new Canvas(bitmap)); return bitmap; } }