Android gesture action Recognition-go

Source: Internet
Author: User

Android gesture Operation recognition Time: 2012-10-20 23:09 Source: Unknown Author: admin Click: 282 times

abstract First, in Android, each gesture interaction is performed in the following order. 1. Touch the touch screen flash, triggering a motionevent event. 2. The event is ontouchlistener monitored to obtain the Motionevent object in its Ontouch () method. 3. Forward secondary motionevent objects via Gesturedetector (gesture recognizer)

First, in an Android system, each gesture interaction is performed in the following order.

1. Touch the touch screen flash, triggering a motionevent event.

2. The event is ontouchlistener monitored to obtain the Motionevent object in its Ontouch () method.

3. Forward the secondary Motionevent object to Ongesturelistener via Gesturedetector (gesture recognizer).

4. Ongesturelistener obtains the object and listens to the information encapsulated by the object to make appropriate feedback.

This order can be said to be the principle of gesture interaction, the following together to learn about Motionevent, Gesturedetector and Ongesturelistener.

Motionevent: This class is used to encapsulate action events such as gestures, touch pens, trackball, and so on. It internally encapsulates two important attributes X and Y, which are used to record the coordinates of the horizontal and vertical axes, respectively.

Gesturedetector: Identify various gestures.

Ongesturelistener: This is a listener interface for gesture interaction, which provides several abstract methods, and calls the corresponding method according to the gesturedetector gesture recognition result.

Let me show you the implementation of the gesture interaction by a code example that toggles the image, so that we have a deeper understanding and memory of the order of execution and the distinction between gestures.

First, provide a layout file that has only ImageView--main.xml.

1234567891011 <?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">   <ImageView android:id="@+id/image"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_gravity="center"/></LinearLayout>


Then, complete our activity, because to listen to touch screen touch events and gesture time, so the activity must implement Ontouchlistener and Ongesturelistener two interfaces, and override the methods. The specific code is as follows:



1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283848586 public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {   //创建一个用于识别收拾的GestureDetector对象waiyuwu.blogcn.comprivate GestureDetector detector = newGestureDetector(this);//定义一个数组,用于放漂亮的女孩int[] girls = newint[]{R.drawable.girl1, R.drawable.girl2, R.drawable.girl3}; //定义数组下标,以方便观看各个女孩private int index; private ImageView image;   @Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.main);image = (ImageView)findViewById(R.id.image); //设置一个初始显示的girl吧image.setImageResource(girls[index]);//监听这个ImageView组件上的触摸屏时间image.setOnTouchListener(this);//下面两个要记得设哦,不然就没法处理轻触以外的事件了,例如抛掷动作。image.setLongClickable(true);detector.setIsLongpressEnabled(true);}//用于呼喊下一个女孩的方法public void goNext(){ index++;index = Math.abs(index % girls.length); image.setImageResource(girls[index]);}  //重写OnTouchListener的onTouch方法//此方法在触摸屏被触摸,即发生触摸事件(接触和抚摸两个事件,挺形象)的时候被调用。@Overridepublic boolean onTouch(View v, MotionEvent event) { detector.onTouchEvent(event);returntrue;}  //在按下动作时被调用@Overridepublic boolean onDown(MotionEvent e) { returnfalse;}  //在抛掷动作时被调用@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //velocityX表示横向的移动,根据手指移动的方向切换女孩if(velocityX < 0){ goNext();}elseif(velocityX > 0){ goPrevious();}returnfalse;}   //用户呼唤上一个女孩的方法public void goPrevious(){ index--;index = Math.abs(index % girls.length); image.setImageResource(girls[index]);}  //在长按时被调用@Overridepublic void onLongPress(MotionEvent e) { }  //在滚动时调用@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { returnfalse;}  //在按住时被调用@Overridepublic void onShowPress(MotionEvent e) { }  //在抬起时被调用@Overridepublic boolean onSingleTapUp(MotionEvent e) { returnfalse;}}


When I first started learning Android, I felt that Google's documents were not good, and when we studied gestures, I felt that Google's documents were too poor. A lot of constants, properties, and methods don't even have a description. There is no description, but there are so many gestures in the ongesturelistener, it does not have a description, who can understand the relationship and difference between onlongpress and Onshowpress,onscroll and onfling before attempting to do it continuously? Google really needs to do a big surgery on the documentation. But fortunately, after my repeated attempts. These gestures are defined from a personal point of view.

    • Press (Ondown): Just the moment the finger touches the touch screen, it is the touch of the moment.

    • Throw (onfling): The finger moves quickly on the touchscreen and releases the action.

    • Long Press (onlongpress): The finger is pressed for a period of time and is not loosened.

    • Scrolling (onscroll): Fingers slide on the touchscreen.

    • Press and Hold (onshowpress): The finger is pressed on the touchscreen, its time range is pressed, and before the long press.

    • Lift (Onsingletapup): The moment the finger leaves the touch screen.

In addition to these definitions, I also summed up a bit of experience, and here to share with you.

    • Any gesture action will first perform a pressing (Ondown) action.

    • Press and hold (onshowpress) action before long pressing (onlongpress) action.

    • A lift (Onsingletapup) action is performed after holding (onshowpress) the action and pressing (Ondown) the action.

    • Hold (onlongpress), scroll (onscroll), and throw (onfling) actions do not perform a lift (onsingletapup) action.

Android gesture action Recognition-go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.