博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Android 屏幕截图 代码

Posted on 2011-09-05 11:52  梦回鲁南  阅读(1585)  评论(1编辑  收藏  举报
    public static Bitmap getViewBitmap(View v) {

v.clearFocus();
v.setPressed(
false);

//能画缓存就返回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) {
// 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;
}


//保存到sdcard
// savePic(getViewBitmap(v), "sdcard/xx.png");
private static void savePic(Bitmap b,String strFileName){
FileOutputStream fos
= null;
try {
fos
= new FileOutputStream(strFileName);
if (null != fos)
{
b.compress(Bitmap.CompressFormat.PNG,
90, fos);
fos.flush();
fos.close();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
无觅相关文章插件,快速提升流量