android截图代码

来源:互联网 发布:空气净化器买哪个知乎 编辑:程序博客网 时间:2024/06/07 01:43

在pc上的截图软件很多,但是android上的比较少,所以就自己写了一个,下面时截图软件的核心代码private Bitmap getViewBitmap(View v) { // 将一个View转化成一张图片

Java代码  收藏代码
  1.     v.clearFocus(); // 清除视图焦点  
  2.     v.setPressed(false);// 将视图设为不可点击  
  3.   
  4.     boolean willNotCache = v.willNotCacheDrawing(); // 返回视图是否可以保存他的画图缓存  
  5.     v.setWillNotCacheDrawing(false);  
  6.   
  7.     // Reset the drawing cache background color to fully transparent  
  8.     // for the duration of this operation //将视图在此操作时置为透明  
  9.     int color = v.getDrawingCacheBackgroundColor(); // 获得绘制缓存位图的背景颜色  
  10.     v.setDrawingCacheBackgroundColor(0); // 设置绘图背景颜色  
  11.     if (color != 0) { // 如果获得的背景不是黑色的则释放以前的绘图缓存  
  12.         v.destroyDrawingCache(); // 释放绘图资源所使用的缓存  
  13.     }  
  14.     v.buildDrawingCache(); // 重新创建绘图缓存,此时的背景色是黑色  
  15.     Bitmap cacheBitmap = v.getDrawingCache(); // 将绘图缓存得到的,注意这里得到的只是一个图像的引用  
  16.     if (cacheBitmap == null) {  
  17.         return null;  
  18.     }  
  19.     Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // 将位图实例化  
  20.     // Restore the view //恢复视图  
  21.     v.destroyDrawingCache();// 释放位图内存  
  22.     v.setWillNotCacheDrawing(willNotCache);// 返回以前缓存设置  
  23.     v.setDrawingCacheBackgroundColor(color);// 返回以前的缓存颜色设置  
  24.     return bitmap;  
  25. }  
原创粉丝点击