{"id":961,"date":"2023-03-11T19:39:30","date_gmt":"2023-03-11T11:39:30","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=961"},"modified":"2023-04-29T13:29:39","modified_gmt":"2023-04-29T05:29:39","slug":"android-layout-ui-optimization","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-layout-ui-optimization\/","title":{"rendered":"Android\u5e03\u5c40UI\u4f18\u5316"},"content":{"rendered":"<h2>\u5361\u987f\u7f6a\u9b41\u7978\u9996<\/h2>\n<p>UI\u5361\u987f\u5c31\u662fAndroid\u5728\u754c\u9762\u6e32\u67d3\u65f6\u4e22\u5e27\u3002 Android\u7cfb\u7edf\u8981\u6c42\u6bcf\u4e00\u5e27\u90fd\u8981\u5728 16ms \u5185\u7ed8\u5236\u5b8c\u6210\uff0c\u5728\u8fd9\u4e2a\u65f6\u95f4\u5185\u754c\u9762\u5c31\u662f\u6d41\u7545\u7684\uff08\u80fd\u591f\u8fbe\u5230\u6d41\u7545\u7684\u753b\u9762\u6240\u9700\u8981\u768460fps\uff09<\/p>\n<p>\u4e22\u5e27\u7684\u5404\u79cd\u539f\u56e0\uff1a<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>layout \u592a\u8fc7\u590d\u6742\uff0c\u5c42\u6b21\u8fc7\u591a\uff0cUI \u4e0a\u6709\u5c42\u53e0\u592a\u591a\u7684\u7ed8\u5236\u5355\u5143\uff0c\u8fc7\u5ea6\u7ed8\u5236<\/li>\n<li>CPU \u6216\u8005 GPU \u8d1f\u8f7d\u8fc7\u91cd<\/li>\n<li>\u52a8\u753b\u6267\u884c\u7684\u6b21\u6570\u8fc7\u591a<\/li>\n<li>\u9891\u7e41 GC\uff0c\u4e3b\u8981\u662f\u5185\u5b58\u6296\u52a8<\/li>\n<li>UI \u7ebf\u7a0b\u6267\u884c\u8017\u65f6\u64cd\u4f5c\uff08\u8981\u5728\u5b50\u7ebf\u7a0b\u6267\u884c\u8017\u65f6\u64cd\u4f5c\uff09<\/li>\n<\/ul>\n<h2>\u5206\u6790\u4e0e\u5efa\u8bae\u89e3\u51b3<\/h2>\n<h3>Layout\u592a\u8fc7\u590d\u6742\uff0c\u5c42\u6b21\u8fc7\u591a\uff0cUI \u4e0a\u6709\u5c42\u53e0\u592a\u591a\u7684\u7ed8\u5236\u5355\u5143\uff0c\u8fc7\u5ea6\u7ed8\u5236<\/h3>\n<p>\u5bf9\u4e8e\u6211\u4eecLayout\u6587\u4ef6\uff0c\u5c42\u7ea7\u8d8a\u591a\uff0cAndroid\u5728\u521d\u59cb\u5316\u4ed6\u4eec\u65f6\uff0c\u4e5f\u5c31\u662f\u6e32\u67d3\u65f6\u5c31\u8d8a\u6d6a\u8d39\u65f6\u95f4\u3002\u6240\u4ee5\u5e03\u5c40\u7684\u4f18\u5316\u5176\u5b9e\u8bf4\u767d\u4e86\u5c31\u662f\u51cf\u5c11\u5c42\u7ea7\uff0c\u8d8a\u7b80\u5355\u8d8a\u597d\uff0c\u51cf\u5c11overdraw\uff08 \u8fc7\u5ea6\u7ed8\u5236\u662f\u4e00\u4e2a\u672f\u8bed\uff0c\u8868\u793a\u67d0\u4e9b\u7ec4\u4ef6\u5728\u5c4f\u5e55\u4e0a\u7684\u4e00\u4e2a\u50cf\u7d20\u70b9\u7684\u7ed8\u5236\u6b21\u6570\u8d85\u8fc7 1 \u6b21\uff09\uff0c\u5c31\u80fd\u66f4\u597d\u7684\u7a81\u51fa\u6027\u80fd\u3002<\/p>\n<h3>LinearLayout\u548cRelativeLayout \u6027\u80fd\u5927\u6bd4\u62fc\uff1a<\/h3>\n<p>LinearLayout \u5728 measure \u7684\u65f6\u5019\uff0c\u5728\u6a2a\u5411\u6216\u8005\u7eb5\u5411\u4f1a\u53bb\u6d4b\u91cf\u5b50 View \u7684\u5bbd\u5ea6\u6216\u9ad8\u5ea6\uff0c\u4e14\u53ea\u4f1a\u6d4b\u91cf\u4e00\u6b21\u3002\u4f46\u662f\u5f53\u8bbe\u7f6e layout_weight \u5c5e\u6027\u7684\u65f6\u5019\u4f1a\u53bb\u6d4b\u91cf\u4e24\u6b21\u624d\u80fd\u83b7\u5f97\u7cbe\u786e\u7684\u5c55\u793a\u5c3a\u5bf8\u3002<\/p>\n<p>RelativeLayout \u5728 measure \u7684\u65f6\u5019\u4f1a\u5728\u6a2a\u5411\u548c\u7eb5\u5411\u5404\u6d4b\u91cf\u4e00\u6b21\u3002\u4e5f\u5c31\u662f\u9700\u8981\u4e24\u6b21\u5ea6\u91cf\u6765\u786e\u4fdd\u81ea\u5df1\u5904\u7406\u4e86\u6240\u6709\u7684\u5e03\u5c40\u5173\u7cfb\u3002<\/p>\n<p>\u9ad8\u80fd\uff1a\u5982\u679c\u5e26\u6709 layout_weight \u5c5e\u6027\u7684 LinearLayout \u6216\u8005 RelativeLayout \u88ab\u5957\u5d4c\u4f7f\u7528\uff0cmeasure \u6240\u8d39\u65f6\u95f4\u53ef\u80fd\u4f1a\u5448\u6307\u6570\u7ea7\u589e\u957f\uff08\u4e24\u4e2a\u5957\u5d4c\u7684\u53f6\u5b50 View \u4f1a\u6709\u56db\u6b21 measure\uff0c\u4e09\u4e2a\u5957\u5d4c\u7684\u53f6\u5b50 view \u4f1a\u67098\u6b21\u7684 measure\uff09\u3002\u4e3a\u4e86\u7f29\u77ed\u8fd9\u4e2a\u65f6\u95f4\uff0c\u4fdd\u6301\u6811\u5f62\u7ed3\u6784\u5c3d\u91cf\u6241\u5e73\uff08\u6df1\u5ea6\u4f4e\uff09\uff0c\u800c\u4e14\u5c3d\u91cf\u8981\u79fb\u9664\u6240\u6709\u4e0d\u9700\u8981\u6e32\u67d3\u7684 View\u3002<\/p>\n<ul>\n<li>\u5c3d\u91cf\u907f\u514d\u5728\u89c6\u56fe\u5c42\u7ea7\u7684\u9876\u5c42\u4f7f\u7528\u76f8\u5bf9\u5e03\u5c40 RelativeLayout\u3002\u76f8\u5bf9\u5e03\u5c40 RelativeLayout \u6bd4\u8f83\u8017\u8d44\u6e90\uff0c\u56e0\u4e3a\u4e00\u4e2a\u76f8\u5bf9\u5e03\u5c40 RelativeLayout\u9700\u8981\u4e24\u6b21\u5ea6\u91cf\u6765\u786e\u4fdd\u81ea\u5df1\u5904\u7406\u6240\u6709\u7684\u5e03\u5c40\u5173\u7cfb\uff1b<\/li>\n<li>\u5e03\u5c40\u5c42\u7ea7\u9876\u5c42\u5efa\u8bae\u4f7f\u7528\u7ebf\u6027\u5e03\u5c40 LinearLayout \u4ee3\u66ff\u76f8\u5bf9\u5e03\u5c40 RelativeLayout\uff0c\u56e0\u4e3a\u7ebf\u6027\u5e03\u5c40 LinearLayout \u6027\u80fd\u8981\u66f4\u9ad8\u4e00\u4e9b\uff1b<\/li>\n<li>\u5f53LinearLayout \u9700\u8981\u5d4c\u5957\u624d\u80fd\u5b9e\u73b0\u7684\u590d\u6742\u5e03\u5c40\uff0c\u5efa\u8bae\u4f7f\u7528RelativeLayout\uff1b<\/li>\n<li>\u5c3d\u91cf\u907f\u514d\u4f7f\u7528 layout_weight \u5c5e\u6027\u3002\uff08\u4f46\u662f\u7684\u786e\u5728\u7b49\u5206\u5e03\u5c40\u65f6\u633a\u597d\u7528\u7684\uff09<\/li>\n<\/ul>\n<h3>\u5176\u4ed6\u5e03\u5c40\u4f18\u5316<\/h3>\n<ul>\n<li>include\u6807\u7b7e \uff08\u5f15\u5165\u5e03\u5c40\uff09<\/li>\n<li>merge\u6807\u7b7e\uff08\u7b80\u5355\u7c97\u66b4\u70b9\u56de\u7b54\uff1a\u5e72\u6389\u4e00\u4e2aView\u5c42\u7ea7\u3002\u4e3b\u8981\u4f5c\u7528\u662f\u4e3a\u4e86\u9632\u6b62\u5728\u5f15\u7528\u5e03\u5c40\u6587\u4ef6\u65f6\u4ea7\u751f\u591a\u4f59\u7684\u5e03\u5c40\u5d4c\u5957\uff09<\/li>\n<li>ViewStub\u6807\u7b7e\uff08\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u9ad8\u6548\u5360\u4f4d\u7b26\u3002ViewStub\u662fView\u7684\u4e00\u79cd\uff0c\u4f46\u662f\u5b83\u6ca1\u6709\u5927\u5c0f\uff0c\u6ca1\u6709\u7ed8\u5236\u529f\u80fd\uff0c\u4e5f\u4e0d\u53c2\u4e0e\u5e03\u5c40\uff0c\u8d44\u6e90\u6d88\u8017\u975e\u5e38\u4f4e\u3002\u4e3b\u8981\u4f5c\u7528\u662f\u628a\u6211\u4eec\u4e0d\u5e38\u7528\u7684View\u5728\u6211\u4eec\u9700\u8981\u7684\u65f6\u5019\u624d\u53bb\u52a0\u8f7d\uff09<\/li>\n<\/ul>\n<h3>Android\u6700\u65b0\u7684\u5e03\u5c40\u65b9\u5f0fConstaintLayout<\/h3>\n<p>ConstraintLayout\u5141\u8bb8\u5728\u4e0d\u9002\u7528\u4efb\u4f55\u5d4c\u5957\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u5927\u578b\u800c\u53c8\u590d\u6742\u7684\u5e03\u5c40\u3002\u5b83\u4e0eRelativeLayout\u975e\u5e38\u76f8\u4f3c\uff0c\u6240\u6709\u7684View\u90fd\u4f9d\u8d56\u4e8e\u5144\u5f1f\u63a7\u4ef6\u548c\u7236\u63a7\u4ef6\u7684\u76f8\u5bf9\u5173\u7cfb\u3002\u4f46\u662f\uff0cConstraintLayout\u6bd4RelativeLayout\u66f4\u52a0\u7075\u6d3b\uff0c\u652f\u6301\u6bd4\u4f8b\u5e03\u5c40\uff0c\u76ee\u524d\u5728Android Studio\u4e2d\u4f7f\u7528\u4e5f\u5341\u5206\u65b9\u4fbf\u3002<\/p>\n<h3>\u8fc7\u5ea6\u7ed8\u5236<\/h3>\n<p>\u8fc7\u5ea6\u7ed8\u5236\uff1a\u8fc7\u6e21\u7ed8\u5236\u662f\u4e00\u4e2a\u672f\u8bed\uff0c\u8868\u793a\u67d0\u4e9b\u7ec4\u4ef6\u5728\u5c4f\u5e55\u4e0a\u7684\u4e00\u4e2a\u50cf\u7d20\u70b9\u7684\u7ed8\u5236\u6b21\u6570\u8d85\u8fc7 1 \u6b21\u3002\u8fc7\u5ea6\u7ed8\u5236\u5bfc\u81f4\u7684\u95ee\u9898\u662f\u82b1\u8d39\u592a\u591a\u7684\u65f6\u95f4\u53bb\u7ed8\u5236\u90a3\u4e9b\u5806\u53e0\u5728\u4e0b\u9762\u7684\u3001\u7528\u6237\u770b\u4e0d\u5230\u7684\u4e1c\u897f\uff0c\u6d6a\u8d39 CPU \u5468\u671f\u548c\u6e32\u67d3\u65f6\u95f4\u3002<\/p>\n<p>\u4f18\u5316\uff1a<\/p>\n<ul>\n<li>\u53bb\u9664\u91cd\u590d\u6216\u8005\u4e0d\u5fc5\u8981\u7684 background<\/li>\n<li>\u70b9\u51fb\u6001\u4e2d\u7684 normal \u5c3d\u91cf\u8bbe\u7f6e\u6210 transparent<\/li>\n<li>\u53bb\u9664 window \u4e2d\u7684 background\uff08\u53ef\u4ee5\u901a\u8fc7\u5904\u7406 decorView \u6216\u8005\u8bbe\u7f6e Theme \u7684\u65b9\u5f0f\uff09<\/li>\n<\/ul>\n<h3>CPU \u6216\u8005 GPU \u8d1f\u8f7d\u8fc7\u91cd<\/h3>\n<p>UI \u7ebf\u7a0b\u662f\u5e94\u7528\u7684\u4e3b\u7ebf\u7a0b\uff0c\u5f88\u591a\u7684\u6027\u80fd\u548c\u5361\u987f\u95ee\u9898\u662f\u7531\u4e8e\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u505a\u4e86\u5927\u91cf\u7684\u5de5\u4f5c\u3002\u9664\u4e86\u4e3b\u7ebf\u7a0b\u5916\uff0c\u5b50\u7ebf\u7a0b\u5360\u7528\u8fc7\u591a CPU \u8d44\u6e90\u4e5f\u4f1a\u5bfc\u81f4\u6e32\u67d3\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<p>\u5728 UI \u6e32\u67d3\u7684\u8fc7\u7a0b\u4e2d\uff0c\u662f CPU \u548c GPU \u5171\u540c\u5408\u4f5c\u5b8c\u6210\u7684\uff0c\u5176\u4e2d CPU \u8d1f\u8d23\u628a UI \u7ec4\u4ef6\u8ba1\u7b97\u6210 Polygons\uff0cTexture \u7eb9\u7406\uff0c\u7136\u540e\u4ea4\u7ed9 GPU \u8fdb\u884c\u6805\u683c\u5316\u6e32\u67d3\u3002\u5982\u679c\u5728\u4e3b\u7ebf\u7a0b\u505a\u4e86\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u7b80\u5355\u7684\u4f1a\u9020\u6210\u754c\u9762\u7684\u5361\u987f\uff0c\u4e25\u91cd\u7684\u76f4\u63a5ANR\u3002<\/p>\n<p>\u5404\u754c\u9762\u8017\u65f6\u64cd\u4f5cANR\u65f6\u95f4\u5bf9\u6bd4\uff1a<\/p>\n<ul>\n<li>\u4e3b\u7ebf\u7a0b\uff1a5\u79d2<\/li>\n<li>\u5e7f\u64ad\u63a5\u6536\u8005\uff1a10\u79d2<\/li>\n<li>\u670d\u52a1\uff1a20s<\/li>\n<\/ul>\n<h3>\u9891\u7e41GC<\/h3>\n<p>\u5982\u679c\u9891\u7e41\u521b\u5efa\u5927\u5bf9\u8c61\u6216\u8005\u9891\u7e41\u521b\u5efa\u5927\u91cf\u5bf9\u8c61\uff0c\u5e76\u4e14\u8fd9\u4e9b\u5bf9\u8c61\u5c5e\u4e8e\u7528\u5b8c\u5c31\u5e9f\u5f03\u7684\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6296\u52a8\u7684\u95ee\u9898\u3002<\/p>\n<ul>\n<li>\u5927\u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528\u5bf9\u8c61\u6c60\u590d\u7528\uff0c\u6bd4\u5982 byte[]<\/li>\n<li>\u5c3d\u91cf\u5728 16ms \u5185\u5c11\u521b\u5efa\u5bf9\u8c61\uff0c\u6bd4\u5982\u4e0d\u8981\u5728 onDraw \u4e2d\u521b\u5efa Paint \u5bf9\u8c61\uff0cdecodeBitmap \u4e4b\u7c7b\u7684<\/li>\n<\/ul>\n<h3>\u5176\u4ed6\u4f18\u5316<\/h3>\n<ul>\n<li>\u5982\u679c\u662f\u4e00\u4e9b\u7b80\u5355\u7684\u56fe\u7247\uff0c\u4f7f\u7528ShapeDrawable<\/li>\n<li>\u5982\u679c\u662f\u53ef\u62c9\u4f38\u7684\u56fe\u7247\u80cc\u666f\uff0c\u4f7f\u7528draw9patch<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5361\u987f\u7f6a\u9b41\u7978\u9996 UI\u5361\u987f\u5c31\u662fAndroid\u5728\u754c\u9762\u6e32\u67d3\u65f6\u4e22\u5e27\u3002 Android\u7cfb\u7edf\u8981\u6c42\u6bcf\u4e00\u5e27\u90fd\u8981\u5728 16ms \u5185\u7ed8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-961","post","type-post","status-publish","format-standard","hentry","category-android-ui"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=961"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/961\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}