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;
     
        }
  • 相关阅读:
    go语言最新版本 下载地址
    PHP5 各版本维护时间
    springzuul本地路由和跨服务器路由问题
    大数据学习路线(转载)
    springcoud feign超时的问题
    java 桥接模式
    springcloud单个服务内存使用详情
    centos7搭建filebeat
    centos7搭建logstash
    centos7搭建kibana
  • 原文地址:https://www.cnblogs.com/lexus/p/2798393.html
Copyright © 2011-2022 走看看