{"id":34823,"date":"2024-05-30T11:06:08","date_gmt":"2024-05-30T03:06:08","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=34823"},"modified":"2024-05-30T11:06:08","modified_gmt":"2024-05-30T03:06:08","slug":"%e4%b8%80%e4%b8%aa%e7%ae%80%e6%98%93%e5%ae%89%e5%8d%93%e8%a7%a6%e6%8e%a7app%e7%9a%84%e5%bc%80%e5%8f%91%e8%a6%81%e6%80%8e%e4%b9%88%e5%81%9a%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2024\/34823\/","title":{"rendered":"\u4e00\u4e2a\u7b80\u6613\u5b89\u5353\u89e6\u63a7app\u7684\u5f00\u53d1\u8981\u600e\u4e48\u505a\uff1f"},"content":{"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<\/p>\n

## \u539f\u7406<\/p>\n

\u5728 Android \u5e73\u53f0\u4e0a\u5f00\u53d1\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4f7f\u7528\u4ee5\u4e0b\u4e24\u4e2a\u4e3b\u8981\u7ec4\u4ef6\uff1a<\/p>\n

1. View\uff1a\u89c6\u56fe\u662f\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u672c\u6784\u5efa\u5757\uff0c\u5b83\u8d1f\u8d23\u5904\u7406\u56fe\u5f62\u548c\u7528\u6237\u4ea4\u4e92\u3002\u5728\u6211\u4eec\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u89c6\u56fe\uff0c\u4ee5\u652f\u6301\u7528\u6237\u5728\u753b\u5e03\u4e0a\u7ed8\u5236\u56fe\u6848\u3002<\/p>\n

2. MotionEvent\uff1a\u8fd9\u662f Android \u4e2d\u7684\u4e00\u4e2a\u4e8b\u4ef6\u7c7b\uff0c\u7528\u4e8e\u63cf\u8ff0\u5c4f\u5e55\u4e0a\u7684\u89e6\u6478\u4e8b\u4ef6\uff0c\u5982\u70b9\u51fb\u3001\u6ed1\u52a8\u7b49\u3002\u6211\u4eec\u9700\u8981\u76d1\u542c\u548c\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\uff0c\u4ee5\u54cd\u5e94\u7528\u6237\u7684\u8f93\u5165\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u7ed8\u56fe\u64cd\u4f5c\u3002<\/p>\n

\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u57fa\u672c\u539f\u7406\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u521b\u5efa\u4e00\u4e2a\u7b80\u5b89\u5353app<\/a>\u5355\u7684\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n

## \u8be6\u7ec6\u6b65\u9aa4<\/p>\n

### 1. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Android \u9879\u76ee<\/p>\n

\u6253\u5f00 Android Studio\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a “SimpleTouchApp” \u7684\u65b0\u9879\u76ee\u3002\u9009\u62e9 Empty Activity\uff08\u7a7a\u6d3b\u52a8\uff09\uff0c\u7136\u540e\u5355\u51fb “Finish”\uff08\u5b8c\u6210\uff09\u3002<\/p>\n

### 2. \u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u89c6\u56fe<\/p>\n

\u4e3a\u4e86\u5b9e\u73b0\u753b\u5e03\u4e0a\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u89c6\u56fe\u3002\u5728\u9879\u76ee\u7684 `app\/java` \u76ee\u5f55\u4e2d\uff0c\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a `MyTouch<\/p>\n

<\/figure>\n<\/p>\n

View` \u7684\u65b0 Java \u6587\u4ef6\u3002\u5c06\u6b64\u6587\u4ef6\u8bbe\u7f6e\u4e3a\u7ee7\u627f `View` \u7c7b\uff0c\u7136\u540e\u8986\u76d6\u5176 `onDraw`\uff08\u7ed8\u5236\uff09\u65b9\u6cd5\u3002\u60a8\u8fd8\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u753b\u7b14\u5bf9\u8c61\u6765\u8bbe\u7f6e\u7ed8\u56fe\u7684\u989c\u8272\u3001\u7c97\u7ec6\u7b49\u5c5e\u6027\u3002<\/p>\n

“`java<\/p>\n

import android.content.Context;<\/p>\n

import android.graphics.Canvas;<\/p>\n

import android.graphics.Color;<\/p>\n

import android.graphics.Paint;<\/p>\n

import android.graphics.Path;<\/p>\n

import android.util.AttributeSet;<\/p>\n

import android.view.MotionEvent;<\/p>\n

import android.view.View;<\/p>\n

public class MyTouchView extends View {<\/p>\n

private Paint pa\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}]}}