{"id":3975,"date":"2021-02-05T16:47:52","date_gmt":"2021-02-05T08:47:52","guid":{"rendered":"http:\/\/122.152.205.50\/wordpress\/?p=3975"},"modified":"2025-08-13T08:55:53","modified_gmt":"2025-08-13T00:55:53","slug":"%e5%a6%82%e4%bd%95%e8%87%aa%e5%ae%9a%e4%b9%89view","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=3975","title":{"rendered":"\u5982\u4f55\u81ea\u5b9a\u4e49View"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=3975\/#1%E8%87%AA%E5%AE%9A%E4%B9%89View%E7%9A%84%E6%A6%82%E5%BF%B5\" title=\"1.\u81ea\u5b9a\u4e49View\u7684\u6982\u5ff5\">1.\u81ea\u5b9a\u4e49View\u7684\u6982\u5ff5<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=3975\/#2%E8%87%AA%E5%AE%9A%E4%B9%89View%E7%9A%84%E5%B8%B8%E8%A7%81%E5%BD%A2%E5%BC%8F\" title=\"2.\u81ea\u5b9a\u4e49View\u7684\u5e38\u89c1\u5f62\u5f0f\">2.\u81ea\u5b9a\u4e49View\u7684\u5e38\u89c1\u5f62\u5f0f<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=3975\/#3%E8%87%AA%E5%AE%9A%E4%B9%89View%E7%9A%84%E6%80%9D%E8%B7%AF\" title=\"3.\u81ea\u5b9a\u4e49View\u7684\u601d\u8def\">3.\u81ea\u5b9a\u4e49View\u7684\u601d\u8def<\/a><ul class=\"ez-toc-list-level-2\"><li class=\"ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=3975\/#%E9%80%9A%E7%94%A8%E5%A5%97%E8%B7%AF\" title=\"\u901a\u7528\u5957\u8def\">\u901a\u7528\u5957\u8def<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=3975\/#%E4%B8%8D%E5%90%8C%E5%BD%A2%E5%BC%8F%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89View%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E5%A5%97%E8%B7%AF\" title=\"\u4e0d\u540c\u5f62\u5f0f\u7684\u81ea\u5b9a\u4e49View\u7684\u81ea\u5b9a\u4e49\u5957\u8def\">\u4e0d\u540c\u5f62\u5f0f\u7684\u81ea\u5b9a\u4e49View\u7684\u81ea\u5b9a\u4e49\u5957\u8def<\/a><ul class=\"ez-toc-list-level-6\"><li class=\"ez-toc-heading-level-6\"><ul class=\"ez-toc-list-level-6\"><li class=\"ez-toc-heading-level-6\"><ul class=\"ez-toc-list-level-6\"><li class=\"ez-toc-heading-level-6\"><ul class=\"ez-toc-list-level-6\"><li class=\"ez-toc-heading-level-6\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=3975\/#%E4%B8%80%E3%80%81%E7%BB%98%E5%88%B6%E5%9E%8B%E5%AE%9A%E4%B9%89View\" title=\"\u4e00\u3001\u7ed8\u5236\u578b\u5b9a\u4e49View\">\u4e00\u3001\u7ed8\u5236\u578b\u5b9a\u4e49View<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-6\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=3975\/#%E4%BA%8C%E3%80%81%E6%89%A9%E5%B1%95%E5%9E%8B%E8%87%AA%E5%AE%9A%E4%B9%89View\" title=\"\u4e8c\u3001\u6269\u5c55\u578b\u81ea\u5b9a\u4e49View\">\u4e8c\u3001\u6269\u5c55\u578b\u81ea\u5b9a\u4e49View<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-6\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=3975\/#%E4%B8%89%E3%80%81%E7%BB%84%E5%90%88%E5%9E%8B%E8%87%AA%E5%AE%9A%E4%B9%89View\" title=\"\u4e09\u3001\u7ec4\u5408\u578b\u81ea\u5b9a\u4e49View\">\u4e09\u3001\u7ec4\u5408\u578b\u81ea\u5b9a\u4e49View<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"1%E8%87%AA%E5%AE%9A%E4%B9%89View%E7%9A%84%E6%A6%82%E5%BF%B5\"><\/span>1.\u81ea\u5b9a\u4e49View\u7684\u6982\u5ff5<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u901a\u8fc7\u7ee7\u627fView\u3001ViewGroup\u53ca\u5176\u5b50\u7c7b\u6240\u521b\u5efa\u7684View\u5c31\u53eb\u505a\u81ea\u5b9a\u4e49View\u3002<\/p>\n<h1><span class=\"ez-toc-section\" id=\"2%E8%87%AA%E5%AE%9A%E4%B9%89View%E7%9A%84%E5%B8%B8%E8%A7%81%E5%BD%A2%E5%BC%8F\"><\/span>2.\u81ea\u5b9a\u4e49View\u7684\u5e38\u89c1\u5f62\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li>1\uff09\u7ed8\u5236\u578b\u81ea\u5b9a\u4e49View<br \/>\n\u901a\u8fc7\u7ee7\u627fView\uff0c\u81ea\u5df1\u5b9e\u73b0onDraw\u3001onTouchEvent\u65b9\u6cd5\u3002<\/li>\n<li>2\uff09\u6269\u5c55\u578b\u81ea\u5b9a\u4e49View<br \/>\n\u901a\u8fc7\u7ee7\u627f\u4e00\u4e2aView\uff0c\u901a\u8fc7\u590d\u5199\u5b83\u7684\u4e00\u4e9b\u65b9\u6cd5\u6765\u6269\u5c55\u5b83\u7684\u529f\u80fd\uff0c\u5982\u81ea\u5b9a\u4e49RecyclerView\u7b49\u3002<\/li>\n<li>\n<p>2\uff09\u7ec4\u5408\u578b\u81ea\u5b9a\u4e49View<br \/>\n\u4e00\u822c\u7ee7\u627f\u81eaViewGroup\u7684\u5b50\u7c7b\uff0c\u5982FrameLayout\u7b49\uff0c\u901a\u8fc7Inflate\u5e03\u5c40\u7136\u540efindViewById\u6216\u8005getChild\u6765\u52a8\u6001\u7684\u64cd\u4f5c\u8fd9\u4e9b\u5b50View\uff0c\u5f62\u6210\u4e00\u4e2a\u7edf\u4e00\u7684\u6574\u4f53\u3002<\/p>\n<h1><span class=\"ez-toc-section\" id=\"3%E8%87%AA%E5%AE%9A%E4%B9%89View%E7%9A%84%E6%80%9D%E8%B7%AF\"><\/span>3.\u81ea\u5b9a\u4e49View\u7684\u601d\u8def<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"%E9%80%9A%E7%94%A8%E5%A5%97%E8%B7%AF\"><\/span>\u901a\u7528\u5957\u8def<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>1\uff09\u6784\u9020\u65b9\u6cd5\u5b9a\u4e49<\/p>\n<pre><code class=\"language-java\">public XXView(@NonNull Context context) {\n    this(context,null);\n}\n\npublic XXView(@NonNull Context context, @Nullable AttributeSet attrs) {\n    this(context, attrs,-1);\n}\n\npublic XXView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {\n    super(context, attrs, defStyleAttr);\n    \/\/\u8fd9\u91cc\u505a\u4e00\u4e9b\u521d\u59cb\u5316\u7684\u64cd\u4f5c\uff0c\u5982\u83b7\u53d6\u81ea\u5b9a\u4e49\u5c5e\u6027\u3001Inflate\u5e03\u5c40\u7b49\u3002\n\uff5d<\/code><\/pre>\n<p>2\uff09\u83b7\u53d6\u6d4b\u91cf\u5bbd\u9ad8<\/p>\n<pre><code class=\"language-java\">@Override\nprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {\n    super.onMeasure(widthMeasureSpec, heightMeasureSpec);\n    mWidth = getMeasuredWidth();\n    mHeight = getMeasuredHeight();\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"%E4%B8%8D%E5%90%8C%E5%BD%A2%E5%BC%8F%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89View%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E5%A5%97%E8%B7%AF\"><\/span>\u4e0d\u540c\u5f62\u5f0f\u7684\u81ea\u5b9a\u4e49View\u7684\u81ea\u5b9a\u4e49\u5957\u8def<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h6><span class=\"ez-toc-section\" id=\"%E4%B8%80%E3%80%81%E7%BB%98%E5%88%B6%E5%9E%8B%E5%AE%9A%E4%B9%89View\"><\/span>\u4e00\u3001\u7ed8\u5236\u578b\u5b9a\u4e49View<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<p>\u8bf4\u5f97\u901a\u4fd7\u4e00\u70b9\uff0c\u5176\u5b9e\u5c31\u662f\u4f5c\u753b\u3002<br \/>\n1\uff09\u521b\u5efa\u753b\u7b14\u3002<br \/>\n\u4e5f\u5c31\u662f\u5728View\u7684\u6784\u9020\u521d\u59cb\u5316\u65b9\u6cd5\u91cc\u6784\u9020Paint\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u753b\u7b14\u7684\u5bbd\u5ea6\u3001\u989c\u8272\u7b49\u3002<\/p>\n<pre><code class=\"language-java\">cornerPaint.setColor(getContext().getResources().getColor(R.color.cutview_corner));\ncornerPaint.setStrokeWidth(cornerStrokeWidth);\ncornerPaint.setAntiAlias(true);\ncornerPaint.setStrokeCap(Paint.Cap.SQUARE);\ncornerPaint.setStyle(Paint.Style.STROKE);<\/code><\/pre>\n<p>2\uff09\u5b9a\u4e49\u4e00\u4e9b\u53c2\u6570\u53d8\u91cf<br \/>\n3)  \u4f5c\u753b<br \/>\n\u5c31\u662f\u5728onDraw\u65b9\u6cd5\uff0c\u901a\u8fc7canvas\u7ed8\u5236\u4e00\u4e9b\u56fe\u6848\uff0c\u53ef\u80fd\u4f1a\u7528\u5230\u4e0a\u9762\u5b9a\u4e49\u597d\u7684\u53c2\u6570\u53d8\u91cf\u3002<\/p>\n<p>4\uff09\u52a8\u753b<br \/>\n\u5728onTouch\u65b9\u6cd5\u91cc\u52a8\u6001\u7684\u6539\u53d8\u4e0a\u9762\u5b9a\u4e49\u7684\u53c2\u6570\u53d8\u91cf\uff0c\u7136\u540e\u8c03\u7528invalidate()\u65b9\u6cd5\u5373\u53ef\u8ba9onDraw\u65b9\u6cd5\u5237\u65b0\u3002<\/p>\n<p>5\uff09\u6dfb\u52a0\u76d1\u542c<\/p>\n<p>6\uff09\u8865\u5145\u3001\u4f18\u5316<br \/>\n\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e9b\u81ea\u5b9a\u4e49\u5c5e\u6027<\/p>\n<h6><span class=\"ez-toc-section\" id=\"%E4%BA%8C%E3%80%81%E6%89%A9%E5%B1%95%E5%9E%8B%E8%87%AA%E5%AE%9A%E4%B9%89View\"><\/span>\u4e8c\u3001\u6269\u5c55\u578b\u81ea\u5b9a\u4e49View<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<p>\u7565<\/p>\n<h6><span class=\"ez-toc-section\" id=\"%E4%B8%89%E3%80%81%E7%BB%84%E5%90%88%E5%9E%8B%E8%87%AA%E5%AE%9A%E4%B9%89View\"><\/span>\u4e09\u3001\u7ec4\u5408\u578b\u81ea\u5b9a\u4e49View<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<p>\u7565<\/p>\n<button class=\"simplefavorite-button\" data-postid=\"3975\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 1.\u81ea\u5b9a\u4e49View\u7684\u6982\u5ff52.\u81ea\u5b9a\u4e49View\u7684\u5e38\u89c1\u5f62\u5f0f3.\u81ea\u5b9a\u4e49View\u7684\u601d\u8def\u901a\u7528\u5957\u8def\u4e0d\u540c\u5f62\u5f0f\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[142],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/3975"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3975"}],"version-history":[{"count":2,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/3975\/revisions"}],"predecessor-version":[{"id":15881,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/3975\/revisions\/15881"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3975"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}