{"id":300,"date":"2020-03-25T16:53:43","date_gmt":"2020-03-25T08:53:43","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=300"},"modified":"2020-05-19T13:55:21","modified_gmt":"2020-05-19T05:55:21","slug":"android-%e8%87%aa%e5%ae%9a%e7%be%a9view","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-%e8%87%aa%e5%ae%9a%e7%be%a9view\/","title":{"rendered":"Android \u81ea\u5b9a\u7fa9View"},"content":{"rendered":"<h3>1.\u7e7c\u627fView\u4e26\u8907\u5beb\u5efa\u69cb\u65b9\u6cd5(\u7b2c\u4e00\u7a2e new\u6642\u8abf\u7528 \u7b2c\u4e8c\u7a2e\u5728xml\u6642\u8abf\u7528 \u7b2c\u4e09\u7a2e\u5728\u6709\u8a2d\u5b9a\u5c6c\u6027\u6642\u8abf\u7528)<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">class CircleView(context: Context, attrs: AttributeSet?, defStyle: Int) : View(context, attrs, defStyle) {\nconstructor(context: Context) : this(context, null, 0)\nconstructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)\n}\n<\/code><\/pre>\n<h3>2.\u8907\u5bebonDraw\u65b9\u6cd5\u4f86\u7e6a\u88fd\u81ea\u5df1\u60f3\u8981\u7684View<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">override fun onDraw(canvas: Canvas?) {\n   super.onDraw(canvas)\n\n   val paddingLeft = paddingLeft\n   val paddingRight = paddingRight\n   val paddingTop = paddingTop\n   val paddingBottom = paddingBottom\n\n   val width = width - paddingLeft - paddingRight\n   val height = height - paddingTop - paddingBottom\n\n   val r = min(width, height) \/ 2\n   canvas?.apply {\n      drawCircle((paddingLeft + width)\/2.toFloat(), (paddingTop + height)\/2.toFloat(), r.toFloat(), paint)\n   }\n}\n<\/code><\/pre>\n<h3>3.\u8a2d\u5b9a\u81ea\u5b9a\u7fa9\u5c6c\u6027\u4e4b\u524d\u5148\u5efa\u7acb\/values\/xml \u4e4b\u5f8c\u5c31\u53ef\u4ee5\u5728xml\u8abf\u7528<\/h3>\n<pre><code class=\"language-XML line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;resources&gt;\n    &lt;declare-styleable name=\"CircleView\"&gt;\n      &lt;attr name=\"circle_color\" format=\"color\"\/&gt;\n   &lt;\/declare-styleable&gt;\n&lt;\/resources&gt;\n<\/code><\/pre>\n<h3>4.\u8981\u8b93\u81ea\u5b9a\u7fa9\u5c6c\u6027\u6709\u7528 \u9700\u8981\u5728\u7a0b\u5f0f\u78bc\u88e1\u9762\u8072\u660e<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">val type = context.obtainStyledAttributes(attrs, R.styleable.CircleView)\nval color = type.getColor(R.styleable.CircleView_circle_color, Color.RED)\ntype.recycle()\nsetColor(color)\nstrokeWidth = 5f\nstyle = Paint.Style.FILL\n<\/code><\/pre>\n<h3>5.kotlin&#038;xml\u5b8c\u6574\u7a0b\u5f0f\u78bc<\/h3>\n<h4>kotlin<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">class CircleView(context: Context, attrs: AttributeSet?, defStyle: Int) : View(context, attrs, defStyle) {\n constructor(context: Context) : this(context, null, 0)\n constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)\n\n  private val paint = Paint()\n    init {\n         paint.apply {\n             val type = context.obtainStyledAttributes(attrs, R.styleable.CircleView)\n             val color = type.getColor(R.styleable.CircleView_circle_color, Color.RED)\n             type.recycle()\n             setColor(color)\n             strokeWidth = 5f\n             style = Paint.Style.FILL\n        }\n  }\n\n   override fun onDraw(canvas: Canvas?) {\n      super.onDraw(canvas)\n\n       val paddingLeft = paddingLeft\n       val paddingRight = paddingRight\n       val paddingTop = paddingTop\n       val paddingBottom = paddingBottom\n\n       val width = width - paddingLeft - paddingRight\n       val height = height - paddingTop - paddingBottom\n\n       val r = min(width, height) \/ 2\n       canvas?.apply {\n           drawCircle((paddingLeft + width)\/2.toFloat(), (paddingTop + height)\/2.toFloat(), r.toFloat(), paint)\n      }\n    }\n}\n<\/code><\/pre>\n<h4>xml<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;view\n   android:id=\"@+id\/view\"\n   app:circle_color=\"#ffff00\"\n   class=\"com.fly.fingertest.CircleView\"\n   android:layout_width=\"wrap_content\"\n   android:layout_height=\"200dp\"\n   android:padding=\"20dp\"\n   android:background=\"#000000\"\n   app:layout_constraintEnd_toEndOf=\"parent\"\n   app:layout_constraintHorizontal_bias=\"1.0\"\n   app:layout_constraintStart_toStartOf=\"parent\"\n   app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n<\/code><\/pre>\n<h4>\u81ea\u5b9a\u7fa9\u5c6c\u6027xml<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;resources&gt;\n  &lt;declare-styleable name=\"CircleView\"&gt;\n     &lt;attr name=\"circle_color\" format=\"color\"\/&gt;\n  &lt;\/declare-styleable&gt;\n&lt;\/resources&gt;\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u7e7c\u627fView\u4e26\u8907\u5beb\u5efa\u69cb\u65b9\u6cd5(\u7b2c\u4e00\u7a2e new\u6642\u8abf\u7528 \u7b2c\u4e8c\u7a2e\u5728xml\u6642\u8abf\u7528 \u7b2c\u4e09\u7a2e\u5728\u6709\u8a2d\u5b9a\u5c6c\u6027\u6642\u8abf\u7528) cla &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,15,33],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-kotlin","tag-view"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=300"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/300\/revisions\/472"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}