Android-gestures

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

Android-ジェスチャー

Androidは、ピンチ、ダブルタップ、スクロール、長押し、フリンチなどの特別なタイプのタッチスクリーンイベントを提供します。 これらはすべてジェスチャーと呼ばれます。

Androidには、モーションイベントを受信し、これらのイベントがジェスチャーに対応しているかどうかを通知するGestureDetectorクラスが用意されています。 使用するには、GestureDetectorのオブジェクトを作成し、 GestureDetector.SimpleOnGestureListener で別のクラスを拡張して、リスナーとして機能し、いくつかのメソッドをオーバーライドする必要があります。 その構文は以下のとおりです-

GestureDetector myG;
myG = new GestureDetector(this,new Gesture());

class Gesture extends GestureDetector.SimpleOnGestureListener{
   public boolean onSingleTapUp(MotionEvent ev) {
   }

   public void onLongPress(MotionEvent ev) {
   }

   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
   }

   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
   }
}

ピンチジェスチャの処理

Androidには、ピンチe.t.cなどのジェスチャーを処理する ScaleGestureDetector クラスが用意されています。 それを使用するには、このクラスのオブジェクトをインスタンス化する必要があります。 その構文は次のとおりです-

ScaleGestureDetector SGD;
SGD = new ScaleGestureDetector(this,new ScaleListener());

最初のパラメーターはコンテキストで、2番目のパラメーターはイベントリスナーです。 イベントリスナーを定義し、機能 OnTouchEvent をオーバーライドして機能させる必要があります。 その構文は以下のとおりです-

public boolean onTouchEvent(MotionEvent ev) {
   SGD.onTouchEvent(ev);
   return true;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
   @Override
   public boolean onScale(ScaleGestureDetector detector) {
      float scale = detector.getScaleFactor();
      return true;
   }
}

ピンチジェスチャの他に、タッチイベントについてさらに通知する他の方法があります。 それらは以下にリストされています-

Sr.No Method & description
1

getEventTime()

このメソッドは、処理中の現在のイベントのイベント時間を取得します。

2

getFocusX()

このメソッドは、現在のジェスチャーの焦点のX座標を取得します。

3

getFocusY()

このメソッドは、現在のジェスチャーの焦点のY座標を取得します。

4

getTimeDelta()

このメソッドは、以前に受け入れられたスケーリングイベントと現在のスケーリングイベントの時間差をミリ秒単位で返します。

5

isInProgress()

このメソッドは、スケールジェスチャが進行中の場合にtrueを返します。

6

onTouchEvent(MotionEvent event)

このメソッドはMotionEventsを受け入れ、必要に応じてイベントを送出します。

以下は、ScaleGestureDetectorクラスの使用を示す例です。 ピンチでズームインおよびズームアウトできる基本的なアプリケーションを作成します。

この例を試すには、実際のデバイスまたはタッチスクリーンが有効になっているエミュレータでこれを実行します。

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 necessary 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.graphics.Matrix;
import android.os.Bundle;

import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

public class MainActivity extends Activity {
   private ImageView iv;
   private Matrix matrix = new Matrix();
   private float scale = 1f;
   private ScaleGestureDetector SGD;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      iv=(ImageView)findViewById(R.id.imageView);
      SGD = new ScaleGestureDetector(this,new ScaleListener());
   }

   public boolean onTouchEvent(MotionEvent ev) {
      SGD.onTouchEvent(ev);
      return true;
   }

   private class ScaleListener extends ScaleGestureDetector.
      SimpleOnScaleGestureListener {

      @Override
      public boolean onScale(ScaleGestureDetector detector) {
         scale *= detector.getScaleFactor();
         scale = Math.max(0.1f, Math.min(scale, 5.0f));
         matrix.setScale(scale, scale);
         iv.setImageMatrix(matrix);
         return true;
      }
   }
}

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

'_ここでabcはfinddevguidesのロゴを示します_

<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" >

   <TextView android:text="Gestures
      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:scaleType="matrix"
      android:layout_below="@+id/textView"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentBottom="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"/>

</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="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >

      <activity
         android:name="com.example.sairamkrishna.myapplicationMainActivity"
         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スタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。サンプル出力は次のようになります-

Android Gestures Tutorial

ここで、2本の指をandroid screenの上に置いて、それらを一部だけ離すと、Androidイメージがズームしていることがわかります。 それは以下の画像に示されています-

Android Gestures Tutorial

もう一度Android画面に2本の指を置き、それらを閉じてみると、Androidイメージが縮小していることがわかります。 それは以下の画像に示されています-

Android Gestures Tutorial