
AndroidのonTouchEventを使ってタッチパネルのタップの判定サンプル@tryScreenTouch00

onTouchEventを使って画面がタッチされたかどうかを判定する。
onTouchEventを使った場合はActivity全体のタッチに対する
処理を受け取ることができる。
マルチタッチされたときの座標。
動的にコントロールを配置してレイアウトを作る
main.xmlは使わない。
動的にGLSurfaceViewをはめ込む。
画面いっぱいにボタンを配置
●大事なところ
MOVEイベントが発生してないときはgetHistorySizeが0になる
HISTORYって処理が重いときとかに間引かれたデータが入ってるのか。
ほとんどgetX/getYにしか値が入ってこない。(NEXUSだからかもしれないけど…)
内容はLogCatで確認する。
●関連項目
onTouchEvent
MotionEvent
getHistoricalX
getHistoricalY
getX
getY
getHistorySize
getPointerCount
開発環境
Eclipse IDE バージョン: 3.7 Indigo Service Release 1
ターゲットプラットフォーム: 2.3.3
API レベル: 10
package trial.sample.tryscreentouch00; import android.app.Activity; import android.app.AlertDialog; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.Window; import android.widget.LinearLayout; import android.widget.TextView; public class TryScreenTouch00Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // タイトルは無し this.requestWindowFeature(Window.FEATURE_NO_TITLE); // レイアウトを作って設定 LinearLayout layout = new LinearLayout(this); layout.setBackgroundColor(Color.rgb(255, 255, 0)); layout.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); layout.setOrientation(LinearLayout.VERTICAL); this.setContentView(layout); // テキストボックス表示する final TextView text = new TextView(this); // テキストサイズを大きめに text.setTextSize(45); text.setText("確認はLogCatで!ここのタッチイベントも拾えます"); layout.addView(text); // GLSurfaceViewを作る final SurfaceView surfaceView = new SurfaceView(this); surfaceView.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); surfaceView.setBackgroundColor(Color.CYAN); layout.addView(surfaceView); } @Override public boolean onTouchEvent(MotionEvent event) { String currAction = ""; //////////////////////////////////////////////////////////// // イベントの状態を調べる switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: currAction = "DOWN"; break; case MotionEvent.ACTION_MOVE: currAction = "MOVE"; break; case MotionEvent.ACTION_UP: currAction = "UP"; break; case MotionEvent.ACTION_CANCEL: currAction = "CANCEL"; break; } //////////////////////////////////////////////////////////// // タッチされている箇所を全部表示 // タッチされている箇所(人差し指と親指で触ってるとか) int touchedPointCount = event.getPointerCount(); // タッチされている座標 for (int i = 0; i < touchedPointCount; i++) { Log.v(currAction + " TOUCH[" + i + "]", "(" + event.getX(i) + "," + event.getY(i) + ")"); } //////////////////////////////////////////////////////////// // MOVEイベントの履歴(画面を指でスライドさせてるとき) // MOVEイベントが起こった数 int moveHistoryCount = event.getHistorySize(); // 新しい順に取り出す(最新は一番後ろ) for (int i = moveHistoryCount - 1; 0 <= i; i--) { for (int j = 0; j < touchedPointCount; j++) { Log.v(currAction + " HISTORY[" + i + "]", "(" + event.getHistoricalX(j, i) + "," + event.getHistoricalY(j, i) + ")"); } } // TODO 自動生成されたメソッド・スタブ return super.onTouchEvent(event); } //////////////////////////////////////////////////////////// // ダイアログを表示する private void showDlg(String title, String text) { AlertDialog.Builder dlg = new AlertDialog.Builder(this); dlg.setTitle(title); dlg.setMessage(text); dlg.show(); } }
Androidでレイアウトを動的に配置するサンプル@tryDynamicLayout00 AndroidでOnTouchListenerを使ったタッチ処理サンプル@tryOnTouchListener00