Android提供了手势检测,并为手势检测提供了相应监听器
Android允许开发者添加手势,并提供了相应的API识别用户手势
手势检测
GestureDetector类即用来实现手势检测,GestureDetector创建时需要传入一个GestureDetector.OnGestureListener实例,GestureDetector.OnGestureListener就是一个监听器、负责对用户的手势行为提供响应,包含的处理方法有:
boolean onDown(MotionEvent e)
|
当触碰事件按下时触发该方法
|
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
|
当用户在触摸屏上“拖过”时触发该方法。velocity代表速度
|
abstract void onLongPress(MotionEvent e)
|
长按触发
|
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
|
滚动时触发
|
void onShowPress(MotionEvent e)
|
按下、而且还未移动和松开时触发
|
boolean onSingleTapUp(MotionEvent e)
|
轻击事件触发
|
使用Android的手势检测只需两个步骤
- 创建一个GestureDetector对象(必须实现GestureDetector.OnGestureListener)监听器实例
- 为应用程序的Activity的TouchEvent事件绑定监听器,在事件处理中指定把Activity上的TouchEvent事件交给GestureDetector处理
示例代码
GestureTest.java
public
class
GestureTest
extends ActionBarActivity
implements OnGestureListener{
GestureDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this, this);
}
@Override
public boolean onTouchEvent(MotionEvent me)
{
return detector.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent arg0)
{
Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2
, float velocityX, float velocityY)
{
Toast.makeText(this, "onFling", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onLongPress(MotionEvent e)
{
Toast.makeText(this, "onLongPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
Toast.makeText(this, "onScroll", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onShowPress(MotionEvent e)
{
Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return false;
implements OnGestureListener{
GestureDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this, this);
}
@Override
public boolean onTouchEvent(MotionEvent me)
{
return detector.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent arg0)
{
Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2
, float velocityX, float velocityY)
{
Toast.makeText(this, "onFling", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onLongPress(MotionEvent e)
{
Toast.makeText(this, "onLongPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
Toast.makeText(this, "onScroll", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onShowPress(MotionEvent e)
{
Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return false;
}
}
另一个示例,通过手势实现图片的缩放,代码如下:
GestureZoom.java
public
class
GestureZoom
extends ActionBarActivity
implements OnGestureListener{
GestureDetector detector;
ImageView imageView;
Bitmap bitmap;
int width,height;
float currentScale = 1;
Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this, this);
imageView = (ImageView) findViewById(R.id.show);
matrix = new Matrix();
bitmap = BitmapFactory.decodeResource(this.getResources()
, R.drawable.flower);
}
@Override
public boolean onTouchEvent(MotionEvent me)
{
return detector.onTouchEvent(me);
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2
,float velocityX, float velocityY)
{
velocityX = velocityX > 4000 ? 4000: velocityX;
velocityX = velocityX < -4000 ? -4000: velocityX;
currentScale += currentScale * velocityX / 4000.0f;
currentScale = currentScale > 0.01 ? currentScale: 0.01f;
matrix.reset();
matrix.setScale(currentScale, currentScale , 160, 200);
BitmapDrawable tmp = (BitmapDrawable)
imageView.getDrawable();
if(!tmp.getBitmap().isRecycled())
{
tmp.getBitmap().recycle();
}
Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0 ,width, height, matrix, true);
imageView.setImageBitmap(bitmap2);
return true;
}
@Override
public boolean onDown(MotionEvent arg0)
{
return false;
}
@Override
public void onLongPress(MotionEvent event)
{}
@Override
public boolean onScroll(MotionEvent event1,
MotionEvent event2, float distanceX, float distanceY)
{
return false;
}
@Override
public void onShowPress(MotionEvent event)
{}
@Override
public boolean onSingleTapUp(MotionEvent event)
{
return false;
implements OnGestureListener{
GestureDetector detector;
ImageView imageView;
Bitmap bitmap;
int width,height;
float currentScale = 1;
Matrix matrix;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this, this);
imageView = (ImageView) findViewById(R.id.show);
matrix = new Matrix();
bitmap = BitmapFactory.decodeResource(this.getResources()
, R.drawable.flower);
}
@Override
public boolean onTouchEvent(MotionEvent me)
{
return detector.onTouchEvent(me);
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2
,float velocityX, float velocityY)
{
velocityX = velocityX > 4000 ? 4000: velocityX;
velocityX = velocityX < -4000 ? -4000: velocityX;
currentScale += currentScale * velocityX / 4000.0f;
currentScale = currentScale > 0.01 ? currentScale: 0.01f;
matrix.reset();
matrix.setScale(currentScale, currentScale , 160, 200);
BitmapDrawable tmp = (BitmapDrawable)
imageView.getDrawable();
if(!tmp.getBitmap().isRecycled())
{
tmp.getBitmap().recycle();
}
Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0 ,width, height, matrix, true);
imageView.setImageBitmap(bitmap2);
return true;
}
@Override
public boolean onDown(MotionEvent arg0)
{
return false;
}
@Override
public void onLongPress(MotionEvent event)
{}
@Override
public boolean onScroll(MotionEvent event1,
MotionEvent event2, float distanceX, float distanceY)
{
return false;
}
@Override
public void onShowPress(MotionEvent event)
{}
@Override
public boolean onSingleTapUp(MotionEvent event)
{
return false;
}
}
增加手势
Android除了提供了手势检测之外,还允许应用程序把用户手势(多个持续的触摸事件在屏幕上形成特定的形状)添加到指定文件中,这样,下次用户再画出该手势时系统就可以识别该手势了
GestureLibrary代表手势库,并提供了Libraries工具类来创建手势库,有如下4个静态方法从不同位置加载手势库
static GestureLibrary fromFile(String path)
| 从path代表的文件加载 |
static GestureLibrary fromFile(File path)
|
|
static GestureLibrary fromPrivateFile(Context context, String name)
|
从指定应用程序的数据文件夹中name文件中加载手势库
|
static GestureLibrary fromRawResource(Context context, int resourceId)
|
从resourceId代表的资源中加载
|
获得GestureLibrary对象之后,该对象提供了如下方法来添加、识别手势
void addGesture(String entryName, Gesture gesture)
|
添加一个名位entryName的手势
|
Set<String> getGesture(String entryName)
|
获取entryName名称对应的全部手势
|
ArrayList<Gesture> getGestures(String entryName)
|
获取entryName名称对应的全部手势
|
ArrayList<Prediction>recognize(Gesture gesture)
|
从当前手势库中识别与gesture匹配的全部手势
|
void removeEntry(String entryName)
|
|
void removeGesture(String entryName,Gesture gesture)
| 删除手势库中entryName、gesture对应的手势 |
boolean save()
|
当向手势库中添加手势或从中删除手势后调用该方法保存手势库
|
Android除了提供了GestureLibraries、GestureLibrary来管理手势外,还提供了一个专门的手势编辑组件:GestureOverlayView,该组件就像一个“绘图组件”
自动朗读TTS
TextToSpeech