zoukankan      html  css  js  c++  java
  • Android使用GestureDetector实现手势滑动效果

    直接看实例:
    package com.example.gesturedetector;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.Menu;
    import android.view.MotionEvent;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnGestureListener {
    
    private GestureDetector mGestureDetector;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGestureDetector = new GestureDetector(this, this);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    mGestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
    }
    
    @Override
    public boolean onDown(MotionEvent arg0) {
    // TODO Auto-generated method stub
    //Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
    return false;
    }
    
    @Override
    public boolean onFling(MotionEvent startEvent, MotionEvent endEvent,
    float velocityX, float velocityY) {
    // TODO Auto-generated method stub
    if (startEvent.getY() - endEvent.getY() > 100) {
    Toast.makeText(this, "手势向上滑动", Toast.LENGTH_SHORT).show();
    return true;
    } else if (startEvent.getY() - endEvent.getY() < -100) {
    Toast.makeText(this, "手势向下滑动", Toast.LENGTH_SHORT).show();
    return true;
    } else if (startEvent.getX() - endEvent.getX() > 100) {
    Toast.makeText(this, "手势向左滑动", Toast.LENGTH_SHORT).show();
    return true;
    } else if (startEvent.getX() - endEvent.getX() < -100) {
    Toast.makeText(this, "手势向右滑动", Toast.LENGTH_SHORT).show();
    return true;
    }
    return false;
    }
    
    @Override
    public void onLongPress(MotionEvent arg0) {
    // TODO Auto-generated method stub
    //Toast.makeText(this, "onLongPress ", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
    float arg3) {
    // TODO Auto-generated method stub
    //Toast.makeText(this, "onScroll", Toast.LENGTH_SHORT).show();
    return false;
    }
    
    @Override
    public void onShowPress(MotionEvent arg0) {
    // TODO Auto-generated method stub
    //Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public boolean onSingleTapUp(MotionEvent arg0) {
    // TODO Auto-generated method stub
    //Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
    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.activity_main, menu);
    return true;
    }
    
    }
    事件解释:
    1.   onDown(MotionEvent e):down事件;
    2.   onSingleTapUp(MotionEvent e):一次点击up事件;
    3.   onShowPress(MotionEvent e):down事件发生而move或则up还没发生前触发该事件;
    4.   onLongPress(MotionEvent e):长按事件;
    5.   onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):滑动手势事件;
    6.   onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY):在屏幕上拖动事件。
  • 相关阅读:
    SQL Server 2012 自动增长列,值跳跃问题(自增增加1000)
    根据城市表生成json数据
    LeetCode_257. Binary Tree Paths
    LeetCode_242. Valid Anagram
    LeetCode_237. Delete Node in a Linked List
    LeetCode_235. Lowest Common Ancestor of a Binary Search Tree
    LeetCode_234. Palindrome Linked List
    LeetCode_232. Implement Queue using Stacks
    LeetCode_231. Power of Two
    LeetCode_225. Implement Stack using Queues
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7100717.html
Copyright © 2011-2022 走看看