/**
* 对屏幕进行截图
* @author Administrator
*
*/
public class ScreenShot {
public static final String RESULTIMGNAME = "test_result.jpg";
public static String RESULT_IMAGE;
// 获取指定Activity的截屏,保存到png文件
private static Bitmap takeScreenShot(Activity activity, int imageHeight , int titleBarHeight) {
// 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+titleBarHeight, width, imageHeight);
view.destroyDrawingCache();
return b;
}
// 保存到sdcard
private static void savePic(Bitmap b, File file) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
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 File initImagePath(Activity mContext) throws IOException {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& Environment.getExternalStorageDirectory().exists()) {
RESULT_IMAGE = Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator + RESULTIMGNAME;
}
else {
RESULT_IMAGE = mContext.getApplication().getFilesDir().getAbsolutePath() +File.separator + RESULTIMGNAME;
}
File file = new File(RESULT_IMAGE);
if(!file.exists()) {
file.createNewFile();
} else {
file.delete();
file.createNewFile();
}
return file;
}
// 程序入口
public static String shoot(Activity mContext, int imageHeight , int titleBarHeight){
try {
File file = initImagePath(mContext);
ScreenShot.savePic(takeScreenShot(mContext,imageHeight,titleBarHeight), file);
return file.getAbsolutePath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
* 对屏幕进行截图
* @author Administrator
*
*/
public class ScreenShot {
public static final String RESULTIMGNAME = "test_result.jpg";
public static String RESULT_IMAGE;
// 获取指定Activity的截屏,保存到png文件
private static Bitmap takeScreenShot(Activity activity, int imageHeight , int titleBarHeight) {
// 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+titleBarHeight, width, imageHeight);
view.destroyDrawingCache();
return b;
}
// 保存到sdcard
private static void savePic(Bitmap b, File file) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
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 File initImagePath(Activity mContext) throws IOException {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& Environment.getExternalStorageDirectory().exists()) {
RESULT_IMAGE = Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator + RESULTIMGNAME;
}
else {
RESULT_IMAGE = mContext.getApplication().getFilesDir().getAbsolutePath() +File.separator + RESULTIMGNAME;
}
File file = new File(RESULT_IMAGE);
if(!file.exists()) {
file.createNewFile();
} else {
file.delete();
file.createNewFile();
}
return file;
}
// 程序入口
public static String shoot(Activity mContext, int imageHeight , int titleBarHeight){
try {
File file = initImagePath(mContext);
ScreenShot.savePic(takeScreenShot(mContext,imageHeight,titleBarHeight), file);
return file.getAbsolutePath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}