android截图代码
16lz
2022-05-26
public static BitmapgetViewBitmap(Viewv){
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();
BitmapcacheBitmap = v.getDrawingCache();
if (cacheBitmap == null ){
// Log.e(TAG,"failedgetViewBitmap("+v+")",new
// RuntimeException());
return null ;
}
Bitmapbitmap = Bitmap.createBitmap(cacheBitmap);
// Restoretheview
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
// 保存到sdcard
// savePic(getViewBitmap(v),"sdcard/xx.png");
private static void savePic(Bitmapb,StringstrFileName){
FileOutputStreamfos = null ;
try {
fos = new FileOutputStream(strFileName);
if ( null != fos){
b.compress(Bitmap.CompressFormat.PNG, 90 ,fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundExceptione){
e.printStackTrace();
} catch (IOExceptione){
e.printStackTrace();
}
}
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();
BitmapcacheBitmap = v.getDrawingCache();
if (cacheBitmap == null ){
// Log.e(TAG,"failedgetViewBitmap("+v+")",new
// RuntimeException());
return null ;
}
Bitmapbitmap = Bitmap.createBitmap(cacheBitmap);
// Restoretheview
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
// 保存到sdcard
// savePic(getViewBitmap(v),"sdcard/xx.png");
private static void savePic(Bitmapb,StringstrFileName){
FileOutputStreamfos = null ;
try {
fos = new FileOutputStream(strFileName);
if ( null != fos){
b.compress(Bitmap.CompressFormat.PNG, 90 ,fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundExceptione){
e.printStackTrace();
} catch (IOExceptione){
e.printStackTrace();
}
}
更多相关文章
- 『ANDROID』反射取子类、父类 属性
- Android数据缓存
- android post get
- Android(安卓)返回键退出APP
- Android为返回键设置动画效果
- android Style应用
- 2011.07.12——— android Foreground service
- Android中遇到问题:file.delete()不能删除文件
- android webview点击返回键返回上一个html