Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- private GestureDetectorCompat lSwipeDetector;
- RelativeLayout main_layout;
- TextView tvTxt;
- int i;
- private static final int SWIPE_MIN_DISTANCE = 130;
- private static final int SWIPE_MAX_DISTANCE = 300;
- private static final int SWIPE_MIN_VELOCITY = 200;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- i = 1;
- lSwipeDetector = new GestureDetectorCompat(this, new MyGestureListener());
- main_layout = (RelativeLayout) findViewById(R.id.main_layout);
- tvTxt = (TextView) findViewById(R.id.tvTxt);
- tvTxt.setText("" + i);
- main_layout.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- return lSwipeDetector.onTouchEvent(event);
- }
- });
- }
- private class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
- @Override
- public boolean onDown(MotionEvent e) {
- return true;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
- if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_DISTANCE)
- return false;
- if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_MIN_VELOCITY) {
- i++;
- tvTxt.setText("" + i);
- }
- return false;
- }
- }
- }
- <RelativeLayout
- android:id="@+id/main_layout"
- 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:padding="5dp"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Number"
- android:id="@+id/tvTxt"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- android:gravity="center" />
- </RelativeLayout>
- public class MainActivity extends Activity {
- ...
- // This example shows an Activity, but you would use the same approach if
- // you were subclassing a View.
- @Override
- public boolean onTouchEvent(MotionEvent event){
- int action = MotionEventCompat.getActionMasked(event);
- switch(action) {
- case (MotionEvent.ACTION_DOWN) :
- Log.d(DEBUG_TAG,"Action was DOWN");
- return true;
- case (MotionEvent.ACTION_MOVE) :
- Log.d(DEBUG_TAG,"Action was MOVE");
- return true;
- case (MotionEvent.ACTION_UP) :
- Log.d(DEBUG_TAG,"Action was UP");
- return true;
- case (MotionEvent.ACTION_CANCEL) :
- Log.d(DEBUG_TAG,"Action was CANCEL");
- return true;
- case (MotionEvent.ACTION_OUTSIDE) :
- Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
- "of current screen element");
- return true;
- default :
- return super.onTouchEvent(event);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement