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.os.Environment;import android.view.View;//获取指定Activity的截屏,保存到png文件public class ScreenShot {private static Bitmap takeScreenShot(Activity activity) {// View是你需要截图的ViewView 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;} // 保存到sdcardprivate 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), Environment.getExternalStorageDirectory()+"/xx.png");}}

来源于网络

更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. android 获取手机中的联系人
  3. Android动态获取当前手机IP地址
  4. Android获取包相关信息--code
  5. android sqlite 图片保存和读出
  6. android中的状态保存
  7. 获取Android系统信息
  8. Android动态添加删除recycleview并动态保存recycleview中的的数
  9. Android(安卓)打开文件管理器 获取文件的真实路径

随机推荐

  1. android TextView中间删除线显示
  2. android 为textView设置selector
  3. Android Popupwindow 显示ListView
  4. Window下使用android NDK开发Android应用
  5. Android 界面的横竖屏监听
  6. 4. android 设置全屏且无标题
  7. import eclipse project to android stud
  8. 要好好研究的一篇关于android的博文
  9. Android实现左右滑动指引效果
  10. Android打电话&发短信