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>
主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;
}
}
需要使用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;
}
}
相关文章推荐
- Android OnGestureListener用法 识别用户手势 左右滑动
- Android OnGestureListener用法 识别用户手势 上下左右滑动
- Android OnGestureListener用法 识别用户手势 左右滑动
- Android 手势识别 (左右滑动)实现 页面 切换
- Android 手势识别 (左右滑动)实现 页面 切换
- Android 手势识别 (左右滑动)实现 页面 切换
- Android手势识别——上下左右滑动、屏幕上下左右中区域处理
- Android三种左右滑动效果 手势识别(转)
- Android处理手势识别的解决方法(左右滑动)
- Android三种左右滑动效果 手势识别
- Android三种左右滑动效果 手势识别
- Android三种左右滑动效果 手势识别
- Android手势识别——上下左右滑动、屏幕上下左右中区域处理
- android左右手势滑动时与listview中的item事件触发有冲突
- android 手势左右滑动
- android图片随手势左右滑动
- android 利用左右滑动手势实现avtivity的跳转
- android onTouchEvent 左右手势滑动事件处理
- Android手势识别(单击 双击 抬起 短按 长按 滚动 滑动)
- Android手势识别(单击 双击 抬起 短按 长按 滚动 滑动)