[转]Android 屏幕截图 代码
2011-09-08 15:45
323 查看
本文转自:/article/5859011.html
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();
}
}
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();
}
}
相关文章推荐
- Android 屏幕截图 代码
- Android 屏幕截图 代码
- Android 代码实现屏幕截图功能
- Android手机截图代码(针对root设备,可截取任意屏幕)
- Android 屏幕截图 代码
- Android 开发代码教你屏幕截图
- Android 屏幕截图 代码
- 实现屏幕截图的代码-Android
- 实现屏幕截图的代码-Android
- android中实现屏幕截图的代码
- Android Surface 屏幕截图
- Android getDecorView用途——屏幕截图
- android获取屏幕分辨率代码
- 获取和保存当前屏幕的截图 实现的C++代码如下
- Android中使用代码截图的各种方法总结
- Android 各种屏幕截图方法
- Android 获取屏幕高度,标题高度,状态栏高度(实例代码)
- 【Android代码片段之八】监听Android屏幕是否锁屏
- react-native 中配置极光推送 Android关键详细的说明与截图代码 找不到类Bundle/JPushInterface
- Native.js屏幕截图实例代码