Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. private GestureDetectorCompat lSwipeDetector;
  4.  
  5. RelativeLayout main_layout;
  6. TextView tvTxt;
  7. int i;
  8.  
  9. private static final int SWIPE_MIN_DISTANCE = 130;
  10. private static final int SWIPE_MAX_DISTANCE = 300;
  11. private static final int SWIPE_MIN_VELOCITY = 200;
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17.  
  18. i = 1;
  19. lSwipeDetector = new GestureDetectorCompat(this, new MyGestureListener());
  20. main_layout = (RelativeLayout) findViewById(R.id.main_layout);
  21. tvTxt = (TextView) findViewById(R.id.tvTxt);
  22. tvTxt.setText("" + i);
  23.  
  24. main_layout.setOnTouchListener(new View.OnTouchListener() {
  25. @Override
  26. public boolean onTouch(View v, MotionEvent event) {
  27. return lSwipeDetector.onTouchEvent(event);
  28. }
  29. });
  30. }
  31.  
  32. private class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
  33. @Override
  34. public boolean onDown(MotionEvent e) {
  35. return true;
  36. }
  37. @Override
  38. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
  39. if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_DISTANCE)
  40. return false;
  41. if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_MIN_VELOCITY) {
  42. i++;
  43. tvTxt.setText("" + i);
  44. }
  45. return false;
  46. }
  47. }
  48. }
  49.  
  50. <RelativeLayout
  51. android:id="@+id/main_layout"
  52. xmlns:android="http://schemas.android.com/apk/res/android"
  53. xmlns:tools="http://schemas.android.com/tools"
  54. android:layout_width="match_parent"
  55. android:layout_height="match_parent"
  56. android:padding="5dp"
  57. tools:context=".MainActivity">
  58.  
  59. <TextView
  60. android:layout_width="match_parent"
  61. android:layout_height="match_parent"
  62. android:textAppearance="?android:attr/textAppearanceLarge"
  63. android:text="Number"
  64. android:id="@+id/tvTxt"
  65. android:layout_centerVertical="true"
  66. android:layout_centerHorizontal="true"
  67. android:gravity="center" />
  68. </RelativeLayout>
  69.  
  70. public class MainActivity extends Activity {
  71. ...
  72. // This example shows an Activity, but you would use the same approach if
  73. // you were subclassing a View.
  74. @Override
  75. public boolean onTouchEvent(MotionEvent event){
  76.  
  77. int action = MotionEventCompat.getActionMasked(event);
  78.  
  79. switch(action) {
  80. case (MotionEvent.ACTION_DOWN) :
  81. Log.d(DEBUG_TAG,"Action was DOWN");
  82. return true;
  83. case (MotionEvent.ACTION_MOVE) :
  84. Log.d(DEBUG_TAG,"Action was MOVE");
  85. return true;
  86. case (MotionEvent.ACTION_UP) :
  87. Log.d(DEBUG_TAG,"Action was UP");
  88. return true;
  89. case (MotionEvent.ACTION_CANCEL) :
  90. Log.d(DEBUG_TAG,"Action was CANCEL");
  91. return true;
  92. case (MotionEvent.ACTION_OUTSIDE) :
  93. Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
  94. "of current screen element");
  95. return true;
  96. default :
  97. return super.onTouchEvent(event);
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement