android可以識别使用者的手勢(即使用者用手指滑動的方向),通過使用者不同的手勢,進而做出不同的處理
需要使用OnGestureListener
比如說看電子書的時候翻頁,或者要滑動一些其他内容
直接上代碼
界面檔案
main.xml
view plain copy to clipboard print ?
- <?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 plain copy to clipboard print ?
- 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;
- @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;
- }
- }