实现的主要原理是,重写OnGestureListener的onFling方法.OnGestureListener不能直接设置侦听的,可以设置OnTouchListener,让其指向OnGestureListener。
OnFling的四个参数意思分别为
e1: The first down motion event that started the fling.手势起点的移动事件
e2: The move motion event that triggered the current onFling.当前手势点的移动事件
velocityX: The velocity of this fling measured in pixels per second along the x axis.
X轴移动速度,像素/秒
velocityY: The velocity of this fling measured in pixels per second along the y axis.
Y轴移动速度,像素/秒
向上滑动的判定条件:向上滑动超过一定距离并且沿Y轴移动超过一定速度。
显示滑动的路径:用e1.getX()和e1.getY()获得滑动开始点坐标,用e2.getX()和e2.getY()获得滑动终止点坐标。以开始点和终止点新建一个路径,设置画笔风格后在bitmap上画出滑动路径,再显示出来。
启动应用: Intent intent = new Intent();
// 指定运行的app的入口Activity
intent.setAction("com.example.dialogactivitydemo.MainActivity");
startActivity(intent);
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/mActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</RelativeLayout>
MainActivity.java
package com.example.test3;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements OnTouchListener,
OnGestureListener {
private static final String tag = "mainActivity";
private ImageView imageView;
private GestureDetector mGestureDetector;
private int verticalMinistance = 30; // 水平最小识别距离
private int minVelocity = 10; // 最小识别速度
private RelativeLayout mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 手势监听
mGestureDetector = new GestureDetector(MainActivity.this,
(OnGestureListener) this);// (OnGestureListener) 可以省略
// 获得当前接受操作的布局id
mActivity = (RelativeLayout) findViewById(R.id.mActivity);
imageView = (ImageView) findViewById(R.id.imageView);
// 触屏监听
mActivity.setOnTouchListener(this);
mActivity.setLongClickable(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// 大于设定的最小滑动距离并且在水平/竖直方向速度绝对值大于设定的最小速度,则执行相应方法
if (e1.getX() - e2.getX() > verticalMinistance
&& Math.abs(velocityX) > minVelocity) {
Toast.makeText(MainActivity.this, "turn left", Toast.LENGTH_SHORT)
.show();
} else if (e2.getX() - e1.getX() > verticalMinistance
&& Math.abs(velocityX) > minVelocity) {
Toast.makeText(MainActivity.this, "turn right", Toast.LENGTH_SHORT)
.show();
} else if (e1.getY() - e2.getY() > 20 && Math.abs(velocityY) > 10) {
Toast.makeText(MainActivity.this, "turn up", Toast.LENGTH_SHORT)
.show();
// 连接开始滑动和停止滑动的两个点为一条实线。并在结束滑动的位置画一个三角形
Path path = new Path();
path.moveTo(e1.getX(), e1.getY());
path.lineTo(e2.getX(), e2.getY());
path.lineTo(e2.getX() + 30, e2.getY() + 50);
path.lineTo(e2.getX() - 30, e2.getY() + 50);
path.lineTo(e2.getX(), e2.getY());
// 设置画笔的样式
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(20);
// 新建一个bitmap,宽是屏幕的宽,长是屏幕的长
Bitmap bitmap = Bitmap.createBitmap(getResources()
.getDisplayMetrics().widthPixels, getResources()
.getDisplayMetrics().heightPixels, Config.ARGB_8888);
// 要在bitmap上画线
Canvas canvas = new Canvas(bitmap);
// 用画笔沿着路线画出大致的滑动路径
canvas.drawPath(path, paint);
// 在屏幕上显示出路径
imageView.setImageBitmap(bitmap);
Intent intent = new Intent();
// 指定运行的app的入口Activity
intent.setAction("com.example.dialogactivitydemo.MainActivity");
startActivity(intent);
} else if (e2.getY() - e1.getY() > 20 && Math.abs(velocityY) > 10) {
Toast.makeText(MainActivity.this, "turn down", Toast.LENGTH_SHORT)
.show();
}
return false;
}
// 只要有触发就会调用次方法
@Override
public boolean onDown(MotionEvent e) {
Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_SHORT)
.show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_SHORT)
// .show();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// Toast.makeText(MainActivity.this, "onShowPress",
// Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// Toast.makeText(MainActivity.this, "onsingleTapup",
// Toast.LENGTH_SHORT).show();
return false;
}
}