android 关于截屏
在android4.0以下用这个方法:
private void cutScreen()
{
View v = getLayoutInflater().inflate(R.layout.activity_main, null);
//打开图像缓存
v.setDrawingCacheEnabled(true);
//v.buildDrawingCache();
//测量view的大小
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
//发送位置和尺寸到View及其所有的子View
/*View v = this.getWindow().getDecorView();
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();*/
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
LogHelper.LogV("" + v.getMeasuredWidth() + " " + v.getMeasuredHeight());
try {
Bitmap bitmap = v.getDrawingCache();
FileOutputStream fos = new FileOutputStream("/sdcard/test.png");
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
LogHelper.LogE("截屏成功");
} catch (Exception e) {
// TODO Auto-generated catch block
LogHelper.LogE("截屏失败 + " + e.getMessage());
e.printStackTrace();
}
}
如果是android 4.0以上可以这样用:
private void cutScreen()
{
/* View v = getLayoutInflater().inflate(R.layout.activity_main, null);
//打开图像缓存
v.setDrawingCacheEnabled(true);
//v.buildDrawingCache();
//测量view的大小
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));*/
//发送位置和尺寸到View及其所有的子View
View v = this.getWindow().getDecorView();
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
LogHelper.LogV("" + v.getMeasuredWidth() + " " + v.getMeasuredHeight());
try {
Bitmap bitmap = v.getDrawingCache();
FileOutputStream fos = new FileOutputStream("/sdcard/test.png");
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
LogHelper.LogE("截屏成功");
} catch (Exception e) {
// TODO Auto-generated catch block
LogHelper.LogE("截屏失败 + " + e.getMessage());
e.printStackTrace();
}
}

浙公网安备 33010602011771号