Android截屏相关以及裁图、保存SD卡
2012-11-19 12:49
323 查看
public class ScreenShot {
// 获取指定Activity的截屏,保存到png文件
private static Bitmap takeScreenShot(Activity activity) {
// View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
// 获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.i("TAG", "" + statusBarHeight);
// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
// 去掉标题栏
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
// 保存到sdcard
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 void shoot(Activity a) {
ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
}
}
需要注意的是,shoot方法只能在view已经被加载后方可调用。
或者在 @Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
ScreenShot.shoot(this);
}中调用
// 获取指定Activity的截屏,保存到png文件
private static Bitmap takeScreenShot(Activity activity) {
// View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
// 获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.i("TAG", "" + statusBarHeight);
// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
// 去掉标题栏
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
// 保存到sdcard
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 void shoot(Activity a) {
ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
}
}
需要注意的是,shoot方法只能在view已经被加载后方可调用。
或者在 @Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
ScreenShot.shoot(this);
}中调用
相关文章推荐
- android截屏:保存一个view的内容为图片并存放到SD卡
- android保存数据到手机内存以及SD卡
- Android ImageView长按保存图片及截屏相关知识
- 16、Android开发基础之把数据存储到SD卡上以及SD卡相关的API
- Android开发——Activity销毁时异步任务的保存和恢复、以及相关的进度条的显示问题
- Android 线程池及多线程下载,Progressbar动态更新以及文件保存sd卡
- android本地、sd卡保存对象或集合,以及读取该对象
- Android 线程池及多线程下载,Progressbar动态更新以及文件保存sd卡
- Android调用原声分享相关的知识以及保存图片读取图片
- Android将sqlite数据库保存到SD卡以及数据库增删改查操作,模糊查询like or
- android本地、sd卡保存对象或集合,以及读取该对象
- JDBC以及相关技术学习(七)----事务机制与保存点
- Android 读取SD卡中文件以及内存使用情况
- android反编译apk方法以及一些相关问题解决方案
- android 实现橡皮擦效果以及保存涂鸦的功能
- Android 选取图片、手机拍照以及保存图片到本地、读取图片
- Android中保存网络图片到SD卡
- vlc-android截屏以及录像功能的实现
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- 截屏且保存至SD卡