Android:UI控件GestureOverlayView、gesture、手势
程序员文章站
2024-03-24 14:52:04
...
XML代码:
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gestureOverlayView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="CheckBox" />
</RelativeLayout>
</android.gesture.GestureOverlayView>
JAVA代码:
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.CheckBox;
public class MainActivity extends Activity
{
/**
1) 定义手势
导入并运行GestureBuilder
导出mnt/sdcard/gestures
2) 识别手势
a) gestures文件拷贝到res\raw\
b) layout 新增GestureOverlayView,用来做布局
c) 加载手势库
final GestureLibrary library = GestureLibraries.fromRawResource(this, R.raw.gestures);
library.load();
d) 识别手势
GestureOverlayView OverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
OnGesturePerformedListener listener = new OnGesturePerformedListener()
{
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture)
{
ArrayList<Prediction> recognizeList = library.recognize(gesture);
Prediction prediction = recognizeList.get(0);
if(prediction.score >= 5)//相似度score取值范围0-10
{
if(prediction.equals("check"))
{
checkBox.setChecked(true);
}
}
}
};
OverlayView.addOnGesturePerformedListener(listener);
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
//获得手势库
final GestureLibrary library = GestureLibraries.fromRawResource(this, R.raw.gestures);
library.load();//导入库
//找到和监听手写view
GestureOverlayView OverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
OnGesturePerformedListener listener = new OnGesturePerformedListener()
{
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture)
{
ArrayList<Prediction> recognizeList = library.recognize(gesture);
Prediction prediction = recognizeList.get(0);
if(prediction.score >= 5)//相似度score取值范围0-10
{
if(prediction.name.equals("check"))//判断name是否相同
{
checkBox.setChecked(true);
}
}
}
};
OverlayView.addOnGesturePerformedListener(listener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
转载于:https://blog.51cto.com/glblong/1228590
上一篇: AnimationDrawable
推荐阅读
-
Android:UI控件GestureOverlayView、gesture、手势
-
【Android】11.0 UI开发(二)——列表控件ListView的简单实现1
-
利用Android的Gesture手势识别花样侧滑 博客分类: Android初步
-
Android UI设计之AlertDialog弹窗控件
-
Android UI组件AppWidget控件入门详解
-
Android UI控件ExpandableListView基本用法详解
-
Android UI设计之AlertDialog弹窗控件
-
Android实现Gesture手势识别用法分析
-
Android UI控件ExpandableListView基本用法详解
-
Android自定义控件实现手势密码