\u5b89\u5353app\u5f00\u53d1\u5de5\u5177<\/a>int;<\/p>\n private Path path;<\/p>\n
public MyTouchView(Context context, AttributeSet attrs) {<\/p>\n
super(context, attrs);<\/p>\n
paint = new Paint();<\/p>\n
path = new Path();<\/p>\n<\/p>\n
paint.setColor(Color.BLACK);<\/p>\n
paint.setStyle(Paint.Style.STROKE);<\/p>\n
paint.setStrokeWidth(5f);<\/p>\n
}<\/p>\n
@Override<\/p>\n
protected void onDraw(Canvas canvas) {<\/p>\n
canvas.drawPath(path, paint);<\/p>\n
}<\/p>\n
}<\/p>\n
“`<\/p>\n
### 3. \u5904\u7406\u89e6\u6478\u4e8b\u4ef6<\/p>\n
\u5728 `MyTouchView` \u7c7b\u4e2d\uff0c\u8986\u76d6 `onTouchEvent` \u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u5c06\u68c0\u6d4b\u7528\u6237\u7684\u89e6\u6478\u4e8b\u4ef6\u5e76\u6839\u636e\u4e8b\u4ef6\u7c7b\u578b\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n
“`java<\/p>\n
@Override<\/p>\n
public boolean onTouchEvent(MotionEvent event) {<\/p>\n
float xPos = event.getX();<\/p>\n
float yPos = event.getY();<\/p>\n
switch (event.getAction()) {<\/p>\n
case MotionEvent.ACTION_DOWN:<\/p>\n
path.moveTo(xPos, yPos);<\/p>\n
break;<\/p>\n
case MotionEvent.ACTION_MOVE:<\/p>\n
path.lineTo(xPos, yPos);<\/p>\n
break;<\/p>\n
case MotionEvent.ACTION_UP:<\/p>\n
break;<\/p>\n
}<\/p>\n
invalidate();<\/p>\n
return true;<\/p>\n
}<\/p>\n
“`<\/p>\n
### 4. \u4fee\u6539\u5e03\u5c40<\/p>\n
\u5728\u9879\u76ee\u7684 `app\/res\/layout` \u76ee\u5f55\u4e2d\uff0c\u6253\u5f00 `activity_main.xml` \u6587\u4ef6\u3002\u5220\u9664\u5176\u4e2d\u7684\u9ed8\u8ba4 “Hello World” \u6587\u672c\u89c6\u56fe\uff0c\u5e76\u5c06\u65b0\u521b\u5efa\u7684 `MyTouchView` \u6dfb\u52a0\u5230\u5e03\u5c40\u4e2d\u3002<\/p>\n
“`xml<\/p>\n<\/p>\n
xmlns:app=”http:\/\/schemas.android.com\/apk\/res-auto”<\/p>\n
xmlns:tools=”http:\/\/schemas.android.com\/tools”<\/p>\n
android:layout_width=”match_parent”<\/p>\n
android:layout_height=”match_parent”<\/p>\n
android:orientation=”vertical”<\/p>\n
tools:context=”.MainActivity”><\/p>\n<\/p>\n
android:id=”@+id\/my_touch_view”<\/p>\n
android:layout_width=”match_parent”<\/p>\n
android:layout_height=”match_parent” \/><\/p>\n
“`<\/p>\n
### 5. \u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f<\/p>\n
\u7528 Android \u6a21\u62df\u5668\u6216\u5b9e\u9645\u8bbe\u5907\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002\u60a8\u5e94\u8be5\u80fd\u591f\u5728\u5e94\u7528\u7a0b\u5e8f\u5c4f\u5e55\u4e0a\u7ed8\u5236\u4efb\u610f\u56fe\u6848\u3002<\/p>\n
\u60a8\u5df2\u5b8c\u6210\u4e00\u4e2a\u7b80\u6613\u7684 Android \u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002\u4e3a\u4e86\u6269\u5c55\u6b64\u5e94\u7528\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u5c06\u66f4\u591a\u529f\u80fd\u6dfb\u52a0\u5230\u753b\u5e03\u89c6\u56fe\uff0c\u5982\u6dfb\u52a0\u4e0d\u540c\u7684\u7ed8\u56fe\u989c\u8272\u3001\u66f4\u6539\u753b\u7b14\u5927\u5c0f\u6216\u6e05\u9664\u753b\u5e03\u7b49\u3002\u60a8\u8fd8\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u89e6\u6478\u65b9\u6cd5\u5e94\u7528\u4e8e\u521b\u5efa\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f8b\u5982\u624b\u5199\u8bc6\u522b\u6216\u6e38\u620f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u521b\u5efa\u4e00\u4e2a\u7b80\u6613 Android \u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u7684\u539f\u7406\u548c\u8be6\u7ec6\u6b65\u9aa4\u3002\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5c06\u5305\u62ec\u4e00\u4e2a\u57fa\u672c\u7684\u753b\u5e03\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u4e0a\u9762\u7ed8\u5236\u56fe\u50cf\u5e76\u901a\u8fc7\u89e6\u6478\u5c4f\u5b9e\u73b0\u4ea4\u4e92\u3002## \u539f\u7406\u5728 Android \u5e73\u53f0\u4e0a\u5f00<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[917,32793,14,1520,32794],"class_list":["post-34823","post","type-post","status-publish","format-standard","hentry","category-zuoapp","tag-917","tag-app","tag-14","tag-1520"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/34823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=34823"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/34823\/revisions"}],"predecessor-version":[{"id":34936,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/34823\/revisions\/34936"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=34823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=34823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=34823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}