Skip to content

Instantly share code, notes, and snippets.

@takeshiyako2
Created July 30, 2015 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takeshiyako2/25c376311d43a8a9fb1f to your computer and use it in GitHub Desktop.
Save takeshiyako2/25c376311d43a8a9fb1f to your computer and use it in GitHub Desktop.
Android Sample Flick Input Event
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package flick.input.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
// テキストビュー
private TextView textView1;
private TextView textView2;
// X軸最低スワイプ距離
private static final int SWIPE_MIN_DISTANCE = 50;
// X軸最低スワイプスピード
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
// Y軸の移動距離 これ以上なら横移動を判定しない
private static final int SWIPE_MAX_OFF_PATH = 250;
// タッチイベントを処理するためのインタフェース
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector = new GestureDetector(this, mOnGestureListener);
textView1 = (TextView)findViewById(R.id.textView1);
textView2 = (TextView)findViewById(R.id.textView2);
}
// タッチイベント
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
// タッチイベントのリスナー
private final GestureDetector.SimpleOnGestureListener mOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
// フリックイベント
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
try {
// 移動距離・スピードを出力
float distance_x = Math.abs((event1.getX() - event2.getX()));
float velocity_x = Math.abs(velocityX);
textView1.setText("横の移動距離:" + distance_x + " 横の移動スピード:" + velocity_x);
// Y軸の移動距離が大きすぎる場合
if (Math.abs(event1.getY() - event2.getY()) > SWIPE_MAX_OFF_PATH) {
textView2.setText("縦の移動距離が大きすぎ");
}
// 開始位置から終了位置の移動距離が指定値より大きい
// X軸の移動速度が指定値より大きい
else if (event1.getX() - event2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
textView2.setText("右から左");
}
// 終了位置から開始位置の移動距離が指定値より大きい
// X軸の移動速度が指定値より大きい
else if (event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
textView2.setText("左から右");
}
} catch (Exception e) {
// TODO
}
return false;
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment