您的位置:首页 > 移动开发 > Android开发

Android手势操作

2013-07-18 18:51 204 查看
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.gesture.GestureOverlayView
android:id="@+id/gestureOverlayView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gestureStrokeType="multiple" >
</android.gesture.GestureOverlayView>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/Button"
android:layout_weight="0"
android:text="识别"/>
</LinearLayout>

注意:
android_weight=0 表示是首先计算该控件的的高度或宽度,然后计算android_weight="1"控件的 高度,一般他是和 android:layout_height="0dp"结合使用的,否则是不会出来效果

public class MainActivity extends Activity {
private GestureLibrary library;
private Gesture gestures;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GestureOverlayView gesture=(GestureOverlayView) this.findViewById(R.id.gestureOverlayView1);
library=GestureLibraries.fromRawResource(this, R.raw.gestures);//得到手势文件的类文件
library.load();//加载手势库

/**
* 为手势添加事件监听
*/
gesture.addOnGestureListener(new OnGestureListener(){
public void onGesture(GestureOverlayView overlay, MotionEvent event)
{
// TODO Auto-generated method stub
}
@Override
public void onGestureCancelled(GestureOverlayView overlay,MotionEvent event)
{
// TODO Auto-generated method stub
}
@Override
public void onGestureEnded(GestureOverlayView overlay,MotionEvent event)
{
gestures=overlay.getGesture();//得到一个手势
}
@Override
public void onGestureStarted(GestureOverlayView overlay,MotionEvent event)
{
// TODO Auto-generated method stub
}
});

this.findViewById(R.id.Button).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
find(gestures);
}
});
}

public void find(Gesture gesture)
{
ArrayList<Prediction> predictions=library.recognize(gesture);//得到手势库中的一个集合
if(predictions!=null)
{
Prediction prediction= predictions.get(0);//得到一个最匹配的一项,手势匹配默认会把最匹配的一项放在最前面
/**
* 判断匹配的度数是否大于60%,prediction.score的值在大于0小于10的,如果是4的话就是匹配值是40%的意思
*/
if(prediction.score>5)
{
if("close".equals(prediction.name))
{
finish();//此方法只能那个关闭Activity,并不能杀死进程,所以要在OnDestory()方法中杀死进程
}
else if("call".equals(prediction.name))
{
Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:15824935047"));
startActivity(intent);
}
}
else
{
Toast.makeText(this, "匹配值太低", Toast.LENGTH_LONG).show();
}
}else
{
Toast.makeText(this, "匹配不成功", Toast.LENGTH_LONG).show();
}
}

@Override
protected void onDestroy() {
Process.killProcess(Process.myPid());//杀死当前进程
super.onDestroy();
}

@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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: