本系列文章均为A2BGeek原创,转载务必在明显处注明:
转载自A2BGeek的【Android每周专题】系列,原文链接:http://blog.csdn.net/benbmw2008/article/details/11367631
这篇专题来研究一下Android的触摸屏手势Gesture,Android的手势有两种,一种是View和Activity的(基于触摸屏事件,所以Activity也能添加手势),一种是手写输入法或者一些手机浏览器的快捷手势那样的带笔迹的手势识别。这一点从API文档中就能体现出来:分别有android.view.GestureDetector和android.gesture.Gesture。
我们先来介绍View和Activity的手势,再介绍输入法手势识别。
package com.example.gesturedemo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;
public class MyTextView extends TextView implements OnGestureListener {
private GestureDetector mGestureDetector;
public MyTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(context, this);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(context, this);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
mGestureDetector = new GestureDetector(context, this);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
DebugTool.log("MyTextView--->dispatchTouchEvent");
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
DebugTool.log("MyTextView--->onTouchEvent");
// int action = event.getAction();
// switch (action) {
// case MotionEvent.ACTION_DOWN:
// DebugTool.log("MyTextView--->onTouchEvent--->DOWN");
// break;
// case MotionEvent.ACTION_MOVE:
// DebugTool.log("MyTextView--->onTouchEvent--->MOVE");
// break;
// case MotionEvent.ACTION_UP:
// DebugTool.log("MyTextView--->onTouchEvent--->UP");
// break;
// }
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
DebugTool.log("MyTextView--->onFling");
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;
}
}
package com.a2bgeek.gesturedemo2;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView mTextView;
private GestureOverlayView mGestureOverlayView;
private GestureLibrary mGestureLibrary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
mTextView = (TextView) findViewById(R.id.tv1);
mGestureLibrary = GestureLibraries.fromRawResource(
getApplicationContext(), R.raw.gestures);
if (mGestureLibrary.load()) {
mGestureOverlayView = (GestureOverlayView) findViewById(R.id.gestures);
mGestureOverlayView
.addOnGesturePerformedListener(new MyGesturePerformListener());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class MyGesturePerformListener implements
OnGesturePerformedListener {
@Override
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
// TODO Auto-generated method stub
ArrayList list = mGestureLibrary.recognize(gesture);
if (list.size() > 0) {
//list是mGestureLibrary中可能与gesture匹配的手势集,匹配的程度会有一个score评分。
StringBuilder sb = new StringBuilder();
for (Prediction prediction : list) {
sb.append(prediction.name);
sb.append(":");
sb.append(prediction.score);
sb.append("\n");
}
mTextView.setText(sb.toString());
} else {
Toast.makeText(getApplicationContext(), "没有匹配",
Toast.LENGTH_SHORT).show();
}
}
}
}