zoukankan      html  css  js  c++  java
  • Simple Gesture – Fling

    Simple Gesture – Fling | Monkey Can Code


    Simple Gesture – Fling
    17. November 2010 · 5 comments    · Categories: Android, Code · Tags: Android, Fling, Gesture, Touch   

    In Android, it’s very easy to implement a simple gesture such as Fling (the action of your finger wipe across the screen in a straight line).

    for the Event onFling, if you don’t have code to handle finer gesture detection, such as taking into account x,y and velocity, Fling would work whether you swipe up and down / down and up/ left to right / right to left.

    In the following example, I will show you how to easily implement a fling gesture to your app (without using Gesture view control), to simply open another screen. (Calling another activity):

    1. You need to import the following library to your code:

    //gesture
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.MotionEvent;

    2. Your class needs to implement the OnGestureListner:

    public class Tipster extends Activity implements OnClickListener, OnGestureListener{}

    3. Create a private variable GestureDetector in your class variable definition:

    private GestureDetector myGesture ;

    4. On the class’ onCreate, initialize private GestureDetector:

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
     
            myGesture = new GestureDetector(this);

    5. The most important part, is to add onTouchEvent to your class:

        @Override
        public boolean onTouchEvent(MotionEvent event){
            return myGesture.onTouchEvent(event);
        }

    6. by implementing the class “OnGestureListener”, you will need to implement the following functions:
    To open another screen (activity), just write code inside the onFling function. For example, my onFling calls viewHistory function which opens the History screen.

        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
     
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            // TODO Auto-generated method stub
            ViewHistory();
            return false;
        }
     
        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub
     
        }
     
        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub
     
        }
     
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
     
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
            return false;
        }

    To have finer control, such as only do something when swiping from right to left try this:

    //these constants are used for onFling
        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;
     
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
     
            try {
                //do not do anything if the swipe does not reach a certain length of distance
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
     
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
     
                }
                // left to right swipe
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    ViewHistory();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
     
        }
  • 相关阅读:
    ETHINK组件取值手册
    【学习笔记】PYTHON数据分析与展示(北理工 嵩天)
    Python可视化查看数据集完整性: missingno库(用于数据分析前的数据检查)
    【学习笔记】PYTHON网络爬虫与信息提取(北理工 嵩天)
    【学习笔记】PYTHON语言程序设计(北理工 嵩天)
    ORACLE隐藏参数查看及修改
    LINUX中ORACLE 11.2.0.1 升级到11.2.0.4
    ORACLE ORION测试IO性能
    OEL7.6设置光盘YUM源
    连载《一个程序猿的生命周期》- 40、张弛有度的工作,留给自己一些思考的时间
  • 原文地址:https://www.cnblogs.com/lexus/p/2798393.html
Copyright © 2011-2022 走看看