如何在android应用程序中实现手势支持?
尝试在我的应用程序中实现左右手势 以下是我的布局:如何在android应用程序中实现手势支持?,android,gesture,Android,Gesture,尝试在我的应用程序中实现左右手势 以下是我的布局: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="10sp">
<TextView android:id="@+id/movie_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="text"
android:paddingTop="8sp"
android:paddingBottom="16sp"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<ImageView android:id="@+id/movie_poster"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
<TextView android:text=""
android:id="@+id/movie_description"
android:layout_height="wrap_content"
android:textSize="16sp"
android:paddingTop="16sp"
android:paddingBottom="16sp"
android:layout_width="fill_parent"/>
<Button android:id="@+id/confirm"
android:text="@string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.gesture.GestureOverlayView
android:id="@+id/movie_gestures"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0" />
</LinearLayout>
</ScrollView>
:
公共类MovieView将活动实现扩展到EsturePerformedListener{
私人图书馆;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
...
//手势
mLibrary=GestureLibraries.fromRawResource(this,R.raw.spells);
如果(!mLibrary.load()){
完成();
}
GestureOverlayView手势=(GestureOverlayView)findViewById(R.id.movie_手势);
手势。添加感知性能监听器(此);
}
已执行的检测的公共无效(手势覆盖视图覆盖、手势){
ArrayList predictions=mLibrary.recognize(手势);
if(predictions.size()>0&&predictions.get(0).score>1.0){
String action=predictions.get(0).name;
Toast.makeText(this,predictions.get(0).name,Toast.LENGTH_SHORT.show();
如果(“左”。等于(动作)){
Log.i(“,”左“);
}
否则如果(“正确”。等于(动作)){
Log.i(“,”右“);
}
}
}
但从未调用OnTesturePerformed。我使用了onFling
替代。与ScrollView
配合得很好我不确定,但我想知道使用ScrollView是否会让人困惑,因为它会有自己的“手势”处理来允许滚动。请仅将您的布局作为线性布局尝试(摆脱ScrollView)看看这是否有帮助。@MisterSquonk,我试着用LinearLayout替换ScrollView,但没用。
public class MovieView extends Activity implements OnGesturePerformedListener {
private GestureLibrary mLibrary;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// gestures
mLibrary = GestureLibraries.fromRawResource(this, R.raw.spells);
if (!mLibrary.load()) {
finish();
}
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.movie_gestures);
gestures.addOnGesturePerformedListener(this);
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
String action = predictions.get(0).name;
Toast.makeText(this, predictions.get(0).name, Toast.LENGTH_SHORT).show();
if ("left".equals(action)) {
Log.i("", "left");
}
else if ("right".equals(action)) {
Log.i("", "right");
}
}
}