画面タッチは onTouchEvent を使えばを検出でき、その後に指を離した等は MotionEvent から状況を取得できます。
Android Studio 3.0.1
Android 8.1.0
Android 8.1.0
onTouchEvent
onTouchEventでのタッチ反応はMotionEventのACTION_DOWN、あるいはACTION_UPから取得することができます。
またMotionEventではタッチの移動も取り出せるので、画像などをドラッグしていくことが可能です。
MotionEvent
MotionEvent の関連はたくさんありますがよく使うメソッドはこちら
- getX(), getY()
- スクリーンの座標(pixl)
- getAction()
- ACTION_DOWN
- タッチダウン
- ACTION_MOVE
- タッチしたまま移動
- ACTION_UP
- タッチアップ
- ACTION_CANCEL
- UPとDOWNが同時に発生した
- ACTION_DOWN
- getDownTime()
- press downした時間
- getEventTime()
- イベント発生時間、uptimeMillis「ブートからの時間(msec)」
- getPressure()
- タッチ圧力
サンプルコード
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 70 71 72 73 74 75 76 77 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private StringBuffer info = new StringBuffer("Test onTouchEvent\n\n"); private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text_view); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { StringBuffer info = new StringBuffer("Test onTouchEvent\n\n"); textView.setText(info); } }); } @Override public boolean onTouchEvent(MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: info.append("ACTION_DOWN\n"); info.append("Pressure"); info.append(motionEvent.getPressure()); info.append("\n"); info.append("x1:"); info.append(motionEvent.getX()); info.append("\n"); info.append("y1:"); info.append(motionEvent.getY()); info.append("\n\n"); break; case MotionEvent.ACTION_UP: info.append("ACTION_UP\n"); info.append("x2:"); info.append(motionEvent.getX()); info.append("\n"); info.append("y2:"); info.append(motionEvent.getY()); info.append("\n"); long eventDuration2 = motionEvent.getEventTime() - motionEvent.getDownTime(); info.append("duration: "); info.append(eventDuration2); info.append(" msec\n\n"); break; case MotionEvent.ACTION_MOVE: info.append("ACTION_MOVE\n"); break; case MotionEvent.ACTION_CANCEL: info.append("ACTION_CANCEL\n"); break; } textView.setText(info); return false; } } |
レイアウトファイルです
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 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:padding="16dp" tools:context=".MainActivity"> <Button android:id="@+id/button" android:text="@string/button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> |
strings.xml
1 2 3 4 |
<resources> <string name="app_name">TestOnTouchEvent</string> <string name="button">clear</string> </resources> |
タッチ期間は
long eventDuration = motionEvent.getEventTime() – motionEvent.getDownTime();
で計算します
サンプル動画
画像をドラッグするときはMotionEventを使います。
References:
onTouchListener | Android Developers
MotionEvent | Android Developers