{"id":8456,"date":"2023-11-10T17:25:12","date_gmt":"2023-11-10T09:25:12","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=8456"},"modified":"2023-11-10T17:25:12","modified_gmt":"2023-11-10T09:25:12","slug":"%e6%89%8b%e6%9c%baapp%e5%bc%80%e5%8f%91-%e7%94%bb%e7%82%b9%e8%a6%81%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/8456\/","title":{"rendered":"\u624b\u673aapp\u5f00\u53d1 \u753b\u70b9\u8981\u600e\u4e48\u5b9e\u73b0\uff1f"},"content":{"rendered":"

\u753b\u70b9\u662f\u79fb\u52a8\u5e94\u7528\u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u624b\u5199\u7b7e\u540d\u3001\u6d82\u9e26\u3001\u6807\u8bb0\u7b49\u529f\u80fd\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u753b\u70b9\u529f\u80fd\u3002<\/p>\n

1. \u539f\u7406<\/p>\n

\u5728\u79fb\u52a8\u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u753b\u70b9\u901a\u5e38\u662f\u901a\u8fc7\u89e6\u6478\u5c4f\u5e55\u6765\u5b9e\u73b0\u3002\u5f53\u7528\u6237\u89e6\u6478\u5c4f\u5e55\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u4f1a\u83b7\u53d6\u89e6\u6478\u70b9\u7684\u5750\u6807\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5750\u6807\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u753b\u70b9\u51fd\u6570\u3002\u753b\u70b9\u51fd\u6570\u4f1a\u5c06\u8fd9\u4e9b\u5750\u6807\u8f6c\u6362\u4e3a\u5c4f\u5e55\u4e0a\u7684\u50cf\u7d20\u5750\u6807\uff0c\u5e76\u5728\u8be5\u4f4d\u7f6e\u7ed8\u5236\u4e00\u4e2a\u70b9\u3002<\/p>\n

\u5728Android\u5e73\u53f0\u4e0a\uff0c\u753b\u70b9\u53ef\u4ee5\u901a\u8fc7Canvas\u7c7b\u6765\u5b9e\u73b0\u3002aspnet\u624b\u673a\u7aef\u5f00\u53d1\u6846\u67b6<\/a>Canvas\u7c7b\u662fAndroid\u4e2d\u7684\u4e00\u4e2a\u753b\u5e03\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u7ed8\u56fe\u65b9\u6cd5\uff0c\u5305\u62ec\u753b\u70b9\u3001\u753b\u7ebf\u3001\u753b\u5706\u3001\u753b\u77e9\u5f62\u7b49\u3002\u901a\u8fc7\u8c03\u7528Canvas\u7684\u7ed8\u56fe\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5c4f\u5e55\u4e0a\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u3002<\/p>\n

2. \u8be6\u7ec6\u4ecb\u7ecd<\/p>\n

\u4e0b\u9762\u6211\u4eec\u6765\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Android\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u753b\u70b9\u529f\u80fd\u3002<\/p>\n

\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u5e03\u5c40\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2aView\u7ec4\u4ef6\uff0c\u7528\u4e8e\u663e\u793a\u7ed8\u5236\u7684\u56fe\u5f62\u3002\u5728\u8fd9\u4e2aView\u7ec4\u4ef6\u7684onDraw\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528Canvas\u7684\u7ed8\u56fe\u65b9\u6cd5\u6765\u7ed8\u5236\u56fe\u5f62\u3002<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728Activity\u4e2d\u5904\u7406\u89e6\u6478\u4e8b\u4ef6\u3002\u5f53\u7528\u6237\u89e6\u6478\u5c4f\u5e55\u65f6\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u89e6\u6478\u70b9\u7684\u5750\u6807\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5750\u6807\u4f20\u9012\u7ed9\u753b\u70b9\u51fd\u6570\u3002<\/p>\n

\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u753b\u70b9\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n

“`java<\/p>\n

public class MainActivity extends AppCompatActivity {<\/p>\n

private Paint mPaint;<\/p>\n

private Bitmap mBitmap;<\/p>\n

private Canvas mCanvas;<\/p>\n

private float mX, mY;<\/p>\n

private static final float TOUCH_TOLERANCE = 4;<\/p>\n

@Override<\/p>\n

protected void onCreate(Bundle savedInstanceState) {<\/p>\n

super.onCreate(savedInstanceState);<\/p>\n

setContentView(R.layout.activity_main);<\/p>\n

\/\/ \u521d\u59cb\u5316\u753b\u7b14<\/p>\n

mPaint = new Paint();<\/p>\n

mPaint.setAntiAlias(true);<\/p>\n

mPaint.setDither(true);<\/p>\n

mPaint.setColor(Color.BLACK);<\/p>\n

mPaint.setStyle(Paint.Style.STROKE);<\/p>\n

mPaint.setStrokeJoin(Paint.Join.ROUND);<\/p>\n

mPaint.setStrokeCap(Paint.Cap.ROUND);<\/p>\n

mPaint.setStrokeWidth(12);<\/p>\n

\/\/ \u521d\u59cb\u5316\u753b\u5e03<\/p>\n

mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);<\/p>\n

mCanvas = new Canvas(mBitmap);<\/p>\n

mCanvas.drawColor(Color.WHITE);<\/p>\n

\/\/ \u83b7\u53d6View\u7ec4\u4ef6\u5e76\u8bbe\u7f6e\u89e6\u6478\u4e8b\u4ef6\u76d1\u542c\u5668<\/p>\n

View view = findViewById(R.id.view);<\/p>\n

view.setOnTouchListener(new View.OnTouchListener() {<\/p>\n

@Override<\/p>\n

public boolean onTouch(View v, MotionEvent event) {<\/p>\n

float x = event.getX();<\/p>\n

float y = event.getY();<\/p>\n

switch (event.getAction()) {<\/p>\n

case MotionEvent.ACTION_DOWN:<\/p>\n

touchDown(x, y);<\/p>\n

break;<\/p>\n

case MotionEvent.ACTION_MOVE:<\/p>\n

touchMove(x, y);<\/p>\n

break;<\/p>\n

case MotionEvent.ACTION_UP:<\/p>\n

touchUp();<\/p>\n

break;<\/p>\n

}<\/p>\n

return true;<\/p>\n

}<\/p>\n

});<\/p>\n

}<\/p>\n

private void touchDown(float x, float y) {<\/p>\n

mX = x;<\/p>\n

mY = y;<\/p>\n

\/\/ \u5728\u753b\u5e03\u4e0a\u7ed8\u5236\u4e00\u4e2a\u70b9<\/p>\n

mCanvas.drawPoint(mX, mY, mPaint);<\/p>\n

}<\/p>\n

private void touchMove(float x, float y) {<\/p>\n

float dx = Math.abs(x – mX);<\/p>\n

float dy = Math.abs(y – mY);<\/p>\n

\/\/ \u5982\u679c\u79fb\u52a8\u8ddd\u79bb\u8d85\u8fc7\u4e86\u9608\u503c\uff0c\u5c31\u5728\u753b\u5e03\u4e0a\u7ed8\u5236\u4e00\u6761\u7ebf<\/p>\n

if (dx >= <\/p>\n

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

TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {<\/p>\n

mCanvas.drawLine(mX, mY, x, y, mPaint);<\/p>\n

mX = x;<\/p>\n

mY = y;<\/p>\n

}<\/p>\n

}<\/p>\n

private void touchUp() {<\/p>\n

\/\/ do nothing<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u5728\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521d\u59cb\u5316\u4e86\u753b\u7b14\u548c\u753b\u5e03\uff0c\u5e76\u5728\u753b\u5e03\u4e0a\u7ed8\u5236\u4e86\u4e00\u4e2a\u767d\u8272\u80cc\u666f\u3002\u63a5\u7740\uff0c\u6211\u4eec\u83b7\u53d6\u4e86View\u7ec4\u4ef6\u5e76\u8bbe\u7f6e\u4e86\u89e6\u6478\u4e8b\u4ef6\u76d1\u542c\u5668\u3002\u5728\u89e6\u6478\u4e8bvue \u80fd\u5f00\u53d1app\u5417<\/a>\u4ef6\u76d1\u542c\u5668\u4e2d\uff0c\u6211\u4eec\u6839\u636e\u89e6\u6478\u4e8b\u4ef6\u7684\u7c7b\u578b\u8c03\u7528\u4e86touchDown\u3001touchMove\u548ctouchUp\u4e09\u4e2a\u65b9\u6cd5\u6765\u5b9e\u73b0\u753b\u70b9\u529f\u80fd\u3002<\/p>\n

\u5728touchDown\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u8bb0\u5f55\u4e86\u89e6\u6478\u70b9\u7684\u5750\u6807\uff0c\u5e76\u5728\u753b\u5e03\u4e0a\u7ed8\u5236\u4e86\u4e00\u4e2a\u70b9\u3002\u5728touchMove\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5224\u65ad\u4e86\u89e6\u6478\u70b9\u7684\u79fb\u52a8\u8ddd\u79bb\u662f\u5426\u8d85\u8fc7\u4e86\u9608\u503c\uff0c\u5982\u679c\u8d85\u8fc7\u4e86\u9608\u503c\uff0c\u5c31\u5728\u753b\u5e03\u4e0a\u7ed8\u5236\u4e00\u6761\u7ebf\u3002\u5728touchUp\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u6ca1\u6709\u505a\u4efb\u4f55\u64cd\u4f5c\u3002<\/p>\n

\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u53ea\u5b9e\u73b0\u4e86\u753b\u70b9\u529f\u80fd\uff0c\u5982\u679c\u9700\u8981\u5b9e\u73b0\u5176\u4ed6\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u53c2\u8003Canvas\u7c7b\u63d0\u4f9b\u7684\u5176\u4ed6\u7ed8\u56fe\u65b9\u6cd5\uff0c\u5e76\u5728\u89e6\u6478\u4e8b\u4ef6\u76d1\u542c\u5668\u4e2d\u6839\u636e\u89e6\u6478\u4e8b\u4ef6\u7684\u7c7b\u578b\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u753b\u70b9\u662f\u79fb\u52a8\u5e94\u7528\u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u624b\u5199\u7b7e\u540d\u3001\u6d82\u9e26\u3001\u6807\u8bb0\u7b49\u529f\u80fd\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u753b\u70b9\u529f\u80fd\u30021. \u539f\u7406\u5728\u79fb\u52a8\u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u753b\u70b9\u901a\u5e38\u662f\u901a\u8fc7\u89e6\u6478\u5c4f\u5e55\u6765\u5b9e\u73b0\u3002\u5f53\u7528\u6237\u89e6\u6478\u5c4f\u5e55\u65f6\uff0c\u5e94<\/p>\n","protected":false},"author":22,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14671,14665,2271,4857,14670],"topic":[],"class_list":["post-8456","post","type-post","status-publish","format-standard","hentry","category-appkf","tag-fastapp","tag-14665","tag-2271","tag-4857","tag-app"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/8456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=8456"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/8456\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=8456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=8456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=8456"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=8456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}