您的位置:首页 > 移动开发 > Android开发

Android OnGestureListener用法 识别用户手势 左右滑动

2011-05-05 17:03 411 查看
android可以识别用户的手势(即用户用手指滑动的方向),通过用户不同的手势,从而做出不同的处理

需要使用OnGestureListener

比如说看电子书的时候翻页,或者要滑动一些其他内容

直接上代码

界面文件

main.xml

view plaincopy to clipboardprint?

<?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"

android:id="@+id/ll"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

</LinearLayout>

<?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"
android:id="@+id/ll"
>
<TextView

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>


主Activity

view plaincopy to clipboardprint?

package zy.lucifer.testGesture;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.GestureDetector;

import android.view.MotionEvent;

import android.view.View;

import android.view.GestureDetector.OnGestureListener;

import android.view.View.OnTouchListener;

import android.widget.LinearLayout;

import android.widget.TextView;

import android.widget.Toast;

public class testGesture extends Activity implements OnTouchListener,

OnGestureListener {

GestureDetector mGestureDetector;

private static final int FLING_MIN_DISTANCE = 50;

private static final int FLING_MIN_VELOCITY = 0;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mGestureDetector = new GestureDetector(this);

LinearLayout ll=(LinearLayout)findViewById(R.id.ll);

ll.setOnTouchListener(this);

ll.setLongClickable(true);

}

@Override

public boolean onTouch(View v, MotionEvent event) {

// TODO Auto-generated method stub

Log.i("touch","touch");

return mGestureDetector.onTouchEvent(event);

}

@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

if (e1.getX()-e2.getX() > FLING_MIN_DISTANCE

&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {

// Fling left

Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();

} else if (e2.getX()-e1.getX() > FLING_MIN_DISTANCE

&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {

// Fling right

Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();

}

return false;

}

@Override

public void onLongPress(MotionEvent e) {

// TODO Auto-generated method stub

}

@Override

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

float distanceY) {

// TODO Auto-generated method stub

return false;

}

@Override

public void onShowPress(MotionEvent e) {

// TODO Auto-generated method stub

}

@Override

public boolean onSingleTapUp(MotionEvent e) {

// TODO Auto-generated method stub

return false;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: