AndroID 截图功能源码的分析
一般没有修改rom的androID原生系统截图功能的组合键是音量减+开机键;今天我们从源码角度来分析截图功能是如何在源码中实现的。
在androID系统中,由于我们的每一个AndroID界面都是一个Activity,而界面的显示都是通过Window对象实现的,每个Window对象实际上都是PhoneWindow的实例,而每个PhoneWindow对象都对应一个PhoneWindowManager对象,当我们在Activity界面执行按键 *** 作的时候,在将按键的处理 *** 作分发到App之前,首先会回调PhoneWindowManager中的dispatchUnhandledKey方法,该方法主要用于执行当前App处理按键之前的 *** 作,我们具体看一下该方法的实现。
/** {@inheritDoc} */ @OverrIDe public KeyEvent dispatchUnhandledKey(windowstate win,KeyEvent event,int policyFlags) { ... KeyEvent fallbackEvent = null; if ((event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) { final KeyCharacterMap kcm = event.getKeyCharacterMap(); final int keyCode = event.getKeyCode(); final int MetaState = event.getMetaState(); final boolean initialDown = event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0; // Check for fallback actions specifIEd by the key character map. final FallbackAction fallbackAction; if (initialDown) { fallbackAction = kcm.getFallbackAction(keyCode,MetaState); } else { fallbackAction = mFallbackActions.get(keyCode); } if (fallbackAction != null) { ... final int flags = event.getFlags() | KeyEvent.FLAG_FALLBACK; fallbackEvent = KeyEvent.obtain( event.getDownTime(),event.getEventTime(),event.getAction(),fallbackAction.keyCode,event.getRepeatCount(),fallbackAction.MetaState,event.getdeviceid(),event.getScanCode(),flags,event.getSource(),null); if (!interceptFallback(win,fallbackEvent,policyFlags)) { fallbackEvent.recycle(); fallbackEvent = null; } if (initialDown) { mFallbackActions.put(keyCode,fallbackAction); } else if (event.getAction() == KeyEvent.ACTION_UP) { mFallbackActions.remove(keyCode); fallbackAction.recycle(); } } } ... return fallbackEvent; }
这里我们关注一下方法体中调用的:interceptFallback方法,通过调用该方法将处理按键的 *** 作下发到该方法中,我们继续看一下该方法的实现逻辑。
private boolean interceptFallback(windowstate win,KeyEvent fallbackEvent,int policyFlags) { int actions = interceptKeyBefore@R_333_4403@(fallbackEvent,policyFlags); if ((actions & ACTION_PASS_TO_USER) != 0) { long delayMillis = interceptKeyBeforedispatching( win,policyFlags); if (delayMillis == 0) { return true; } } return false; }
然后我们看到在interceptFallback方法中我们调用了interceptKeyBefore@R_333_4403@方法,通过阅读我们我们知道该方法主要实现了对截屏按键的处理流程,这样我们继续看一下interceptKeyBeforeWueueing方法的处理:
@OverrIDe public int interceptKeyBefore@R_333_4403@(KeyEvent event,int policyFlags) { if (!mSystemBooted) { // If we have not yet booted,don't let key events do anything. return 0; } ... // Handle special keys. switch (keyCode) { case KeyEvent.KEYCODE_VolUME_DOWN: case KeyEvent.KEYCODE_VolUME_UP: case KeyEvent.KEYCODE_VolUME_MUTE: { if (mUseTvRouting) { // On TVs volume keys never go to the foreground app result &= ~ACTION_PASS_TO_USER; } if (keyCode == KeyEvent.KEYCODE_VolUME_DOWN) { if (down) { if (interactive && !mScreenshotChordVolumeDownKeyTriggered && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) { mScreenshotChordVolumeDownKeyTriggered = true; mScreenshotChordVolumeDownKeyTime = event.getDownTime(); mScreenshotChordVolumeDownKeyConsumed = false; cancelPendingPowerKeyAction(); interceptScreenshotChord(); } } else { mScreenshotChordVolumeDownKeyTriggered = false; cancelPendingScreenshotChordAction(); } } ... return result; }
可以发现这里首先判断当前系统是否已经boot完毕,若尚未启动完毕,则所有的按键 *** 作都将失效,若启动完成,则执行后续的 *** 作,这里我们只是关注音量减少按键和电源按键组合的处理事件。另外这里多说一句想安卓系统的HOME按键事件,MENU按键事件,进程列表按键事件等等都是在这里实现的,后续中我们会陆续介绍这方面的内容。
回到我们的interceptKeyBefore@R_333_4403@方法,当我用按下音量减少按键的时候回进入到:case KeyEvent.KEYCODE_VolUME_MUTE分支并执行相应的逻辑,然后同时判断用户是否按下了电源键,若同时按下了电源键,则执行:
if (interactive && !mScreenshotChordVolumeDownKeyTriggered && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) { mScreenshotChordVolumeDownKeyTriggered = true; mScreenshotChordVolumeDownKeyTime = event.getDownTime(); mScreenshotChordVolumeDownKeyConsumed = false; cancelPendingPowerKeyAction(); interceptScreenshotChord(); }
可以发现这里的interceptScreenshotChrod方法就是系统准备开始执行截屏 *** 作的开始,我们继续看一下interceptcreenshotChord方法的实现。
private voID interceptScreenshotChord() { if (mScreenshotChordEnabled && mScreenshotChordVolumeDownKeyTriggered && mScreenshotChordPowerKeyTriggered && !mScreenshotChordVolumeUpKeyTriggered) { final long Now = SystemClock.uptimeMillis(); if (Now <= mScreenshotChordVolumeDownKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILliS && Now <= mScreenshotChordPowerKeyTime + SCREENSHOT_CHORD_DEBOUNCE_DELAY_MILliS) { mScreenshotChordVolumeDownKeyConsumed = true; cancelPendingPowerKeyAction(); mHandler.postDelayed(mScreenshotRunnable,getScreenshotChordLongPressDelay()); } } }
在方法体中我们最终会执行发送一个延迟的异步消息,请求执行截屏的 *** 作而这里的延时时间,若当前输入框是打开状态,则延时时间为输入框关闭时间加上系统配置的按键超时时间,若当前输入框没有打开则直接是系统配置的按键超时处理时间,可看一下getScreenshotChordLongPressDelay方法的具体实现。
private long getScreenshotChordLongPressDelay() { if (mKeyguardDelegate.isShowing()) { // Double the time it takes to take a screenshot from the keyguard return (long) (KEyguard_SCREENSHOT_CHORD_DELAY_MulTIPLIER * VIEwConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout()); } return VIEwConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout(); }
回到我们的interceptScreenshotChord方法,发送了异步消息之后系统最终会被我们发送的Runnable对象的run方法执行;这样我们看一下Runnable类型的mScreenshotRunnable的run方法的实现:
private final Runnable mScreenshotRunnable = new Runnable() { @OverrIDe public voID run() { takeScreenshot(); } };
好吧,方法体中并未执行其他 *** 作,直接就是调用了takeScreenshot方法,这样我们继续看一下takeScreenshot方法的实现。
private voID takeScreenshot() { synchronized (mScreenshotLock) { if (mScreenshotConnection != null) { return; } Componentname cn = new Componentname("com.androID.systemUI","com.androID.systemUI.screenshot.TakeScreenshotService"); Intent intent = new Intent(); intent.setComponent(cn); ServiceConnection conn = new ServiceConnection() { @OverrIDe public voID onServiceConnected(Componentname name,IBinder service) { synchronized (mScreenshotLock) { if (mScreenshotConnection != this) { return; } Messenger messenger = new Messenger(service); Message msg = Message.obtain(null,1); final ServiceConnection myConn = this; Handler h = new Handler(mHandler.getLooper()) { @OverrIDe public voID handleMessage(Message msg) { synchronized (mScreenshotLock) { if (mScreenshotConnection == myConn) { mContext.unbindService(mScreenshotConnection); mScreenshotConnection = null; mHandler.removeCallbacks(mScreenshottimeout); } } } }; msg.replyTo = new Messenger(h); msg.arg1 = msg.arg2 = 0; if (mStatusbar != null && mStatusbar.isVisibleLw()) msg.arg1 = 1; if (mNavigationbar != null && mNavigationbar.isVisibleLw()) msg.arg2 = 1; try { messenger.send(msg); } catch (remoteexception e) { } } } @OverrIDe public voID onServicedisconnected(Componentname name) {} }; if (mContext.bindServiceAsUser( intent,conn,Context.BIND_auto_CREATE,UserHandle.CURRENT)) { mScreenshotConnection = conn; mHandler.postDelayed(mScreenshottimeout,10000); } } }
可以发现这里通过反射机制创建了一个TakeScreenshotService对象然后调用了bindServiceAsUser,这样就创建了TakeScreenshotService服务并在服务创建之后发送了一个异步消息。好了,我们看一下TakeScreenshotService的实现逻辑。
public class TakeScreenshotService extends Service { private static final String TAG = "TakeScreenshotService"; private static GlobalScreenshot mScreenshot; private Handler mHandler = new Handler() { @OverrIDe public voID handleMessage(Message msg) { switch (msg.what) { case 1: final Messenger callback = msg.replyTo; if (mScreenshot == null) { mScreenshot = new GlobalScreenshot(TakeScreenshotService.this); } mScreenshot.takeScreenshot(new Runnable() { @OverrIDe public voID run() { Message reply = Message.obtain(null,1); try { callback.send(reply); } catch (remoteexception e) { } } },msg.arg1 > 0,msg.arg2 > 0); } } }; @OverrIDe public IBinder onBind(Intent intent) { return new Messenger(mHandler).getBinder(); }}
可以发现在在TakeScreenshotService类的定义中有一个Handler成员变量,而我们在启动TakeScreentshowService的时候回发送一个异步消息,这样就会执行mHandler的handleMessage方法,然后在handleMessage方法中我们创建了一个GlobalScreenshow对象,然后执行了takeScreenshot方法,好吧,继续看一下takeScreentshot方法的执行逻辑。
/** * Takes a screenshot of the current display and shows an animation. */ voID takeScreenshot(Runnable finisher,boolean statusbarVisible,boolean navbarVisible) { // 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 = SurfaceControl.screenshot((int) dims[0],(int) dims[1]); if (mScreenBitmap == null) { notifyScreenshotError(mContext,mnotificationmanager); finisher.run(); 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,null); c.setBitmap(null); // Recycle the prevIoUs bitmap mScreenBitmap.recycle(); mScreenBitmap = ss; } // Optimizations mScreenBitmap.setHasAlpha(false); mScreenBitmap.preparetoDraw(); // Start the post-screenshot animation startAnimation(finisher,mdisplayMetrics.wIDthPixels,statusbarVisible,navbarVisible); }
可以看到这里后两个参数:statusbarVisible,navbarVisible是否可见,而这两个参数在我们
PhoneWindowManager.takeScreenshot方法传递的:if (mStatusbar != null && mStatusbar.isVisibleLw()) msg.arg1 = 1; if (mNavigationbar != null && mNavigationbar.isVisibleLw()) msg.arg2 = 1;
可见若果mStatusbar可见,则传递的statusbarVisible为true,若mNavigationbar可见,则传递的navbarVisible为true。然后我们在截屏的时候判断nStatusbar是否可见,mNavigationbar是否可见,若可见的时候则截屏同样将其截屏出来。继续回到我们的takeScreenshot方法,然后调用了:
// Take the screenshotmScreenBitmap = SurfaceControl.screenshot((int) dims[0],(int) dims[1]);
方法,看注释,这里就是执行截屏事件的具体 *** 作了,然后我看一下SurfaceControl.screenshot方法的具体实现,另外这里需要注意的是,截屏之后返回的是一个Bitmap对象,其实熟悉androID绘制机制的童鞋应该知道androID中所有显示能够显示的东西,在内存中表现都是Bitmap对象。
public static Bitmap screenshot(int wIDth,int height) { // Todo: should take the display as a parameter IBinder displayToken = SurfaceControl.getBuiltIndisplay( SurfaceControl.BUILT_IN_disPLAY_ID_MAIN); return nativeScreenshot(displayToken,new Rect(),wIDth,height,true,false,Surface.ROTATION_0); }
好吧,这里调用的是nativeScreenshot方法,它是一个native方法,具体的实现在JNI层,这里就不做过多的介绍了。继续回到我们的takeScreenshot方法,在调用了截屏方法screentshot之后,判断是否截屏成功:
if (mScreenBitmap == null) { notifyScreenshotError(mContext,mnotificationmanager); finisher.run(); return; }
若截屏之后,截屏的bitmap对象为空,这里判断截屏失败,调用了notifyScreenshotError方法,发送截屏失败的notification通知。
static voID notifyScreenshotError(Context context,notificationmanager nManager) { Resources r = context.getResources(); // Clear all existing notification,compose the new notification and show it Notification.Builder b = new Notification.Builder(context) .setTicker(r.getString(R.string.screenshot_Failed_Title)) .setContentTitle(r.getString(R.string.screenshot_Failed_Title)) .setContentText(r.getString(R.string.screenshot_Failed_text)) .setSmallicon(R.drawable.stat_notify_image_error) .setWhen(System.currentTimeMillis()) .setVisibility(Notification.VISIBIliTY_PUBliC) // ok to show outsIDe lockscreen .setcategory(Notification.category_ERROR) .setautoCancel(true) .setcolor(context.getcolor( com.androID.internal.R.color.system_notification_accent_color)); Notification n = new Notification.BigTextStyle(b) .bigText(r.getString(R.string.screenshot_Failed_text)) .build(); nManager.notify(R.ID.notification_screenshot,n); }
然后继续看takeScreenshot方法,判断截屏的图像是否需要旋转,若需要的话,则旋转图像:
if (requiresRotation) { // Rotate the screenshot to the current orIEntation Bitmap ss = Bitmap.createBitmap(mdisplayMetrics.wIDthPixels,null); c.setBitmap(null); // Recycle the prevIoUs bitmap mScreenBitmap.recycle(); mScreenBitmap = ss; }
在takeScreenshot方法的最后若截屏成功,我们调用了:
// Start the post-screenshot animation startAnimation(finisher,navbarVisible);
开始截屏的动画,好吧,看一下动画效果的实现:
/** * Starts the animation after taking the screenshot */ private voID startAnimation(final Runnable finisher,int w,int h,boolean navbarVisible) { // Add the vIEw for the animation mScreenshotVIEw.setimageBitmap(mScreenBitmap); mScreenshotLayout.requestFocus(); // Setup the animation with the screenshot just taken if (mScreenshotAnimation != null) { mScreenshotAnimation.end(); mScreenshotAnimation.removeAllListeners(); } mWindowManager.addVIEw(mScreenshotLayout,mWindowLayoutParams); ValueAnimator screenshotDropInAnim = createScreenshotDropInAnimation(); ValueAnimator screenshotFadeOutAnim = createScreenshotDropOutAnimation(w,h,navbarVisible); mScreenshotAnimation = new AnimatorSet(); mScreenshotAnimation.playSequentially(screenshotDropInAnim,screenshotFadeOutAnim); mScreenshotAnimation.addListener(new AnimatorListenerAdapter() { @OverrIDe public voID onAnimationEnd(Animator animation) { // Save the screenshot once we have a bit of time Now saveScreenshotInWorkerThread(finisher); mWindowManager.removeVIEw(mScreenshotLayout); // Clear any references to the bitmap mScreenBitmap = null; mScreenshotVIEw.setimageBitmap(null); } }); mScreenshotLayout.post(new Runnable() { @OverrIDe public voID run() { // Play the shutter sound to notify that we've taken a screenshot mCameraSound.play(MediaActionSound.SHUTTER_CliCK); mScreenshotVIEw.setLayerType(VIEw.LAYER_TYPE_HARDWARE,null); mScreenshotVIEw.buildLayer(); mScreenshotAnimation.start(); } }); }
好吧,经过着一些列的 *** 作之后我们实现了截屏之后的动画效果了,这里暂时不分析动画效果,我们看一下动画效果之后做了哪些?还记不记的一般情况下我们截屏之后都会收到一个截屏的notification通知?这里应该也是在其AnimatorListenerAdapter的onAnimationEnd方法中实现的,也就是动画执行完成之后,我们看一下其saveScreenshotInWorkerThread方法的实现:
/** * Creates a new worker thread and saves the screenshot to the media store. */ private voID saveScreenshotInWorkerThread(Runnable finisher) { SaveImageInBackgroundData data = new SaveImageInBackgroundData(); data.context = mContext; data.image = mScreenBitmap; data.iconSize = mNotificationIconSize; data.finisher = finisher; data.prevIEwWIDth = mPrevIEwWIDth; data.prevIEwheight = mPrevIEwHeight; if (mSaveInBgTask != null) { mSaveInBgTask.cancel(false); } mSaveInBgTask = new SaveImageInBackgroundTask(mContext,data,mnotificationmanager,R.ID.notification_screenshot).execute(data); }
好吧,这里主要逻辑就是构造了一个SaveImageInBackgroundTask对象,看样子发送截屏成功的通知应该是在这里实现的,我们看一下SaveImageInBackgroundTask构造方法的实现逻辑:
SaveImageInBackgroundTask(Context context,SaveImageInBackgroundData data,notificationmanager nManager,int nID) { ... // Show the intermediate notification mTickerAddspace = !mTickerAddspace; mNotificationID = nID; mnotificationmanager = nManager; final long Now = System.currentTimeMillis(); mNotificationBuilder = new Notification.Builder(context) .setTicker(r.getString(R.string.screenshot_saving_ticker) + (mTickerAddspace ? " " : "")) .setContentTitle(r.getString(R.string.screenshot_saving_Title)) .setContentText(r.getString(R.string.screenshot_saving_text)) .setSmallicon(R.drawable.stat_notify_image) .setWhen(Now) .setcolor(r.getcolor(com.androID.internal.R.color.system_notification_accent_color)); mNotificationStyle = new Notification.BigPictureStyle() .bigPicture(picture.createAshmemBitmap()); mNotificationBuilder.setStyle(mNotificationStyle); // For "public" situations we want to show all the same info but // omit the actual screenshot image. mpublicNotificationBuilder = new Notification.Builder(context) .setContentTitle(r.getString(R.string.screenshot_saving_Title)) .setContentText(r.getString(R.string.screenshot_saving_text)) .setSmallicon(R.drawable.stat_notify_image) .setcategory(Notification.category_PROGRESS) .setWhen(Now) .setcolor(r.getcolor( com.androID.internal.R.color.system_notification_accent_color)); mNotificationBuilder.setPublicVersion(mpublicNotificationBuilder.build()); Notification n = mNotificationBuilder.build(); n.flags |= Notification.FLAG_NO_CLEAR; mnotificationmanager.notify(nID,n); // On the tablet,the large icon makes the notification appear as if it is clickable (and // on small devices,the large icon is not shown) so defer showing the large icon until // we compose the final post-save notification below. mNotificationBuilder.setLargeIcon(icon.createAshmemBitmap()); // But we still don't set it for the expanded vIEw,allowing the smallicon to show here. mNotificationStyle.bigLargeIcon((Bitmap) null); }
可以发现在构造方法的后面狗仔了一个NotificationBuilder对象,然后发送了一个截屏成功的Notification,这样我们在截屏动画之后就收到了Notification的通知了。
总结:
一般默认情况下按下音量减少键和开机键会执行截图动作,程序执行的入口就在在PhoneWindowManager的dispatchUnhandledKey方法中;然后通过TakeScreenshotService服务执行截图逻辑;通过nativIE方法获取截图的bitmap,如果失败调用失败通知栏消息,如果成功调用截图动画后发送成功通知栏消息。
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android 截图功能源码的分析全部内容,希望文章能够帮你解决Android 截图功能源码的分析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)