{"id":1671,"date":"2016-08-15T15:55:55","date_gmt":"2016-08-15T07:55:55","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1671"},"modified":"2026-08-18T12:58:51","modified_gmt":"2026-08-18T04:58:51","slug":"surfaceview%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1671","title":{"rendered":"SurfaceView\u4ecb\u7ecd"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u666e\u901a View \u7684\u7ed8\u5236\u548c\u7528\u6237\u54cd\u5e94\u90fd\u5728 UI \u4e3b\u7ebf\u7a0b\u3002\u8017\u65f6\u64cd\u4f5c\u8981\u4e22\u5230\u522b\u7684\u7ebf\u7a0b\uff0c\u5426\u5219\u4f1a\u5361\u4f4f\u754c\u9762\uff1b\u4f46\u540e\u53f0\u7ebf\u7a0b\u53c8\u4e0d\u80fd\u6539 UI\uff0c\u4f8b\u5982\u5728\u81ea\u5b9a\u4e49 View \u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">onDraw<\/code> \u91cc\u4ece\u5de5\u4f5c\u7ebf\u7a0b\u5237\u65b0\u662f\u4e0d\u5141\u8bb8\u7684\u3002<\/p>\n<\/blockquote>\n<p style=\"margin: 1em 0px;\">\u9700\u8981\u5728\u522b\u7684\u7ebf\u7a0b\u753b\u754c\u9762\u3001\u8981\u8fc5\u901f\u5237\u65b0\u3001\u6216\u6e32\u67d3\u5f88\u8017\u65f6\uff0c\u5c31\u7528 SurfaceView\u3002\u5b83\u5185\u5d4c\u4e00\u4e2a Surface\uff0c\u53ef\u4ee5\u5728\u540e\u53f0\u7ebf\u7a0b\u7ed8\u5236\u3002<\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e00\u3001\u5b98\u65b9 API \u600e\u4e48\u8bf4<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">SurfaceView \u7ee7\u627f View\uff0c\u5185\u5d4c\u4e13\u95e8\u7528\u4e8e\u7ed8\u5236\u7684 Surface\u3002\u4f60\u53ef\u4ee5\u63a7\u5236\u8fd9\u4e2a Surface \u7684\u683c\u5f0f\u548c\u5c3a\u5bf8\uff1bSurfaceView \u8d1f\u8d23\u5b83\u753b\u5728\u7a97\u53e3\u7684\u54ea\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<p style=\"margin: 1em 0px;\">Surface \u662f Z-ordered \u7684\uff0c\u603b\u5728\u81ea\u5df1\u6240\u5728\u7a97\u53e3\u7684\u540e\u9762\u3002SurfaceView \u63d0\u4f9b\u53ef\u89c1\u533a\u57df\uff1a\u533a\u57df\u5185\u7684 Surface \u624d\u770b\u5f97\u89c1\u3002\u6392\u7248\u53d7\u89c6\u56fe\u5c42\u7ea7\u5f71\u54cd\uff0c\u5144\u5f1f\u7ed3\u70b9\u4f1a\u76d6\u5728\u4e0a\u9762\u2014\u2014\u53ef\u7528\u6765\u653e\u6587\u5b57\u3001\u6309\u94ae\u7b49\u906e\u76d6\u7269\u3002\u82e5 Surface \u4e0a\u6709\u900f\u660e\u63a7\u4ef6\uff0c\u6bcf\u6b21\u53d8\u5316\u90fd\u8981\u91cd\u7b97\u900f\u660e\u6548\u679c\uff0c\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u901a\u8fc7 SurfaceHolder \u8bbf\u95ee Surface\uff0c<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">getHolder()<\/code> \u62ff\u5230\u63a5\u53e3\u3002SurfaceView \u53d8\u5f97\u53ef\u89c1\u65f6 Surface \u88ab\u521b\u5efa\uff0c\u9690\u85cf\u524d\u88ab\u9500\u6bc1\uff0c\u4ee5\u8282\u7701\u8d44\u6e90\u3002\u8981\u89c2\u5bdf\u65f6\u673a\uff0c\u91cd\u5199 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">surfaceCreated<\/code> \/ <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">surfaceDestroyed<\/code>\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6838\u5fc3\u662f\u4e24\u6761\u7ebf\u7a0b\uff1aUI \u7ebf\u7a0b\u548c\u6e32\u67d3\u7ebf\u7a0b\u3002\u6ce8\u610f\uff1a<\/p>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: decimal;\">\n<li>\n<section>\u6240\u6709 SurfaceView \u548c SurfaceHolder.Callback \u65b9\u6cd5\u90fd\u5e94\u5728 UI \u7ebf\u7a0b\uff08\u4e00\u822c\u662f\u4e3b\u7ebf\u7a0b\uff09\u8c03\u7528\u3002\u6e32\u67d3\u7ebf\u7a0b\u8bbf\u95ee\u7684\u53d8\u91cf\u8981\u540c\u6b65\u3002<\/section>\n<\/li>\n<li>\n<section>Surface \u53ef\u80fd\u88ab\u9500\u6bc1\uff0c\u53ea\u5728 surfaceCreated \u548c surfaceDestroyed \u4e4b\u95f4\u6709\u6548\uff0c\u6e32\u67d3\u7ebf\u7a0b\u5fc5\u987b\u8bbf\u95ee\u5408\u6cd5 Surface\u3002<\/section>\n<\/li>\n<\/ol>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e8c\u3001\u81ea\u5df1\u7684\u7406\u89e3<\/span><\/h2>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">1. \u5b9a\u4e49<\/h3>\n<p style=\"margin: 1em 0px;\">\u53ef\u4ee5\u76f4\u63a5\u4ece\u5185\u5b58\u6216 DMA \u7b49\u786c\u4ef6\u63a5\u53e3\u53d6\u5f97\u56fe\u50cf\u6570\u636e\uff0c\u662f\u91cd\u8981\u7684\u7ed8\u56fe\u5bb9\u5668\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7279\u70b9\u662f\uff1a\u53ef\u4ee5\u5728\u4e3b\u7ebf\u7a0b\u4e4b\u5916\u5411\u5c4f\u5e55\u7ed8\u56fe\uff0c\u907f\u514d\u753b\u56fe\u592a\u91cd\u963b\u585e\u4e3b\u7ebf\u7a0b\uff0c\u63d0\u9ad8\u53cd\u5e94\u901f\u5ea6\u3002\u6e38\u620f\u91cc\u80cc\u666f\u3001\u4eba\u7269\u3001\u52a8\u753b\u5c3d\u91cf\u5728 Canvas \u4e0a\u753b\u51fa\u3002<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">2. \u5b9e\u73b0<\/h3>\n<p style=\"margin: 1em 0px;\">\u7ee7\u627f SurfaceView \u5e76\u5b9e\u73b0 SurfaceHolder.Callback\u3002\u539f\u5219\uff1a\u6240\u6709\u7ed8\u56fe\u5fc5\u987b\u5728 Surface \u521b\u5efa\u4e4b\u540e\u5f00\u59cb\uff08Surface \u53ef\u7406\u89e3\u4e3a\u663e\u5b58\u6620\u5c04\uff0c\u5199\u5165\u5185\u5bb9\u53ef\u76f4\u63a5\u590d\u5236\u5230\u663e\u5b58\uff0c\u6240\u4ee5\u5feb\uff09\uff0c\u9500\u6bc1\u4e4b\u524d\u5fc5\u987b\u7ed3\u675f\u3002\u56e0\u6b64 Callback \u7684 surfaceCreated \/ surfaceDestroyed \u5c31\u662f\u7ed8\u56fe\u4ee3\u7801\u7684\u8fb9\u754c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u9700\u8981\u91cd\u5199\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">surfaceChanged(SurfaceHolder holder,int format,int width,int height)<\/code>\uff1a\u5927\u5c0f\u6539\u53d8\u65f6\u6fc0\u53d1\u3002<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">surfaceCreated(SurfaceHolder holder)<\/code>\uff1a\u521b\u5efa\u65f6\u6fc0\u53d1\uff0c\u4e00\u822c\u5728\u8fd9\u91cc\u542f\u52a8\u753b\u56fe\u7ebf\u7a0b\u3002<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">surfaceDestroyed(SurfaceHolder holder)<\/code>\uff1a\u9500\u6bc1\u65f6\u6fc0\u53d1\uff0c\u4e00\u822c\u505c\u6389\u3001\u91ca\u653e\u753b\u56fe\u7ebf\u7a0b\u3002<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">\u6574\u4e2a\u8fc7\u7a0b\uff1a<\/p>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: decimal;\">\n<li>\n<section>\u7ee7\u627f SurfaceView \u5e76\u5b9e\u73b0 Callback<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">getHolder()<\/code> \u5f97\u5230 SurfaceHolder<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">addCallback(callback)<\/code> \u6dfb\u52a0\u56de\u8c03<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">lockCanvas()<\/code> \u5f97\u5230 Canvas \u5e76\u9501\u5b9a<\/section>\n<\/li>\n<li>\n<section>\u5728 Canvas \u4e0a\u753b<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">unlockCanvasAndPost(canvas)<\/code> \u7ed3\u675f\u9501\u5b9a\u5e76\u63d0\u4ea4\u663e\u793a<\/section>\n<\/li>\n<\/ol>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">3. SurfaceHolder<\/h3>\n<p style=\"margin: 1em 0px;\">\u53ef\u4ee5\u5f53\u6210 Surface \u7684\u63a7\u5236\u5668\uff1a\u64cd\u7eb5 Canvas \u4e0a\u7684\u6548\u679c\u548c\u52a8\u753b\uff0c\u63a7\u5236\u8868\u9762\u3001\u5927\u5c0f\u3001\u50cf\u7d20\u3002\u51e0\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">addCallback(SurfaceHolder.Callback callback)<\/code>\uff1a\u7ed9\u5f53\u524d\u6301\u6709\u8005\u4e00\u4e2a\u56de\u8c03\u5bf9\u8c61\u3002<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">lockCanvas()<\/code>\uff1a\u9501\u5b9a\u753b\u5e03\uff0c\u4e4b\u540e\u7528\u8fd4\u56de\u7684 Canvas \u4f5c\u753b\u3002<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">lockCanvas(Rect dirty)<\/code>\uff1a\u53ea\u9501\u4e00\u5757\u810f\u533a\u3002\u63d0\u4ea4\u540e\u53ea\u6539\u8fd9\u5757\uff0c\u5185\u5b58\u5403\u7d27\u7684\u6e38\u620f\u53ef\u907f\u514d\u91cd\u753b dirty \u5916\u50cf\u7d20\u3002<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">unlockCanvasAndPost(Canvas canvas)<\/code>\uff1a\u7ed3\u675f\u9501\u5b9a\u5e76\u63d0\u4ea4\u6539\u53d8\u3002<\/section>\n<\/li>\n<\/ul>\n<table style=\"border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; text-align: left; overflow: auto; display: table; overflow-wrap: break-word; word-break: break-all;\" width=\"817\">\n<thead>\n<tr>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u6b65\u9aa4<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u8c03\u7528<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u62ff\u63a7\u5236\u5668<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">getHolder()<\/code><\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">SurfaceHolder<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u76d1\u542c\u751f\u547d\u5468\u671f<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">addCallback<\/code><\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u521b\u5efa\/\u9500\u6bc1\/\u5c3a\u5bf8\u53d8\u5316<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u5f00\u753b<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">lockCanvas<\/code><\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u72ec\u5360 Canvas<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u63d0\u4ea4<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">unlockCanvasAndPost<\/code><\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u663e\u793a\u5230\u5c4f\u5e55<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">4. Surface \u8c03\u7528\u8fc7\u7a0b\u56fe<\/h3>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/08\/surfaceview_1.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1672\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/08\/surfaceview_1.png\" alt=\"surfaceview_1\" width=\"781\" height=\"469\" \/><\/a><\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e09\u3001\u4f8b\u5b50\uff1a\u77e9\u5f62 + \u8ba1\u65f6\u5668<\/span><\/h2>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import android.app.Activity;\n import android.content.Context;\n import android.graphics.Canvas;\n import android.graphics.Color;\n import android.graphics.Paint;\n import android.graphics.Rect;\n import android.os.Bundle;\n import android.view.SurfaceHolder;\n import android.view.SurfaceView;\n  \n public class ViewTest extends Activity {\n     \/** Called when the activity is first created. *\/\n     @Override\n     public void onCreate(Bundle savedInstanceState) {\n         super.onCreate(savedInstanceState);\n         setContentView(new MyView(this));\n     }\n     \/\/\u89c6\u56fe\u5185\u90e8\u7c7b\n     class MyView extends SurfaceView implements SurfaceHolder.Callback\n     {\n         private SurfaceHolder holder;\n         private MyThread myThread; \n         public MyView(Context context) {\n             super(context);\n             \/\/ TODO Auto-generated constructor stub\n             holder = this.getHolder();\n             holder.addCallback(this);\n             myThread = new MyThread(holder);\/\/\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u7ebf\u7a0b\n         }\n  \n         @Override\n         public void surfaceChanged(SurfaceHolder holder, int format, int width,\n                 int height) {\n             \/\/ TODO Auto-generated method stub\n              \n         }\n  \n         @Override\n         public void surfaceCreated(SurfaceHolder holder) {\n             \/\/ TODO Auto-generated method stub\n             myThread.isRun = true;\n             myThread.start();\n         }\n  \n         @Override\n         public void surfaceDestroyed(SurfaceHolder holder) {\n             \/\/ TODO Auto-generated method stub\n             myThread.isRun = false;\n         }\n          \n     }\n     \/\/\u7ebf\u7a0b\u5185\u90e8\u7c7b\n     class MyThread extends Thread\n     {\n         private SurfaceHolder holder;\n         public boolean isRun ;\n         public  MyThread(SurfaceHolder holder)\n         {\n             this.holder =holder; \n             isRun = true;\n         }\n         @Override\n         public void run()\n         {\n             int count = 0;\n             while(isRun)\n             {\n                 Canvas c = null;\n                 try\n                 {\n                     synchronized (holder)\n                     {\n                         c = holder.lockCanvas();\/\/\u9501\u5b9a\u753b\u5e03\uff0c\u4e00\u822c\u5728\u9501\u5b9a\u540e\u5c31\u53ef\u4ee5\u901a\u8fc7\u5176\u8fd4\u56de\u7684\u753b\u5e03\u5bf9\u8c61Canvas\uff0c\u5728\u5176\u4e0a\u9762\u753b\u56fe\u7b49\u64cd\u4f5c\u4e86\u3002\n                         c.drawColor(Color.BLACK);\/\/\u8bbe\u7f6e\u753b\u5e03\u80cc\u666f\u989c\u8272\n                         Paint p = new Paint(); \/\/\u521b\u5efa\u753b\u7b14\n                         p.setColor(Color.WHITE);\n                         Rect r = new Rect(100, 50, 300, 250);\n                         c.drawRect(r, p);\n                         c.drawText(\"\u8fd9\u662f\u7b2c\"+(count++)+\"\u79d2\", 100, 310, p);\n                         Thread.sleep(1000);\/\/\u7761\u7720\u65f6\u95f4\u4e3a1\u79d2\n                     }\n                 }\n                 catch (Exception e) {\n                     \/\/ TODO: handle exception\n                     e.printStackTrace();\n                 }\n                 finally\n                 {\n                     if(c!= null)\n                     {\n                         holder.unlockCanvasAndPost(c);\/\/\u7ed3\u675f\u9501\u5b9a\u753b\u56fe\uff0c\u5e76\u63d0\u4ea4\u6539\u53d8\u3002\n  \n                     }\n                 }\n             }\n         }\n     }\n }<\/code><\/pre>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aSurfaceView \u628a Surface \u4ea4\u7ed9\u540e\u53f0\u7ebf\u7a0b lockCanvas \/ unlockCanvasAndPost\uff1b\u53ea\u5728 Created \u4e0e Destroyed \u4e4b\u95f4\u753b\uff0cCallback \u65b9\u6cd5\u7559\u5728\u4e3b\u7ebf\u7a0b\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u666e\u901a View \u7684\u7ed8\u5236\u548c\u7528\u6237\u54cd\u5e94\u90fd\u5728 UI \u4e3b\u7ebf\u7a0b\u3002\u8017\u65f6\u64cd\u4f5c\u8981\u4e22\u5230\u522b\u7684\u7ebf\u7a0b\uff0c\u5426\u5219\u4f1a\u5361\u4f4f\u754c\u9762\uff1b\u4f46\u540e\u53f0\u7ebf\u7a0b\u53c8\u4e0d\u80fd\u6539 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[195],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1671"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1671"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1671\/revisions"}],"predecessor-version":[{"id":1678,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1671\/revisions\/1678"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}