SurfaceView添加手势

import game.core.GameView;

import java.util.ArrayList;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.Toast;

public class GestureActivity extends Activity implements OnGesturePerformedListener
{
protected GameView surfaceView;
protected GestureOverlayView gestureOverlayView;
protected GestureLibrary mLibrary;
protected FrameLayout frameLayout;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

gestureOverlayView = new GestureOverlayView(this);
surfaceView        = new GameView(this);
frameLayout        = new FrameLayout(this);

//gestureOverlayView.addView(surfaceView);
gestureOverlayView.setOrientation(gestureOverlayView.ORIENTATION_VERTICAL);
gestureOverlayView.setEventsInterceptionEnabled(true);
gestureOverlayView.setGestureStrokeType(gestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);

mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
gestureOverlayView.addOnGesturePerformedListener(this);

frameLayout.addView(surfaceView, 0);
frameLayout.addView(gestureOverlayView,1);

setContentView(frameLayout);
}

@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture)
{
// TODO Auto-generated method stub
ArrayList
predictions = mLibrary.recognize(gesture);

// one prediction needed
if (predictions.size() > 0)
{
Prediction prediction = predictions.get(0);

// checking prediction
if (prediction.score > 1.0)
{
// and action
Toast.makeText(GestureActivity.this, prediction.name,
Toast.LENGTH_SHORT).show();
}
}
}
}本文出处

http://scanplaygames.com/ cat=6

发表评论

电子邮件地址不会被公开。 必填项已用*标注