android截图代码
16lz
2022-04-20
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(安卓)input keyevent
- android保存图片到图库
- Android(安卓)view 转化为Bitmap并保存在本地
- android中dialog工具类的实现(多种dialog的创建)
- android 文件目录权限
- android经常调用的组件
- Android(安卓)- 开发常用工具类Utils
- React Native物理back返回键的使用
- android 获取sd卡根目录