{"id":4250,"date":"2018-07-16T09:49:00","date_gmt":"2018-07-16T01:49:00","guid":{"rendered":"http:\/\/www.bookcard.net\/wordpress\/?p=4250"},"modified":"2018-07-16T09:49:00","modified_gmt":"2018-07-16T01:49:00","slug":"android_game_touch","status":"publish","type":"post","link":"https:\/\/www.bookcard.net\/wordpress\/android_game_touch\/","title":{"rendered":"Android\u904a\u6232\u4e4b\u89f8\u6478\u5c4f"},"content":{"rendered":"<figure id=\"attachment_4251\" aria-describedby=\"caption-attachment-4251\" style=\"width: 1024px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Touch.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4251\" src=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Touch.jpg\" alt=\"\" width=\"1024\" height=\"768\" srcset=\"https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Touch.jpg 1024w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Touch-300x225.jpg 300w, https:\/\/www.bookcard.net\/wordpress\/wp-content\/uploads\/2018\/07\/Android_Game_Touch-768x576.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-4251\" class=\"wp-caption-text\">Android\u904a\u6232\u4e4b\u89f8\u6478\u5c4f<\/figcaption><\/figure>\n<p>Android\u6700\u521d\u5c31\u914d\u5099\u6709\u89f8\u6478\u5c4f.\u55ae\u53ea\u652f\u6301\u55ae\u9ede\u89f8\u78b0.\u76f4\u5230Android2.0(SDK 5)\u624d\u652f\u6301\u591a\u9ede\u89f8\u78b0.\u4f60\u9700\u8981\u8a3b\u518a\u89f8\u6478\u76e3\u807d\u5668OnTouchListener\u63a5\u53e3,\u901a\u904eonTouch()\u51fd\u5f0f\u63a5\u53d7\u89f8\u78b0\u4e8b\u4ef6\u4e26\u8655\u7406.\u82e5\u5df2\u8655\u7406\u89f8\u78b0\u4e8b\u4ef6\u8fd4\u56detrue\u4f46\u5f8c\u7e8cACTION_DOWN\u4e8b\u4ef6\u5c07\u7121\u6cd5\u63a5\u6536.\u5728\u904a\u6232\u4e2d\u8981\u6301\u7e8c\u76e3\u807d\u61c9\u8fd4\u56defalse.\u800c\u4e14\u8981\u5206\u5225\u8655\u7406\u55ae\u9ede\u89f8\u78b0\u548c\u591a\u9ede\u89f8\u78b0\u5169\u7a2e\u60c5\u6cc1.<\/p>\n<p>static class TounchListener implements View.OnTouchListener {<\/p>\n<p>@Override<\/p>\n<p>public boolean onTouch(View v, MotionEvent event)<\/p>\n<p>{<\/p>\n<p>if(SYSTEM.GetVersion() &gt;= 5)<\/p>\n<p>MultiTouch(v, event);\/\/ \u591a\u9ede\u89f8\u78b0<\/p>\n<p>else<\/p>\n<p>SingleTouch(v, event);\/\/ \u55ae\u9ede\u89f8\u78b0<\/p>\n<p>return false;<\/p>\n<p>}<\/p>\n<p>\u89f8\u6478\u76e3\u807d\u5668<\/p>\n<p>private static TounchListener Tounch_Listener = new TounchListener();<\/p>\n<p>\u7136\u5f8c\u5b9a\u7fa9\u4e09\u500b\u89f8\u78b0\u4e8b\u4ef6. \u9b06\u958b\u3001\u6309\u4e0b\u3001\u62d6\u52d5.<\/p>\n<p>public static int ACTION_UP\u00a0\u00a0\u00a0 =\u00a0 1;\/\/\u00a0 \u9b06\u958b\u4e8b\u4ef6<\/p>\n<p>public static int ACTION_DOWN =\u00a0\u00a0 2;\/\/ \u6309\u4e0b\u4e8b\u4ef6<\/p>\n<p>public static int ACTION_DRAGGED = 3;\/\/ \u62d6\u52d5\u4e8b\u4ef6<\/p>\n<p>\u5b9a\u7fa9\u89f8\u78b0\u9ede\u7de9\u5b58\u5217.<\/p>\n<p>public static int MAX_POINT = 1024;<\/p>\n<p>public static int[] Point_Action = new int[MAX_POINT]; \/\/ \u4e8b\u4ef6\u7de9\u5b58<\/p>\n<p>public static int[] Point_X\u00a0\u00a0\u00a0 = new int[MAX_POINT];\/\/ x\u5ea7\u6a19\u7de9\u5b58<\/p>\n<p>public static int[] Point_Y\u00a0\u00a0\u00a0 = new int[MAX_POINT];\/\/ y\u5ea7\u6a19\u7de9\u5b58<\/p>\n<p>public static int\u00a0\u00a0 Point_Count = 0;\/\/ \u89f8\u78b0\u9ede\u91cf<\/p>\n<p>\u56e0\u70ba\u904a\u6232\u5206\u8fa8\u7387\u8207\u5c4f\u5e55\u5206\u8fa8\u7387\u4e26\u4e0d\u4e00\u81f3.\u6240\u4ee5\u4f60\u7e2e\u653e\u6bd4\u8b8a\u91cf.<\/p>\n<p>private static float\u00a0 Scale_X;\/\/\u00a0 \u5bec\u5ea6\u7e2e\u653e\u6bd4<\/p>\n<p>private static float\u00a0 Sacle_Y;\/\/\u00a0 \u9ad8\u5ea6\u7e2e\u653e\u6bd4<\/p>\n<p>\u521d\u6b64\u89f8\u78b0\u6a21\u584a.view\u70baActivity.\u5206\u5225\u8f38\u5165\u300e\u76ee\u6a19\u5206\u8fa8\u7387\u5bec\u8207\u9ad8\u300f\u548c\u300e\u8a2d\u5099\u5206\u8fa8\u7387\u5bec\u8207\u9ad8\u300f<\/p>\n<p>public static boolean Init(View view,int target_width,int target_height,int real_width,int real_height)<\/p>\n<p>{ \/\/ \u8a2d\u7f6e\u89f8\u6478\u76e3\u807d\u5668<\/p>\n<p>view.setOnTouchListener(Tounch_Listener);<\/p>\n<p>\/\/ \u8a08\u7b97\u7e2e\u653e\u56e0\u5b50<\/p>\n<p>Scale_X = (float)target_width\/(float)real_width;<\/p>\n<p>Sacle_Y = (float)target_height\/(float)real_height;<\/p>\n<p>return true;<\/p>\n<p>}<\/p>\n<p>\u7372\u53d6\u89f8\u6478\u4e8b\u4ef6. MotionEvent.getAction()\u8fd4\u56de\u89f8\u6478\u4e8b\u4ef6<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"189\">\u89f8\u6478\u5e38\u91cf<\/td>\n<td width=\"364\">\u7c21\u4ecb<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">ACTION_DOWN<\/td>\n<td width=\"364\">\u7b2c\u4e00\u96bb\u624b\u6307\u89f8\u5c4f\u6642\u89f8\u767c<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">ACTION_POINTER_DOWN<\/td>\n<td width=\"364\">\u975e\u7b2c\u4e00\u96bb\u624b\u6307\u5c4f\u6642\u89f8\u767c<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">ACTION_UP<\/td>\n<td width=\"364\">\u6700\u5f8c\u4e00\u96bb\u624b\u6307\u96e2\u958b\u5c4f\u5e55\u6642\u89f8\u767c<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">ACTION_POINTER_UP<\/td>\n<td width=\"364\">\u975e\u6700\u5f8c\u4e00\u96bb\u624b\u6307\u96e2\u958b\u5c4f\u5e55\u6642\u89f8\u767c<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">ACTION_CANCEL<\/td>\n<td width=\"364\">\u624b\u52e2\u53d6\u6d88\u6642\u89f8\u767c<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">ACTION_MOVE<\/td>\n<td width=\"364\">\u4e00\u96bb\u6216\u591a\u96bb\u624b\u6307\u79fb\u52d5\u6642\u89f8\u767c<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">ACTION_MASK<\/td>\n<td width=\"364\">\u63d0\u53d6\u89f8\u6478\u4e8b\u4ef6<\/p>\n<p>MotionEvent.getAction() &amp; MotionEvent.ACTION_MASK<\/td>\n<\/tr>\n<tr>\n<td width=\"189\">ACTION_POINTER_ID_MASK<\/td>\n<td width=\"364\">\u63d0\u53d6\u624b\u6307\u7d22\u5f15<\/p>\n<p>MotionEvent.getAction()&amp;MotionEvent.ACTION_POINTER_ID_MASK)&gt;&gt;MotionEvent.ACTION_POINT<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8655\u7406\u55ae\u9ede\u89f8\u78b0\u4e8b\u4ef6.\u7372\u53d6\u89f8\u6478\u5ea7\u6a19MotionEvent.getX()\u8fd4\u56dex\u8ef8. MotionEvent.getY()\u8fd4\u56dey\u8ef8.\u5c4f\u5e55\u5de6\u4e0a\u89d2\u70ba\u5ea7\u6a19\u539f\u9ede.x\u8ef8\u6307\u5411\u53f3\u908a.y\u8ef8\u6307\u5411\u4e0b\u908a<\/p>\n<p>static void SingleTouch(View v, MotionEvent event)<\/p>\n<p>{<\/p>\n<p>int action;<\/p>\n<p>int index;<\/p>\n<p>action = event.getAction() &amp; MotionEvent.ACTION_MASK;\/\/ \u63d0\u53d6\u89f8\u78b0\u4e8b\u4ef6\u52d5\u4f5c<\/p>\n<p>index = Point_Count; \/\/\u00a0 \u7d22\u5f15<\/p>\n<p>Point_Count = Point_Count + 1;\/\/ \u89f8\u78b0\u91cf\u52a0\u4e00<\/p>\n<p>Point_Action[index] = action; \/\/\u00a0 \u4e8b\u4ef6<\/p>\n<p>if(action == MotionEvent.ACTION_DOWN ||<\/p>\n<p>action == MotionEvent.ACTION_POINTER_DOWN)<\/p>\n<p>Point_Action[index] = ACTION_DOWN;\/\/ \u6309\u4e0b<\/p>\n<p>else<\/p>\n<p>if(action == MotionEvent.ACTION_UP ||<\/p>\n<p>action == MotionEvent.ACTION_POINTER_UP ||<\/p>\n<p>action == MotionEvent.ACTION_CANCEL)<\/p>\n<p>Point_Action[index] = ACTION_UP;\/\/ \u9b06\u958b<\/p>\n<p>else<\/p>\n<p>if(action == MotionEvent.ACTION_MOVE)<\/p>\n<p>Point_Action[index] = ACTION_DRAGGED;\/\/ \u62d6\u52d5<\/p>\n<p>\/\/ \u8a08\u7b97\u904a\u6232\u5c4f\u5e55\u5ea7\u6a19<\/p>\n<p>Point_X[index] = (int)(event.getX()*Scale_X);<\/p>\n<p>Point_Y[index] = (int)(event.getY()*Sacle_Y);<\/p>\n<p>}<\/p>\n<p>\u8655\u7406\u591a\u9ede\u89f8\u78b0\u53ef\u4ee5\u901a\u904eMotionEvent.getPointerCount()\u7372\u77e5\u540c\u6642\u6709\u5e7e\u96bb\u624b\u6307\u89f8\u78b0\u5c4f\u819c.\u800c\u624b\u6307\u89f8\u78b0\u5c4f\u5e55\u90fd\u6703\u7372\u5f97\u65b0\u7d22\u5f15.\u7d22\u5f15\u53ef\u4ee5\u901a\u904egetAction()\u63d0\u53d6:<\/p>\n<p>MotionEvent.getAction()&amp;MotionEvent.ACTION_POINTER_ID_MASK)&gt;&gt;MotionEvent.ACTION_POINTER_ID_SHIFT;<\/p>\n<p>static void MultiTouch(View v, MotionEvent event)<\/p>\n<p>{<\/p>\n<p>int action;<\/p>\n<p>int Point_index;\/\/ \u9ede\u7d22\u5f15<\/p>\n<p>action = event.getAction() &amp; MotionEvent.ACTION_MASK;\/\/\u00a0 \u63d0\u53d6\u89f8\u6478\u4e8b\u4ef6<\/p>\n<p>Point_index = (event.getAction() &amp; MotionEvent.ACTION_POINTER_ID_MASK)&gt;&gt;MotionEvent.ACTION_POINTER_ID_SHIFT;\/\/\u624b\u6307\u7d22\u5f15<\/p>\n<p>Point_Count = event.getPointerCount();\/\/\u00a0 \u624b\u6307\u89f8\u78b0\u91cf<\/p>\n<p>for(int index = 0; index&lt; Point_Count ; ++index)<\/p>\n<p>{<\/p>\n<p>if (action != MotionEvent.ACTION_MOVE &amp;&amp; index != Point_index)<\/p>\n<p>continue;<\/p>\n<p>if(action == MotionEvent.ACTION_DOWN ||<\/p>\n<p>action == MotionEvent.ACTION_POINTER_DOWN)<\/p>\n<p>Point_Action[index] = ACTION_DOWN;\/\/ \u6309\u4e0b<\/p>\n<p>else<\/p>\n<p>if(action == MotionEvent.ACTION_UP ||<\/p>\n<p>action == MotionEvent.ACTION_POINTER_UP ||<\/p>\n<p>action == MotionEvent.ACTION_CANCEL)<\/p>\n<p>Point_Action[index] = ACTION_UP;\/\/ \u9b06\u958b<\/p>\n<p>else<\/p>\n<p>if(action == MotionEvent.ACTION_MOVE)<\/p>\n<p>Point_Action[index] = ACTION_DRAGGED;\/\/ \u62d6\u52d5<\/p>\n<p>\/\/\u8b80\u53d6\u6307\u5b9a\u624b\u6307\u5ea7\u4e26\u8a08\u7b97\u904a\u6232\u5c4f\u5e55\u5ea7\u6a19<\/p>\n<p>Point_X[index] = (int)(event.getX(index)*Scale_X);<\/p>\n<p>Point_Y[index] = (int)(event.getY(index)*Sacle_Y);<\/p>\n<p>++index;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android\u6700\u521d\u5c31\u914d\u5099\u6709\u89f8\u6478\u5c4f.\u55ae\u53ea\u652f\u6301\u55ae\u9ede\u89f8\u78b0.\u76f4\u5230Android2.0(SDK 5)\u624d\u652f\u6301\u591a\u9ede\u89f8\u78b0.\u4f60\u9700 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.bookcard.net\/wordpress\/android_game_touch\/\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">Android\u904a\u6232\u4e4b\u89f8\u6478\u5c4f<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[2],"class_list":["post-4250","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/comments?post=4250"}],"version-history":[{"count":1,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4250\/revisions"}],"predecessor-version":[{"id":4252,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/posts\/4250\/revisions\/4252"}],"wp:attachment":[{"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/media?parent=4250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/categories?post=4250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bookcard.net\/wordpress\/wp-json\/wp\/v2\/tags?post=4250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}