Quantcast
Channel: 質問!ITmedia 新着質問(Java/253)
Viewing all articles
Browse latest Browse all 2439

onFlingで方向を検知

$
0
0
AndroidStudioでonTouchなどを使えばスワイプの方向を検知することはできると思うのですがonFlingでスワイプの方向を検知する事は可能でしょうか? 参考:http://web-terminal.blogspot.jp/2013/04/android.html onFlingで画面の横幅の9割以上の距離がスワイプされたときにイベントを発生させていて右からスワイプされたときも左からスワイプされた時も 画面の横幅の9割以上の距離がスワイプされていたらイベントが発生してしまうので、 左から右にスワイプされたときだけイベントを発生させるようにしたいのですがonFlingで、それは可能でしょうか? ーーーー package com.example.keita.distanceapplication; import android.app.Activity; import android.graphics.Point; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.WindowManager; import android.widget.TextView; import android.widget.Toast; public class FlingTest extends Activity { private TextView textView_x; private TextView textView_y; // 縦横フリック値トータル float x_total = 0; float y_total = 0; private WindowManager wm; private float preX; Point point = new Point(); private GestureDetector mGestureDetector; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureDetector = new GestureDetector(this, mOnGestureListener); textView_x = (TextView)findViewById(R.id.textView1); textView_y = (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 e1, MotionEvent e2, float velocityX, float velocityY) { // velocityでフリックの距離を取得 float xxx = x_total - velocityX; x_total = xxx; int screenWidth = point.x; final double screenWidth90 = screenWidth * 0.9; if(screenWidth90 < x_total){ Toast.makeText(FlingTest.this, "スワイプ", Toast.LENGTH_SHORT).show(); } // textViewで表示 textView_x.setText( "横フリック値: " + x_total ); return true; } }; } ーーーー ーーーーーーー

Viewing all articles
Browse latest Browse all 2439

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>