Java Code Examples for android.view.View#setWillNotCacheDrawing()
The following examples show how to use
android.view.View#setWillNotCacheDrawing() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ViewUtils.java From Cotable with Apache License 2.0 | 6 votes |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) public static Bitmap b(View view) { Bitmap bitmap = null; view.measure(View.MeasureSpec.makeMeasureSpec(0, 0), View.MeasureSpec.makeMeasureSpec(0, 0)); int i = view.getMeasuredWidth(); int j = view.getMeasuredHeight(); view.layout(0, 0, i, j); view.setDrawingCacheEnabled(false); view.setWillNotCacheDrawing(true); view.setDrawingCacheEnabled(false); view.setWillNotCacheDrawing(true); if (i > 0 && j > 0) { bitmap = Bitmap.createBitmap(i, j, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); if (Build.VERSION.SDK_INT >= 11) view.setLayerType(1, null); view.draw(canvas); } return bitmap; }
Example 2
Source File: Utils.java From Noyze with Apache License 2.0 | 5 votes |
public static Bitmap loadBitmapFromViewCache(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (null == cacheBitmap) return null; Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 3
Source File: BgSparklineBuilder.java From NightWatch with GNU General Public License v3.0 | 5 votes |
/** * Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 4
Source File: ViewShot.java From zone-sdk with MIT License | 5 votes |
/** * @param view * @return * @deprecated use {@link #getCacheBitmap(View,Bitmap.Config)} * <br>感觉这个不好 会清除一些状态</br> * 仅仅为了得到的位图是RGB 565吧 * 但是 结果来看 大部分还是RGB8888 要和 View.mUse32BitDrawingCache相关 */ public static Bitmap getCacheBitmap_ConifgByWindow(View view) { // 将一个View转化成一张图片 view.clearFocus(); // 清除视图焦点 view.setPressed(false);// 将视图设为不可点击 boolean willNotCache = view.willNotCacheDrawing(); // 返回视图是否可以保存他的画图缓存 view.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation //将视图在此操作时置为透明 int color = view.getDrawingCacheBackgroundColor(); // 获得绘制缓存位图的背景颜色 view.setDrawingCacheBackgroundColor(0); // 设置绘图背景颜色 if (color != 0) { // 如果获得的背景不是黑色的则释放以前的绘图缓存 view.destroyDrawingCache(); // 释放绘图资源所使用的缓存 } view.buildDrawingCache(); // 重新创建绘图缓存,此时的背景色是黑色 Bitmap cacheBitmap = view.getDrawingCache(); // 将绘图缓存得到的,注意这里得到的只是一个图像的引用 if (cacheBitmap == null) return failLog(view); Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // 将位图实例化 // Restore the view //恢复视图 view.destroyDrawingCache();// 释放位图内存 view.setWillNotCacheDrawing(willNotCache);// 返回以前缓存设置 view.setDrawingCacheBackgroundColor(color);// 返回以前的缓存颜色设置 return bitmap; }
Example 5
Source File: BgSparklineBuilder.java From NightWatch with GNU General Public License v3.0 | 5 votes |
/** * Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 6
Source File: BitmapUtil.java From AndroidStudyDemo with GNU General Public License v2.0 | 5 votes |
/** * 把一个View的对象转换成bitmap * * @param view View * @return Bitmap */ public static Bitmap getBitmapFromView2(View view) { view.clearFocus(); view.setPressed(false); // 能画缓存就返回false boolean willNotCache = view.willNotCacheDrawing(); view.setWillNotCacheDrawing(false); int color = view.getDrawingCacheBackgroundColor(); view.setDrawingCacheBackgroundColor(0); if (color != 0) { view.destroyDrawingCache(); } view.buildDrawingCache(); Bitmap cacheBitmap = view.getDrawingCache(); if (cacheBitmap == null) { if (DEBUG) { Logger.e("failed getViewBitmap(" + view + ")", new RuntimeException()); } return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view view.destroyDrawingCache(); view.setWillNotCacheDrawing(willNotCache); view.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 7
Source File: FileOperateUtils.java From LLApp with Apache License 2.0 | 5 votes |
public static Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e("TTTTTTTTActivity", "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 8
Source File: BgSparklineBuilder.java From xDrip-plus with GNU General Public License v3.0 | 5 votes |
/** * Draw the view into a bitmap. */ protected Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(Color.TRANSPARENT); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { android.util.Log.e(TAG, "failed getViewBitmap(" + JoH.backTrace() + ")", new RuntimeException()); v.destroyDrawingCache(); // duplicate of below, flow could be reordered better v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 9
Source File: BgSparklineBuilder.java From NightWatch with GNU General Public License v3.0 | 5 votes |
/** * Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 10
Source File: BgSparklineBuilder.java From xDrip with GNU General Public License v3.0 | 5 votes |
/** * Draw the view into a bitmap. */ protected Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(Color.TRANSPARENT); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { android.util.Log.e(TAG, "failed getViewBitmap(" + JoH.backTrace() + ")", new RuntimeException()); v.destroyDrawingCache(); // duplicate of below, flow could be reordered better v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 11
Source File: DragController.java From LB-Launcher with Apache License 2.0 | 5 votes |
/** * Draw the view into a bitmap. */ Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); float alpha = v.getAlpha(); v.setAlpha(1.0f); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setAlpha(alpha); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 12
Source File: ImageUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
/** * View to bitmap. * * @param view The view. * @return bitmap */ public static Bitmap view2Bitmap(final View view) { if (view == null) return null; boolean drawingCacheEnabled = view.isDrawingCacheEnabled(); boolean willNotCacheDrawing = view.willNotCacheDrawing(); view.setDrawingCacheEnabled(true); view.setWillNotCacheDrawing(false); Bitmap drawingCache = view.getDrawingCache(); Bitmap bitmap; if (null == drawingCache) { view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); drawingCache = view.getDrawingCache(); if (drawingCache != null) { bitmap = Bitmap.createBitmap(drawingCache); } else { bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); } } else { bitmap = Bitmap.createBitmap(drawingCache); } view.destroyDrawingCache(); view.setWillNotCacheDrawing(willNotCacheDrawing); view.setDrawingCacheEnabled(drawingCacheEnabled); return bitmap; }
Example 13
Source File: BgSparklineBuilder.java From NightWatch with GNU General Public License v3.0 | 5 votes |
/** * Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 14
Source File: DragDropHelper.java From timecat with Apache License 2.0 | 5 votes |
private Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 15
Source File: ScreenUtils.java From Common with Apache License 2.0 | 5 votes |
/** * Return the bitmap of screen. * * @param activity The activity. * @param isDeleteStatusBar True to delete status bar, false otherwise. * @return the bitmap of screen */ public static Bitmap screenShot(@NonNull final Activity activity, boolean isDeleteStatusBar) { View decorView = activity.getWindow().getDecorView(); boolean drawingCacheEnabled = decorView.isDrawingCacheEnabled(); boolean willNotCacheDrawing = decorView.willNotCacheDrawing(); decorView.setDrawingCacheEnabled(true); decorView.setWillNotCacheDrawing(false); Bitmap bmp = decorView.getDrawingCache(); if (bmp == null) { decorView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); decorView.layout(0, 0, decorView.getMeasuredWidth(), decorView.getMeasuredHeight()); decorView.buildDrawingCache(); bmp = Bitmap.createBitmap(decorView.getDrawingCache()); } if (bmp == null) { return null; } DisplayMetrics dm = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(dm); Bitmap ret; if (isDeleteStatusBar) { Resources resources = activity.getResources(); int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); int statusBarHeight = resources.getDimensionPixelSize(resourceId); ret = Bitmap.createBitmap(bmp, 0, statusBarHeight, dm.widthPixels, dm.heightPixels - statusBarHeight); } else { ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels); } decorView.destroyDrawingCache(); decorView.setWillNotCacheDrawing(willNotCacheDrawing); decorView.setDrawingCacheEnabled(drawingCacheEnabled); return ret; }
Example 16
Source File: ImageUtils.java From DevUtils with Apache License 2.0 | 5 votes |
/** * 通过 View Cache 绘制为 Bitmap * @param view {@link View} * @return {@link Bitmap} */ public static Bitmap getBitmapFromViewCache(final View view) { if (view == null) return null; try { // 清除视图焦点 view.clearFocus(); // 将视图设为不可点击 view.setPressed(false); // 获取视图是否可以保存画图缓存 boolean willNotCache = view.willNotCacheDrawing(); view.setWillNotCacheDrawing(false); // 获取绘制缓存位图的背景颜色 int color = view.getDrawingCacheBackgroundColor(); // 设置绘图背景颜色 view.setDrawingCacheBackgroundColor(0); if (color != 0) { // 获取的背景不是黑色的则释放以前的绘图缓存 view.destroyDrawingCache(); // 释放绘图资源所使用的缓存 } // 重新创建绘图缓存, 此时的背景色是黑色 view.buildDrawingCache(); // 获取绘图缓存, 注意这里得到的只是一个图像的引用 Bitmap cacheBitmap = view.getDrawingCache(); if (cacheBitmap == null) return null; Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // 释放位图内存 view.destroyDrawingCache(); // 回滚以前的缓存设置、缓存颜色设置 view.setWillNotCacheDrawing(willNotCache); view.setDrawingCacheBackgroundColor(color); return bitmap; } catch (Exception e) { LogPrintUtils.eTag(TAG, e, "getBitmapFromViewCache"); } return null; }
Example 17
Source File: Utils.java From Noyze with Apache License 2.0 | 5 votes |
public static Bitmap loadBitmapFromViewCache(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (null == cacheBitmap) return null; Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 18
Source File: ConvertUtils.java From AndroidPicker with MIT License | 4 votes |
/** * 把view转化为bitmap(截图) * 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html */ public static Bitmap toBitmap(View view) { int width = view.getWidth(); int height = view.getHeight(); if (view instanceof ListView) { height = 0; // 获取listView实际高度 ListView listView = (ListView) view; for (int i = 0; i < listView.getChildCount(); i++) { height += listView.getChildAt(i).getHeight(); } } else if (view instanceof ScrollView) { height = 0; // 获取scrollView实际高度 ScrollView scrollView = (ScrollView) view; for (int i = 0; i < scrollView.getChildCount(); i++) { height += scrollView.getChildAt(i).getHeight(); } } view.setDrawingCacheEnabled(true); view.clearFocus(); view.setPressed(false); boolean willNotCache = view.willNotCacheDrawing(); view.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent for the duration of this operation int color = view.getDrawingCacheBackgroundColor(); view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素) if (color != Color.WHITE) { view.destroyDrawingCache(); } view.buildDrawingCache(); Bitmap cacheBitmap = view.getDrawingCache(); if (cacheBitmap == null) { return null; } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(cacheBitmap, 0, 0, null); canvas.save(); canvas.restore(); if (!bitmap.isRecycled()) { LogUtils.verbose("recycle bitmap: " + bitmap.toString()); bitmap.recycle(); } // Restore the view view.destroyDrawingCache(); view.setWillNotCacheDrawing(willNotCache); view.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 19
Source File: ConvertUtils.java From MyBookshelf with GNU General Public License v3.0 | 4 votes |
/** * 把view转化为bitmap(截图) * 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html */ public static Bitmap toBitmap(View view) { int width = view.getWidth(); int height = view.getHeight(); if (view instanceof ListView) { height = 0; // 获取listView实际高度 ListView listView = (ListView) view; for (int i = 0; i < listView.getChildCount(); i++) { height += listView.getChildAt(i).getHeight(); } } else if (view instanceof ScrollView) { height = 0; // 获取scrollView实际高度 ScrollView scrollView = (ScrollView) view; for (int i = 0; i < scrollView.getChildCount(); i++) { height += scrollView.getChildAt(i).getHeight(); } } view.setDrawingCacheEnabled(true); view.clearFocus(); view.setPressed(false); boolean willNotCache = view.willNotCacheDrawing(); view.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent for the duration of this operation int color = view.getDrawingCacheBackgroundColor(); view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素) if (color != Color.WHITE) { view.destroyDrawingCache(); } view.buildDrawingCache(); Bitmap cacheBitmap = view.getDrawingCache(); if (cacheBitmap == null) { return null; } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(cacheBitmap, 0, 0, null); canvas.save(); canvas.restore(); if (!bitmap.isRecycled()) { bitmap.recycle(); } // Restore the view view.destroyDrawingCache(); view.setWillNotCacheDrawing(willNotCache); view.setDrawingCacheBackgroundColor(color); return bitmap; }
Example 20
Source File: ConvertUtils.java From FilePicker with MIT License | 4 votes |
/** * 把view转化为bitmap(截图) * 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html */ public static Bitmap toBitmap(View view) { int width = view.getWidth(); int height = view.getHeight(); if (view instanceof ListView) { height = 0; // 获取listView实际高度 ListView listView = (ListView) view; for (int i = 0; i < listView.getChildCount(); i++) { height += listView.getChildAt(i).getHeight(); } } else if (view instanceof ScrollView) { height = 0; // 获取scrollView实际高度 ScrollView scrollView = (ScrollView) view; for (int i = 0; i < scrollView.getChildCount(); i++) { height += scrollView.getChildAt(i).getHeight(); } } view.setDrawingCacheEnabled(true); view.clearFocus(); view.setPressed(false); boolean willNotCache = view.willNotCacheDrawing(); view.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent for the duration of this operation int color = view.getDrawingCacheBackgroundColor(); view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素) if (color != Color.WHITE) { view.destroyDrawingCache(); } view.buildDrawingCache(); Bitmap cacheBitmap = view.getDrawingCache(); if (cacheBitmap == null) { return null; } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(cacheBitmap, 0, 0, null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); if (!bitmap.isRecycled()) { LogUtils.verbose("recycle bitmap: " + bitmap.toString()); bitmap.recycle(); } // Restore the view view.destroyDrawingCache(); view.setWillNotCacheDrawing(willNotCache); view.setDrawingCacheBackgroundColor(color); return bitmap; }