Android 开发代码教你屏幕截图
2015-08-27 22:23
671 查看
http://jingyan.baidu.com/article/1e5468f9278b0d484861b766.html
Android 开发代码教你屏幕截图
具体代码如下:import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;
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;
System.out.println(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");
}
}
相关文章推荐
- Android编程之对话框
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
- Android Studio, Failed to install Intel HAXM
- Android学习0827<九>(Toast、AlertDialog )
- Android常用组件(Activity学习之一)
- Android的Configuration类
- Android SDK 在线更新镜像服务器
- Android:学习笔记(二)
- 0827Android基础Toast+AlertDialog
- Android开发总结
- android自定义对话框
- <Android>画布的移动和翻转
- Android ImageView
- Android学习心得(15) --- Dex文件结构解析(1)
- Android Studio (Gradle)编译错误
- Android颜色对照表
- Android 线程详解
- Android不编译某个模块
- Android中Preference的使用以及监听事件分析
- Android-数据存储(Content Provider 跨应用调用数据实例)