如何在ImageView上实现自定义手势?

时间:2014-11-06 21:36:21

标签: android imageview gesture

我通过以下"标准"成功实现了活动级别的自定义手势。码。所以整个屏幕都会对#34;做出反应。这个动作。

但是,我希望能够在特定的ImageView上完成自定义手势。有可能吗?

由于

public class DisplayMultiImageActivity extends Activity implements OnGesturePerformedListener {

    GestureLibrary mLibrary;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_multiimage);

        mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (!mLibrary.load()) {
            finish();
        }

        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
        gestures.addOnGesturePerformedListener(this);

    }

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
        if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
            String result = predictions.get(0).name;

            if ("mycustomgesture".equalsIgnoreCase(result)) {
                Toast.makeText(this, "Custom gesture detected!!", Toast.LENGTH_LONG).show();
            }
        }
    }

}

1 个答案:

答案 0 :(得分:2)

在您的XML文件中,GestureOverlayView可能会占据整个屏幕。如果您只想对ImageView上的手势做出反应,则应将GestureOverlayView设置为ImageView

<android.gesture.GestureOverlayView
        android:id="@+id/gestures"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
             // attributes of ImageView />

</android.gesture.GestureOverlayView>

修改

onGesturePerformedListener在方法GestureOverlayView中收到执行了该手势的onGesturePerformed(GestureOverlayView, Gesture)

在此方法中,检查使用了哪个GestureOverlayView,然后执行相关操作。

因此,您应该在要跟踪手势的每个ImageView上设置不同的GestureOverlayView

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
           switch(overlay.getId()) {
           case (R.id.overlay1):
                \\ DO SOMETHING WITH IMAGEVIEW 1
                break;
           case (R.id.overlay2):
                \\ DO SOMETHING WITH IMAGEVIEW 2
                break;
           case (R.id.overlay3):
                \\ DO SOMETHING WITH IMAGEVIEW 3
                break;
           default:
                \\
           }
     }