在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();
		}
	}

posted on 2013-05-12 15:12  纯洁的坏蛋  阅读(168)  评论(0编辑  收藏  举报