android截图代码
16lz
2022-04-19
publicstaticBitmapgetViewBitmap(Viewv){
v.clearFocus();//
v.setPressed(false);//
//能画缓存就返回false
booleanwillNotCache=v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
intcolor=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());
returnnull;
}
Bitmapbitmap=Bitmap.createBitmap(cacheBitmap);
//Restoretheview
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
returnbitmap;
}
//保存到sdcard
//savePic(getViewBitmap(v),"sdcard/xx.png");
privatestaticvoidsavePic(Bitmapb,StringstrFileName){
FileOutputStreamfos=null;
try{
fos=newFileOutputStream(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
booleanwillNotCache=v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
intcolor=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());
returnnull;
}
Bitmapbitmap=Bitmap.createBitmap(cacheBitmap);
//Restoretheview
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
returnbitmap;
}
//保存到sdcard
//savePic(getViewBitmap(v),"sdcard/xx.png");
privatestaticvoidsavePic(Bitmapb,StringstrFileName){
FileOutputStreamfos=null;
try{
fos=newFileOutputStream(strFileName);
if(null!=fos){
b.compress(Bitmap.CompressFormat.PNG,90,fos);
fos.flush();
fos.close();
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
更多相关文章
- android保存数据到xml以及pull解析模板
- 58. SaveVolley
- android 异步下载图片
- Android文件操作
- Android(安卓)双击返回键退出功能的实现
- android 文件保存方法 sd卡中或系统
- greendao接入sql和android cursor的简单应用
- android 按两次返回键退出
- android 以httpclient方式把数据提交到服务器