با عرض سلام و وقت بخیر خدمت کاربران سایت پی وی لرن . و کاربرانی که دوره کامل آموزش برنامه نویسی اندروید را دنبال می کنند. رویداد Multitouch در اندروید زمانی اتفاق می افتد که بیش از یک انگشت را در یک زمان روی صفحه نمایش دستگاه اندروید خود قرار دهیم. در این زمان اندروید حرکات انگشتان دست روی صفحه نمایش را شناسایی کرده و اجازه ی اقداماتی از قبیل زوم کردن تصاویر و متن، جابه جایی آبجکت ها و… را می دهد، اندروید به شما اجازه می دهد تا از این قابلیت در اپلیکیشن های خود نیز برای فعالیت های مختلف استفاده کنید، که ما در ادامه ی مباحث طرز استفاده از قابلیت Multitouch در اندروید را ارائه کرده ایم.
در ادامه ی مباحث طرز استفاده از Multitouch در اندروید و متدهای کاربردی آن را ارائه کرده ایم.
همچنین در ادامه ی آموزش برای درک بهتر طرز بکارگیری Multitouch در برنامه نویسی اندروید ، به ارائه یک مثال ساده پرداخته ایم.
زمانیکه چند انگشت روی صفحه نمایش لمس شود، اندروید رویدادهای زیر را فرخوانی می کند:
ردیف | رویداد و توضیحات آن |
---|---|
۱ | ACTION_DOWN در اولین نقطه از لمس صفحه این رویداد فرخوانی می شود. |
۲ | ACTION_POINTER_DOWN برای نقاط دیگری که علاوه بر نقطه اول روی صفحه نمایش لمس می شود این رویداد اتفاق می افتد. |
۳ | ACTION_MOVE یک عمل را در هنگام لمس صفحه انجام می دهد. |
۴ | ACTION_POINTER_UP زمانیکه یک لمس ثانویه به بالا حرکت می کند فرخوانی می شود. |
۵ | ACTION_UP زمانیکه یک لمس ثانویه به پایین حرکت می کند فرخوانی می شود. |
بنابراین برای تشخیص هر یک از موارد ذکر شده فوق، شما باید متد onTouchEvent () را لحاظ کنید.
ساختار بکارگیری متد فوق به صورت زیر است:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public boolean onTouchEvent(MotionEvent ev){ final int actionPeformed = ev.getAction(); switch(actionPeformed){ case MotionEvent.ACTION_DOWN:{ break; } case MotionEvent.ACTION_MOVE:{ break; } return true; } } |
سپس می توانید اقداماتی از قبیل بزرگنمایی، کاهش و… را انجام دهید.
برای گرفتن مختصات محل مورد نظر در صفحه از متدهای ()getX و ()getY استفاده کنید:
1 2 | final float x = ev.getX(); final float y = ev.getY(); |
علاوه بر متدهای فوق، متدهای دیگری نیز برای مدیریت Multitouch در اندروید از کلاس ارائه شده که به شرح زیر است:
ردیف | متد و توضیحات |
---|---|
۱ | getAction() این متد یک عمل را انجام می دهد. |
۲ | getPressure() این متد index فشار جاری لمس، در این رویداد را می گیرد. |
۳ | getRawX() این متد مختصات اصلی x ازاین رویداد را برمی گرداند. |
۴ | getRawY() این متد مختصات اصلی y ازاین رویداد را برمی گرداند. |
۵ | getSize() این متد اندازه ی pointer index اولیه را برمی گرداند. |
۶ | getSource() این متد منبع رویداد را می گیرد. |
۷ | getXPrecision() این متد مختصات دقیق X را برمی گرداند. |
۸ | getYPrecision() این متد مختصات دقیق y را برمی گرداند. |
در ادامه به یک مثال ساده از استفاده از ویژگی Multitouch در برنامه نویسی اندروید ارائه داده ایم.
این برنامه مختصات لمس را هنگامی که صفحه را لمس می کنید به شما نشان می دهد.
نکته : شما باید این برنامه را در یک دستگاه اندروید واقعی تست کنید.
مراحل انجام کار:
مراحل | توضیحات |
---|---|
۱ | مراحل ایجاد برنامه در اندروید استدیو را طبق بخش های قبلی تحت عنوان پکیج com.example.Pvlearn.myapplication دنبال کنید. |
۲ | به محتوای فایل src/MainActivity.java کدهای multitouch را اضافه کنید. |
۳ | طراحی مناسب با این برنامه را به res/layout/activity_main اضافه کنید. |
۴ | برنامه را اجرا کنید تا دستگاه اندروید شما نتیجه را نمایش دهد. |
محتوای main activity برنامه در فایل src/MainActivity.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package com.example.Pvlearn.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { float xAxis = 0f; float yAxis = 0f; float lastXAxis = 0f; float lastYAxis = 0f; EditText ed1, ed2, ed3, ed4; TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed1 = (EditText) findViewById(R.id.editText); ed2 = (EditText) findViewById(R.id.editText2); ed3 = (EditText) findViewById(R.id.editText3); ed4 = (EditText) findViewById(R.id.editText4); tv1=(TextView)findViewById(R.id.textView2); tv1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { final int actionPeformed = event.getAction(); switch(actionPeformed){ case MotionEvent.ACTION_DOWN:{ final float x = event.getX(); final float y = event.getY(); lastXAxis = x; lastYAxis = y; ed1.setText(Float.toString(lastXAxis)); ed2.setText(Float.toString(lastYAxis)); break; } case MotionEvent.ACTION_MOVE:{ final float x = event.getX(); final float y = event.getY(); final float dx = x - lastXAxis; final float dy = y - lastYAxis; xAxis += dx; yAxis += dy; ed3.setText(Float.toString(xAxis)); ed4.setText(Float.toString(yAxis)); break; } } return true; } }); } } |
طراحی برنامه در فایل res/layout/activity_main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | ?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:transitionGroup="true"> <TextView android:text="Multitouch example" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pvlearn" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:theme="@style/Base.TextAppearance.AppCompat" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/imageView" android:layout_alignRight="@+id/textview" android:layout_alignEnd="@+id/textview" android:hint="X-Axis" android:layout_alignLeft="@+id/textview" android:layout_alignStart="@+id/textview" android:textColorHint="#ff69ff0e" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_below="@+id/editText" android:layout_alignLeft="@+id/editText" android:layout_alignStart="@+id/editText" android:textColorHint="#ff21ff11" android:hint="Y-Axis" android:layout_alignRight="@+id/editText" android:layout_alignEnd="@+id/editText" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText3" android:layout_below="@+id/editText2" android:layout_alignLeft="@+id/editText2" android:layout_alignStart="@+id/editText2" android:hint="Move X" android:textColorHint="#ff33ff20" android:layout_alignRight="@+id/editText2" android:layout_alignEnd="@+id/editText2" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText4" android:layout_below="@+id/editText3" android:layout_alignLeft="@+id/editText3" android:layout_alignStart="@+id/editText3" android:textColorHint="#ff31ff07" android:hint="Move Y" android:layout_alignRight="@+id/editText3" android:layout_alignEnd="@+id/editText3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Touch here" android:id="@+id/textView2" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/imageView" android:layout_alignStart="@+id/imageView" android:focusable="true" android:typeface="sans" android:clickable="true" android:textColor="#ff5480ff" android:textSize="35dp" /> </RelativeLayout> |
محتوای فایل res/values/string.xml:
1 2 3 | <resources> <string name="app_name">My Application</string> </resources> |
محتوای فایل AndroidManifest.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.Pvlearn.myapplication" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
برای اجرای برنامه ی اندرویدی خود یکی از فایل های activity در برنامه ی خود کلیک کنید.
سپس روی نماد (Run) از نوار ابزار برای شروع اجرای برنامه کلیک کنید.
سپس پنجره ای برای انتخاب شبیه ساز اندروید یا دستگاه موبایلی که به سیستم متصل کرده اید ظاهر می شود:
از پنجره ی فوق دستگاه موبایل خود را انتخاب نمائید تا صفحه زیر را مشاهده کنید:
به طور پیش فرض در پس زمینه ی برنامه چیزی را مشاهده نمی کنید، حالا روی عبارت Touch here ضربه بزنید تا نتیجه را ببینید:
شما خواهید دید که فیلد Move مقدار ۰ را دارد، زیرا شما فقط یک لمس را انجام داده اید.
حالا بر روی صفحه ضربه بزنید و انگشت خود را بکشید تا مقدار فیلد فوق تغییر کند.
یکی از ویژگی های بارز سیستم عامل اندروید قابلیت چند لمسی یا Multitouch در اندروید است که اجازه ی لمس چند انگشت در یک زمان روی صفحه نمایش را می دهد، شما می توانید از قابلیت Multitouch در برنامه نویسی اندروید نیز در صورت نیاز استفاده کنید.