Android-multitouch

提供:Dev Guides
移動先:案内検索

Android-マルチタッチ

マルチタッチジェスチャは、複数の指が同時に画面に触れたときに発生します。 Androidでは、これらのジェスチャーを検出できます。

Androidシステムは、複数の指が同時に画面に触れるたびに、次のタッチイベントを生成します。

Sr.No Event & description
1

ACTION_DOWN

画面に触れる最初のポインター。 これにより、ジェスチャーが開始されます。

2

ACTION_POINTER_DOWN

最初を超えて画面に入る余分なポインターの場合。

3

ACTION_MOVE

プレスジェスチャ中に変更が発生しました。

4

ACTION_POINTER_UP

非プライマリポインターが上がると送信されます。

5

ACTION_UP

最後のポインターが画面を離れると送信されます。

したがって、上記のイベントのいずれかを検出するには、* onTouchEvent()*メソッドをオーバーライドし、これらのイベントを手動で確認する必要があります。 その構文は以下のとおりです-

public boolean onTouchEvent(MotionEvent ev){
   final int actionPeformed = ev.getAction();

   switch(actionPeformed){
      case MotionEvent.ACTION_DOWN:{
         break;
      }

      case MotionEvent.ACTION_MOVE:{
         break;
      }
      return true;
   }
}

これらの場合、任意の計算を実行できます。 たとえば、ズーム、縮小など。 X軸とY軸の座標を取得するには、* getX()および getY()*メソッドを呼び出します。 その構文は以下のとおりです-

final float x = ev.getX();
final float y = ev.getY();

これらのメソッドとは別に、マルチタッチをより適切に処理するために、このMotionEventクラスによって提供される他のメソッドがあります。 これらの方法は以下のとおりです-

Sr.No Method & description
1

getAction()

このメソッドは、実行されているアクションの種類を返します

2

getPressure()

このメソッドは、最初のインデックスに対するこのイベントの現在の圧力を返します

3

getRawX()

このメソッドは、このイベントの元の生のX座標を返します

4

getRawY()

このメソッドは、このイベントの元の生のY座標を返します

5

getSize()

このメソッドは、最初のポインターインデックスのサイズを返します

6

getSource()

このメソッドは、イベントのソースを取得します

7

getXPrecision()

このメソッドは、報告されているX座標の精度を返します

8

getYPrecision()

このメソッドは、報告されているY座標の精度を返します

以下は、マルチタッチの使用方法を示す例です。 基本的なマルチタッチジェスチャアプリケーションを作成し、マルチタッチの実行時に座標を表示できるようにします。

この例を試すには、実際のデバイスでこれを実行する必要があります。

Steps Description
1 You will use android studio to create an Android application under a package com.example.sairamkrishna.myapplication.
2 Modify src/MainActivity.java file to add multitouch code.
3 Modify the res/layout/activity_main to add respective XML components.
4 Run the application and choose a running android device and install the application on it and verify the results.

以下は、変更されたメインアクティビティファイル src/MainActivity.java の内容です。

package com.example.sairamkrishna.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;
         }
      });
   }
}

以下は、xml res/layout/activity_main.xml の変更されたコンテンツです。

'_以下のコードで* abc *はfinddevguides.comのロゴを示します_

<?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="Tutorials point"
      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 の内容です。

<resources>
   <string name="app_name">My Application</string>
</resources>

以下は AndroidManifest.xml ファイルの内容です。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.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>

アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。

Anroid MediaPlayer Tutorial

オプションとしてモバイルデバイスを選択し、デフォルト画面を表示するモバイルデバイスを確認します-

Android Multitouch Tutorial

デフォルトでは、どのフィールドにも何も表示されません。 ここで[ここをタッチ]領域をタップすると、フィールドのデータが表示されます。 以下に示されています-

Android Multitouch Tutorial

単一のタッチジェスチャのみが実行されたため、[移動]フィールドのデータが0であることがわかります。 次に、画面をタップして、指のドラッグを開始します。 移動フィールドのデータの変更が表示されます。 以下に示されています-

Android Multitouch Tutorial