{"id":1174,"date":"2017-12-13T00:14:39","date_gmt":"2017-12-12T16:14:39","guid":{"rendered":"http:\/\/blog.xiunian.wang\/?p=1174"},"modified":"2023-06-26T00:26:20","modified_gmt":"2023-06-25T16:26:20","slug":"activity%e5%90%af%e5%8a%a8%e4%bb%a5%e5%8f%8aview%e7%9a%84%e7%bb%98%e5%88%b6","status":"publish","type":"post","link":"https:\/\/blog.xiunian.wang\/?p=1174","title":{"rendered":"Activity\u542f\u52a8\u4ee5\u53caView\u7684\u7ed8\u5236"},"content":{"rendered":"<p>\u5148\u6574\u7406\u4e00\u4e0bActivity A start ActivityB \u7684\u4e00\u4e2abrief \u6d41\u7a0b \u5982\u4e0b<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1184\" src=\"\/wp-content\/uploads\/2017\/12\/activity_lunch.png\" alt=\"\" width=\"791\" height=\"873\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/activity_lunch.png 791w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/activity_lunch-272x300.png 272w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/activity_lunch-768x848.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>\u5bf9\u5e94\u7684 Wms Ams ActivityThread \u7684\u6d41\u7a0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1210\" src=\"\/wp-content\/uploads\/2017\/12\/WX20171212-225139@2x-1024x786.png\" alt=\"\" width=\"525\" height=\"403\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-225139@2x-1024x786.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-225139@2x-300x230.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-225139@2x-768x589.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-225139@2x.png 1590w\" sizes=\"(max-width: 525px) 100vw, 525px\" \/><\/p>\n<h4>setContentView \u5f00\u59cb\uff0c\u6211\u4eec\u770b\u53d1\u751f\u4e86\u4ec0\u4e48<\/h4>\n<p><!--more--><\/p>\n<ol>\n<li>activity \u7684setContentView \u5b9e\u9645\u4e0a\u8c03\u7528\u7684\u662fActivity \u4f9d\u8d56\u7684 mWindow\u5bf9\u8c61\u7684setContentView\u65b9\u6cd5 Window\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u5728\u76ee\u524d\u6765\u8bf4\u4ec5\u6709\u4e00\u4e2a\u5b9e\u73b0\u7c7b&#8211; PhoneWindow\u3002<\/li>\n<li>PhoneWindow \u5e72\u4e86\u5565\u3002<\/li>\n<\/ol>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">  @Override\n    public void setContentView(int layoutResID) {\n        \/\/ Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window\n        \/\/ decor, when theme attributes and the like are crystallized. Do not check the feature\n        \/\/ before this happens.\n        if (mContentParent == null) {\n            installDecor();\n        } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {\n            mContentParent.removeAllViews();\n        }\n\n        if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {\n            final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,\n                    getContext());\n            transitionTo(newScene);\n        } else {\n            mLayoutInflater.inflate(layoutResID, mContentParent);\n        }\n        mContentParent.requestApplyInsets();\n        final Callback cb = getCallback();\n        if (cb != null &amp;amp;amp;&amp;amp;amp; !isDestroyed()) {\n            cb.onContentChanged();\n        }\n        mContentParentExplicitlySet = true;\n    }<\/pre>\n<p>\u5173\u952e\u770b mLayoutInflater.inflate(layoutResID, mContentParent);<br \/>\n\u8fd9\u4e0d\u548c\u6211\u4eec\u666e\u901a\u52a8\u6001\u6dfb\u52a0\u4e00\u4e2aview\u5230\u4e00\u4e2aviewGroup \u4e00\u6837\u4e48\uff1f\u987a\u5e26\u770b\u4e00\u4e0blayoutinflater \u662f\u600e\u4e48\u628a\u4e00\u4e2axml \u751f\u6210\u4e00\u4e2a\u4e2a\u7684view\u5bf9\u8c61\u7684\u3002<\/p>\n<ol>\n<li>\u901a\u8fc7Resources\u5bf9\u8c61\u62ff\u5230\u5e03\u5c40xml\u6587\u4ef6\uff0c\u540c\u65f6ResourceImpl\u53bb\u89e3\u6790\u51faxmlResourceParser \u5b9e\u4f8bXmlResourceParser parser = res.getLayout(resource) \u8fd9\u91cc\u9762ResourceImpl\u89e3\u6790\u5404\u4e2axml\u5757 \u89e3\u6790\u7684\u65f6\u5019\u91cc\u9762\u8fd8\u6709\u4e00\u4e2a\u7f13\u5b58\u673a\u5236\uff0c \u5982\u679c\u5df2\u7ecf\u89e3\u6790\u8fc7\u4e86\uff0c\u5219\u7b2c\u4e8c\u6b21\u518d\u6b21inflat\u8fd9\u4e2a\u5e03\u5c40\u5c31\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u53bb\u800c\u4e0d\u7528\u8bfb\u53d6\u6587\u4ef6\u4e86\uff0c\u8981\u77e5\u9053\u8fd9\u53ef\u662f\u5728\u4e3b\u7ebf\u7a0b\u554a\u3002\u3002\u3002<\/li>\n<li>\u4f20\u9012 parser \u7ed9LayoutInflater \u5bf9\u8c61 View<br \/>\n\u8fd9\u91cc\u5c31\u662f\u5faa\u73af\u8bfb\u53d6parser \u5e76new ViewGroup \u6216\u8005View \u6dfb\u52a0\u4e86\u5230\u76f8\u5e94\u7684ViewGroup\u4e4b\u4e0b\uff0c\u4ee3\u7801\u6bd4\u8f83\u957f\uff0c\u4e0d\u8d34\u4e86\uff0c\u7528\u4e86\u4e00\u5806\u88c5\u9970\u6a21\u5f0f\u3002<\/li>\n<li>View \u5bf9\u5e94\u7684\u7c7b\u6709\u4e86\u3002\u600e\u4e48\u53bb\u6e32\u67d3\u5230\u5c4f\u5e55\u4e0a\uff1f<br \/>\napp\u5f00\u53d1\u77e5\u9053\u3002setContentView \u540e\u6211\u4eec\u7684\u5e03\u5c40\u6587\u4ef6\u4f1a\u6e32\u67d3\u5728\u6807\u9898\u680f\u4e0b\uff0c\u90a3\u6807\u9898\u680f\u662f\u5565\uff1f\u6211\u4eec\u6ca1\u6709\u5199\u76f8\u5173\u4ee3\u7801\uff0c\u5b83\u53c8\u662f\u5982\u4f55\u521b\u5efa\u7684\u5462\uff1f\u6211\u5148\u7528uiautomatorviewer(\u5728sdk tools\/bin\u76ee\u5f55\u4e0b)\u67e5\u770b\u4e00\u4e0b\u5e03\u5c40\u7ed3\u6784\u5982\u4e0b\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1195 size-large\" src=\"\/wp-content\/uploads\/2017\/12\/1513003577281-1024x591.jpg\" alt=\"\" width=\"525\" height=\"303\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/1513003577281-1024x591.jpg 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/1513003577281-300x173.jpg 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/1513003577281-768x443.jpg 768w\" sizes=\"(max-width: 525px) 100vw, 525px\" \/><br \/>\n\u8fd9\u4e2aid\u4e3adecor_content_parent \u7684ViewGroup \u5bf9\u5e94\u7684\u5c31\u662f\u6211\u4eec\u7684\u6574\u4e2aapp\u7684\u5e03\u5c40\uff0c\u5305\u62ec\u6807\u9898\u680f\uff08\u4e0d\u5305\u62ec\u9876\u90e8\u72b6\u6001\u680f\uff09\uff0c\u5176\u5b9e\u6700\u5916\u5c42\u6839\u8282\u70b9FramLayout\u5728activity \u7684window\u4e2d\u5bf9\u5e94\u7684\u5c31\u662f\u4e00\u4e2amDecodeview \u5b83\u7ee7\u627f\u81eaFrameLayout,\u540c\u65f6 PhoneWindow\u91cc\u9762\u8fd8\u6709\u4e00\u4e2aDecorContentParent mDecorContentParent \uff0c\u5b83\u5c31\u662f\u8fd9\u4e2aid\u4e3a decor_content_parent \u7684ViewGroup,(1)View \u5bf9\u5e94\u7684\u5c31\u662fstatusBar \uff08\u6211\u4eec\u7684\u72b6\u6001\u680f,\u56e0\u6b64\u7406\u8bba\u4e0a\u53ef\u4ee5\u53cd\u5c04\u5230\u8fd9\u4e2aView \u7136\u540e\u7ed9\u5176\u8bbe\u7f6e\u52a8\u6001\u80cc\u666f\u989c\u8272\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5229\u7528api\u5566\uff09<\/li>\n<li>Window\u8bbe\u7f6e\u5b8c\u6210\u4e4b\u540e\u3002<br \/>\nWindow\u8bbe\u7f6e\u5b8c\u6210\u4e4b\u540e\uff0c\u5c31\u8981\u628a\u521b\u5efa\u7684\u8fd9\u4e2a\u7a97\u53e3\u544a\u8bc9WmS\u3002 Activity\u51c6\u5907\u597d\u540e\u901a\u77e5Ams\uff0cAms \u5904\u7406\u5b8c\u6761\u4ef6\u5224\u65ad\uff0c\u6700\u7ec8\u8c03\u7528Activity\u7684makeVisible()<\/li>\n<\/ol>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">    void makeVisible() {\n        if (!mWindowAdded) {\n            ViewManager wm = getWindowManager();\n            wm.addView(mDecor, getWindow().getAttributes());\n            mWindowAdded = true;\n        }\n        mDecor.setVisibility(View.VISIBLE);\n    }<\/pre>\n<p>\u8fd9\u91cc\u7684ViewManager \u662fLocalWindowManager\uff0c\u5982\u4e0b\u8bbe\u7f6e\u7684\u65f6\u5019mWindowManager \u5c31\u662fcreateLocalWindowManager \u867d\u7136\u548cWindowmanagerImpl \u90fd\u662f\u5b9e\u73b0WindowManager\uff0c\u4f46\u662f\u524d\u8005\u505a\u4e86\u4e00\u4e9b\u6743\u9650\u62e6\u622a\u5904\u7406\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\/\/window.java\n\/**\n     * Set the window manager for use by this Window to, for example,\n     * display panels.  This is &amp;amp;amp;lt;em&amp;amp;amp;gt;not&amp;amp;amp;lt;\/em&amp;amp;amp;gt; used for displaying the\n     * Window itself -- that must be done by the client.\n     *\n     * @param wm The window manager for adding new windows.\n     *\/\n    public void setWindowManager(WindowManager wm, IBinder appToken, String appName,\n            boolean hardwareAccelerated) {\n        mAppToken = appToken;\n        mAppName = appName;\n        mHardwareAccelerated = hardwareAccelerated\n                || SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);\n        if (wm == null) {\n            wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);\n        }\n        mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);\n    }<\/pre>\n<h4>View\u7ed8\u5236<\/h4>\n<p>invalidate(), \u8bf7\u6c42View\u7684\u91cd\u7ed8\u5236\uff0c\u9996\u5148\u7ed8\u5236\u6839\u89c6\u56fe\uff0c\u7136\u540e\u518d\u662f\u5176\u4e0b\u9762\u7684\u5b50\u89c6\u56fe,\u4f1a\u6839\u636eView \u4e0b\u9762\u7684mPrivateFlags \u5c5e\u6027\u8fdb\u884c\u5224\u65ad\u662f\u5426\u9700\u8981\u7ed8\u5236\uff0c\u5982\u679c\u89c6\u56fe\u5927\u5c0f\u4e3a0 \u4e5f\u4e0d\u4f1a\u53bb\u91cd\u7ed8.<br \/>\nperformTraversals(),\u6839\u636e\u4e4b\u524d\u72b6\u6001\u5224\u65ad\u662f\u5426\u9700\u8981measure\uff0clayout \u548cdraw\u3002<\/p>\n<ol>\n<li>measure<br \/>\nmeasure\u672c\u8d28\u662f\u628a\u76f8\u5bf9\u503c\u8f6c\u5316\u4e3a\u5177\u4f53\u503c\u7684\u8fc7\u7a0b\uff0c\u4eceRootView\u7684host.measure\u5f00\u59cb\u7684<br \/>\n<img decoding=\"async\" class=\"aligncenter size-large wp-image-1203\" src=\"\/wp-content\/uploads\/2017\/12\/WX20171212-173017@2x-1024x789.png\" alt=\"\" width=\"100%\" height=\"100%\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-173017@2x-1024x789.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-173017@2x-300x231.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-173017@2x-768x591.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-173017@2x.png 1688w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><br \/>\nview\u5bf9\u5e94\u7684\u539f\u578b\u4e3a\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">public final void measure(int widthMeasureSpec, int heightMeasureSpec)<\/pre>\n<p>\u56e0\u6b64\uff0c\u662f\u4e0d\u53ef\u88ab\u5b50\u7c7b\u8986\u76d6\uff0c\u53ea\u80fd\u8986\u76d6onMeasure\uff0c\u5b50\u89c6\u56fe\u53ef\u4ee5setMeasureDimension() \u8bbe\u7f6e\u4efb\u610f\u5927\u5c0f\u7684\u5e03\u5c40\u3002match_parent \u5bf9\u5e94\u7684MeasureSpec.EXACTLY(\u7236\u7a97\u53e3\u4f1a\u9650\u5236\u5b50\u7c7b\u7684\u5927\u5c0f) wrap_contet \u5bf9\u5e94MeasureSpec.AT_MOST<\/li>\n<li>layout<br \/>\n<img decoding=\"async\" class=\"aligncenter size-large wp-image-1208\" src=\"\/wp-content\/uploads\/2017\/12\/WX20171212-212757@2x-1024x404.png\" alt=\"\" width=\"100%\" height=\"100%\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-212757@2x-1024x404.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-212757@2x-300x118.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-212757@2x-768x303.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-212757@2x.png 1750w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><br \/>\nsetLayout \u4e2d setFrame \u7136\u540e\u56de\u8c03onLayout \u51fd\u6570\u5982\u679c\u7ee7\u627f\u81eaviewGroup \u5219\u5fc5\u987b\u5b9e\u73b0onLayout\u65b9\u6cd5\u3002\u56e0\u4e3aViewGroup\u4e2d\u7684OnLayout\u662f\u62bd\u8c61\u65b9\u6cd5\uff0c<br \/>\nsetFram\u4e2d\u5982\u679c\u4f4d\u7f6e\u6709\u53d8\u5316\uff0c\u5219\u4f1a\u8c03\u7528invalidate \u5e76\u628aDRAWN \u5199\u5165mPrivateFlags\u3002 onLayout\u4e3b\u8981\u662f\u7528\u4e8eViewGroup \u5bf9\u5b50\u89c6\u56fe\u8fdb\u884c\u5206\u914d\u4f4d\u7f6e\u3002<br \/>\nLinearLayout \u6709\u4e24\u79cd\u5e03\u5c40VerticalLayout\u548cHorizentalLayout<\/li>\n<li>draw<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1209\" src=\"\/wp-content\/uploads\/2017\/12\/WX20171212-214809@2x-1024x575.png\" alt=\"\" width=\"525\" height=\"295\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-214809@2x-1024x575.png 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-214809@2x-300x168.png 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-214809@2x-768x431.png 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171212-214809@2x.png 2038w\" sizes=\"(max-width: 525px) 100vw, 525px\" \/><br \/>\n\u8fd8\u662f\u4eceViewRoot \u7684performTraversals\u51fd\u6570\u5f00\u59cb\uff0c\u8c03\u7528draw(),\u518d\u8c03\u7528mView.draw(),mView\u5c31\u662f\u7a97\u53e3\u7684\u6839\u89c6\u56fe\uff0cActivity \u5c31\u662fDecordView<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\/*\n     * Draw traversal performs several drawing steps which must be executed\n     * in the appropriate order:\n     *\n     *      1. Draw the background\n     *      2. If necessary, save the canvas' layers to prepare for fading\n     *      3. Draw view's content\n     *      4. Draw children\n     *      5. If necessary, draw the fading edges and restore layers\n     *      6. Draw decorations (scrollbars for instance)\n     *\/<\/pre>\n<p>\u4e0a\u9762\u662fView.draw()\uff0c\u6ce8\u91ca\u3002\u89e3\u91ca\u4e86draw\u5f88\u660e\u767d\u4e86 \u7b2c\u56db\u6b65\u4f1a\u8c03\u7528dispatchDraw \u65b9\u6cd5\uff0cviewGroup\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u65b9\u6cd5\u3002\u7ed8\u5236\u5b8c\u4e00\u4e2a\u6b65\u9aa4\u4f1a\u56de\u8c03onDraw(canvas) \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u5728\u5b50\u7c7b\u4e2d\u53ef\u4ee5\u91cd\u8f7d\u3002<\/li>\n<\/ol>\n<p>canvas\u4ece\u54ea\u91cc\u6765\u7684\uff1fViewRoot \u4ecesurface\u4e2d\u83b7\u53d6\u3002\uff08\u663e\u5361\uff0c\u56fe\u5f62\u52a0\u901f\u652f\u6301\u7684surface \u6216\u8005cpu\u5185\u5b58\u6a21\u62df\u7684Surface)<br \/>\n\u91cd\u7ed8View\u7684\u65f6\u673a\uff1ainvalidate(\u6539\u53d8selected \u72b6\u6001\uff0csetVisibility),requestLayout(setVisible),requestFocus(\u4e0b\u4e00\u6b65\uff0c\u641c\u7d22\u7b49)<\/p>\n<h4>\u70b9\u51fb\u4e8b\u4ef6\u5206\u53d1<\/h4>\n<p>WmS \u5224\u65ad\u5f53\u524d\u7528\u6237\u5728\u548c\u54ea\u4e2a\u7a97\u53e3\u4ea4\u4e92\uff0c\u5219\u5c06\u4e8b\u4ef6\u6d3e\u53d1\u5230\u76f8\u5e94\u7684\u7a97\u53e3,\u6309\u952e\u6d88\u606fkeyEvent\u7684\u4f20\u9012\u7684\u8def\u5f84\u662f\uff1a<br \/>\nActivity -&gt;mWindow -&gt;DecordView -&gt;FrameLayout-&gt;ViewGroup-&gt;mFocused.dispatchEventKey()<br \/>\n\u8fd9\u6837\u5c31\u5f00\u59cb\u904d\u5386\u5176\u4e0b\u9762\u7684\u5b50\u89c6\u56fe\u3002<br \/>\n\u5bf9\u4e8e\u89e6\u6478\u6d88\u606f motionEvent\uff0c\u5219\u548c\u4e0a\u8ff0\u76f8\u53cd\uff0c\u5148\u7531\u5b50\u89c6\u56fe\u5904\u7406\uff0c\u6ca1\u6709\u80ce\u513f\u671f\uff08Wms\u4e0d\u4f1a\u9884\u5148\u5904\u7406\uff09.<\/p>\n<p>TouchEvent:<\/p>\n<ol>\n<li>ViewGroup\u53ef\u4ee5\u62e6\u622a\u4e8b\u4ef6\u4f20\u9012\u5230\u5b50View,\u62e6\u622a MotionEvent.ACTION_DOWN \u5373\u53ef,\u8fd9\u6837\uff0c\u53ea\u6709\u8fd9\u4e2aViewGroup\u624d\u80fd\u54cd\u5e94onTouchEvent\u4e8b\u4ef6\uff0c\u5b50view\u88ab\u62e6\u622a\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">@Override\npublic boolean onInterceptTouchEvent(MotionEvent ev) {\n    Log.d(TAG, \"onInterceptTouchEvent: \" + ev);\n    switch (ev.getAction()) {\n        case MotionEvent.ACTION_DOWN:\n            return true;\n    }\n    return super.onInterceptTouchEvent(ev);\n}<\/pre>\n<\/li>\n<li>\u5b50View \u53ef\u4ee5\u4e0d\u8ba9ViewGroup\u54cd\u5e94\u3002\u4e5f\u662f\u901a\u8fc7\u6d88\u5316 MotionEvent.ACTION_DOWN\u3002\u8fd4\u56detrue \u7236group\u5219\u4e0d\u518d\u56de\u8c03onTouchEvent\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">@Override\npublic boolean onTouchEvent(MotionEvent event) {\n    Log.d(TAG, \"-----onTouchEvent: \"+event);\n    switch (event.getAction()) {\n        case MotionEvent.ACTION_DOWN:\n        case MotionEvent.ACTION_MOVE:\n            return true;\n    }\n    return super.onTouchEvent(event);\n}<\/pre>\n<p>\u4e8b\u4ef6\u4f20\u9012\u6700\u540e\u624d\u5230\u8fbeActivity\u7684onTouchEvent \u4e2d\u3002<\/li>\n<li>dispatchTouchEvent<br \/>\n\u5bf9\u4e8eView\uff0c\u8fd9\u4e2a\u65b9\u6cd5 \u8c03\u7528super.dispatchEvent\u65b9\u6cd5\u624d\u80fd\u89e6\u53d1onTouchEvent \u56de\u8c03(\u5982\u679c\u8fd9\u4e2a\u662fviewgroup\u7684\u8bdd\u56de\u8c03\u7684\u662fonInterceptTouchEvent)\u3002retrun true \u4e8b\u4ef6\u5c06\u4e0d\u4f1a\u5411\u4e0b\u5c42\uff08\u7269\u7406\u89c6\u89c9\u4e0a\u7684\uff09\u4f20\u9012,\u4f46\u4e5f\u4e0d\u8c03\u7528onTouchEvent\u3002 false \u5219\u4f1a\u5411\u8fd9\u4e2aView\u7684\u5e95\u5c42\uff08\u89c6\u89c9\u4e0a\uff09\u4f20\u9012,\u5c31\u662f\u90a3\u79cd\u70b9\u51fb\u7a7f\u900f\u3002<br \/>\n\u5bf9\u4e8eViewGroup \u5982\u679cdisptatchTouchEvent\u8fd4\u56desuper.dispatchTouchEvent \u4f1a\u8c03\u7528onInterceptTouchEvent \uff0c\u5982\u679c\u8fd4\u56defalse \u5219\u76f4\u63a5\u8c03\u7528\u7236\u5bb9\u5668\uff08ViewGroup\uff09\u7684onTouchEvent \u65b9\u6cd5\u3002\u4e0d\u518d\u5411\u4e0b\u4f20\u9012. \u5982\u679c\u8fd4\u56deTrue \u5219\u4e8b\u4ef6\u88ab\u6d88\u8d39\uff08\u6d88\u5931\uff09<\/li>\n<\/ol>\n<h4>\u6dfb\u52a0\u4e00\u4e2aView \u5230\u5176\u5b83app\u4e4b\u4e0a\u5982\u4f55\u505a\uff1f<\/h4>\n<p>\u7c7b\u4f3c\u4e8e\u4ee5\u524d\u90a3\u4e9b\u4e00\u952e\u6e05\u7406\u7b49\u5de5\u5177\u3002\u65e2\u7136\u6240\u6709\u7684View \u90fd\u662fWm \u7ba1\u7406\uff0c\u800c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2aaddView\u65b9\u6cd5\u3002\u5176\u4e2d\u53c2\u6570LayoutParams type \u5982\u679c\u662f2000 \u4ee5\u4e0a\u662f\u7cfb\u7edf\u7a97\u53e3\u7c7b\u578b\uff0c\u5982\u679c\u662f1000\u4ee5\u4e0a\u662f\u5b50\u7a97\u53e3\u7c7b\u578b\uff0c\u5982\u679c\u662f\u5c0f\u4e8e100 \u5219\u662f\u5e94\u7528\u7a0b\u9700\u7a97\u53e3\u7c7b\u578b\u3002<br \/>\n\u8bbe\u7f6e\u4e0d\u540c\u7684\u7c7b\u578b \u5bf9\u5e94\u9700\u8981\u7684\u6743\u9650\u4e0d\u4e00\u6837\uff0c\u6bd4\u5982\u6211\u4eec\u8bbe\u7f6e\u4e86TYPE_APPLICATION_OVERLAY \u5219\u9700\u8981\u663e\u793a\u5728\u5176\u5b83\u7a97\u53e3\u4e4b\u4e0a\u7684\u6743\u9650\uff1a&#8221;android.permission.SYSTEM_ALERT_WINDOW&#8221;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\n        final TextView view = new TextView(context);\n        view.setText(R.string.app_name);\n        view.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));\n        final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);\n\n        WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();\n\n        int LAYOUT_FLAG;\n        if (Build.VERSION.SDK_INT &amp;amp;amp;amp;amp;amp;gt;= Build.VERSION_CODES.O) {\n            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;\n        } else {\n            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;\n        }\n        wmParams.type=LAYOUT_FLAG;   \/\/\u8fd9\u91cc\u662f\u5173\u952e\uff0c\u4f60\u4e5f\u53ef\u4ee5\u8bd5\u8bd52003\n        wmParams.format=1;\n\n        wmParams.flags=40;\n        wmParams.width=400;\n        wmParams.height=400;\n        wmParams.verticalMargin=-0.40f;\/\/\u5728\u5c4f\u5e55\u4e0a\u7684\u767e\u5206\u6bd4\u9ed8\u8ba4\u662f\u5728\u4e2d\u95f4\u8fd9\u91cc\u5f80\u4e0a\u79fb\u52a8\u4e8640%\u4e5f\u5c31\u5728\u9876\u90e8\u4e86\u5b83\u4e0d\u5305\u62ec\u7535\u91cf\u680f\n        wm.addView(view,wmParams);\n        view.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                wm.removeView(view);\n            }\n        });\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1189\" src=\"\/wp-content\/uploads\/2017\/12\/WX20171211-201103-copy-1024x793.jpg\" alt=\"\" width=\"525\" height=\"407\" srcset=\"https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171211-201103-copy-1024x793.jpg 1024w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171211-201103-copy-300x232.jpg 300w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171211-201103-copy-768x595.jpg 768w, https:\/\/blog.xiunian.wang\/wp-content\/uploads\/2017\/12\/WX20171211-201103-copy.jpg 1390w\" sizes=\"(max-width: 525px) 100vw, 525px\" \/><\/p>\n<p>\u5982\u679c\u60f3\u8ba9\u5b83\u80fd\u591f\u968f\u7740\u624b\u6307\u62d6\u52a8\uff0c\u53ea\u9700\u76d1\u542cview\u7684OnTouchListener\uff0c\u5e76\u5904\u7406\u597dMotionEvent.ACTION_DOWN \u548cMotionEvent.ACTION_MOVE \u5728move\u7684\u540c\u65f6\u66f4\u65b0wm.updateViewLayout(view, wmParams);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u6574\u7406\u4e00\u4e0bActivity A start ActivityB \u7684\u4e00\u4e2abrief \u6d41\u7a0b \u5982\u4e0b \u5bf9\u5e94\u7684 Wms Ams ActivityThread \u7684\u6d41\u7a0b\uff1a setContentView \u5f00\u59cb\uff0c\u6211\u4eec\u770b\u53d1\u751f\u4e86\u4ec0\u4e48<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[13],"_links":{"self":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1174"}],"collection":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1174"}],"version-history":[{"count":42,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1174\/revisions"}],"predecessor-version":[{"id":1744,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1174\/revisions\/1744"}],"wp:attachment":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}