以编程方式添加新手势

时间:2015-03-30 00:32:09

标签: android gesture

我想让我的应用的用户为特定事件创建新的手势。因此,用户应能够创建保存到文件的新手势。当用户绘制手势时,应执行特定事件,之前已声明。

我尝试使用此代码:

static File mStoreFile = new File ( Environment.getExternalStorageDirectory (), "gestures" );
@Override
protected void onCreate ( Bundle savedInstanceState ) {
    super.onCreate ( savedInstanceState );
    setContentView ( R.layout.neuegeste );

    name = this.getIntent ().getExtras ().getString ( "applikationsname" );
    Log.d("neueGeste", "neueGeste " + name);

   GestureOverlayView gestureView = ( GestureOverlayView ) findViewById ( R.id.
            gestureLayout );

    gestureView.addOnGestureListener ( new GesturesProcessor () );
}

GesturesProcessor():

private class GesturesProcessor implements GestureOverlayView.OnGestureListener {

    @Override
    public void onGestureEnded ( GestureOverlayView overlay, MotionEvent event ) {

        String text;
        gesture = overlay.getGesture ();
        if ( gesture.getLength () < LENGTH_THRESHOLD ) {

            text = "Geste war zu kurz";
            Dialog_Anzeigen2 ( text );
            overlay.clear ( false );

        }

        text = "Geste übernehmen?";
        Dialog_Anzeigen(text);

    }

    @Override
    public void onGestureCancelled ( GestureOverlayView overlay, MotionEvent event ) {

    }

这用于将新手势存储在文件中:

 public void confirm() {
        if (gesture != null) {

            Log.d ( "hier", "hier " );
            GestureLibrary store = GestureLibraries.fromFile ( mStoreFile );

            store.addGesture(name, gesture);
            store.save();

            Set<String> entries = store.getGestureEntries();
            Log.d ( "entries1.size ()", "entries1.size() " + entries.size () );

            setResult ( RESULT_OK );

        } else {
            setResult(RESULT_CANCELED);
        }
    }

}

创建文件没有问题,我也认为新手势保存到文件(文件的文件大小更改),但当我尝试确定文件中保存的手势量时,结果将为0或1 。

失败的位置或如何正确检查保存手势的数量?

我还想绘制已保存的手势,以便用户可以显示创建的手势。有没有可能从文件的所有创建手势中获取Drawables,将它们添加到ImageView?

非常感谢您的帮助。

0 个答案:

没有答案