当前位置: 代码迷 >> Android >> android 截屏的三种步骤
  详细解决方案

android 截屏的三种步骤

热度:81   发布时间:2016-04-28 06:39:01.0
android 截屏的三种方法

1. 采用API做法,这种做法的好处是不需要特殊的权限处理。不过在一种情况下不能用,就是在打开camera的情况下使用这种方法:

?

private static Bitmap takeScreenShot(Activity activity) {		// View是你需要截图的View		View view = activity.getWindow().getDecorView();		view.setDrawingCacheEnabled(true);		view.buildDrawingCache();		Bitmap b1 = null;		try {			b1 = view.getDrawingCache();		} catch (OutOfMemoryError e) {			// TODO: handle exception		}		// 获取状态栏高度		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;	}

?

2.采用Surface的隐藏方法,不过这种方法需要的源码中重新build.

?

void takeScreenshot() {		// We need to orient the screenshot correctly (and the Surface api seems to take screenshots		// only in the natural orientation of the device :!)		mDisplay.getRealMetrics(mDisplayMetrics);		float[] dims = { mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels };		float degrees = getDegreesForRotation(mDisplay.getRotation());		boolean requiresRotation = (degrees > 0);		if (requiresRotation) {			// Get the dimensions of the device in its native orientation			mDisplayMatrix.reset();			mDisplayMatrix.preRotate(-degrees);			mDisplayMatrix.mapPoints(dims);			dims[0] = Math.abs(dims[0]);			dims[1] = Math.abs(dims[1]);		}		// Take the screenshot		mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]);		// mScreenBitmap = android.view.SurfaceControl.screenshot((int) dims[0], (int) dims[1]);		if (mScreenBitmap == null) {			// notifyScreenshotError(mContext, mNotificationManager);			// finisher.run();			Log.i("W", "mScreenBitmap null");			return;		}		if (requiresRotation) {			// Rotate the screenshot to the current orientation			Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);			Canvas c = new Canvas(ss);			c.translate(ss.getWidth() / 2, ss.getHeight() / 2);			c.rotate(degrees);			c.translate(-dims[0] / 2, -dims[1] / 2);			c.drawBitmap(mScreenBitmap, 0, 0, null);			c.setBitmap(null);			// Recycle the previous bitmap			mScreenBitmap.recycle();			mScreenBitmap = ss;		}		// Optimizations		mScreenBitmap.setHasAlpha(false);		mScreenBitmap.prepareToDraw();		SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SS");		String name = sdf.format(new Date()) + ".jpg";		savePic(mScreenBitmap, name);		// Start the post-screenshot animation		// startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, statusBarVisible, navBarVisible);	}

?

3.采用命令的方式,这种方式的4.0之后都可以(包括4.0),如果要在程序中实现,需要系统权限。

? ??

screencap /mnt/sdcard/screen.png

? ? ? 4.0之后也提供screenrecord录制屏幕的方法:

? ?

screenrecord --h                                     Usage: screenrecord [options] <filename>Records the device's display to a .mp4 file.Options:--size WIDTHxHEIGHT    Set the video size, e.g. "1280x720".  Default is the device's main    display resolution (if supported), 1280x720 if not.  For best results,    use a size supported by the AVC encoder.--bit-rate RATE    Set the video bit rate, in megabits per second.  Default 4Mbps.--time-limit TIME    Set the maximum recording time, in seconds.  Default / maximum is 180.--rotate    Rotate the output 90 degrees.--verbose    Display interesting information on stdout.--help    Show this message.Recording continues until Ctrl-C is hit or the time limit is reached.

?

?

?

  相关解决方案