三、HomeActivity
HomeActivity是一个central navigation place,中央导航activity,各个子模块的跳转都要先回到这,其他activity切换到该activity都要通过调用其静态方法launch()。
布局文件时main.xml。整体分为上面的ViewFlipper和下面的ListView,ListView是套在GestureOverlayView下的。分几部分介绍。
1、ViewFlipper,其实就是一个FrameLayout,可以通过setDisplayedChild(int whichchild)或者showNext()等显示其中的子布局,深入的用法比如加入动画可以自己去搜。
2、手势
com.teleca.jamendo.gestures 包是一些手势相关的处理代码。
类图:
gesture的create与perform
这儿的gesture是制作好放到raw目录下的,如何create一个gesture呢,在SDK的demos里有一个GestureBuilder的demo就教你如何create gesture。
create一个gesture的基本步骤:
//sd卡目录下new一个名为gestures的文件 File mStoreFile = new File(Environment.getExternalStorageDirectory(), "gestures"); //文件中读取 GestureLibrary sStore= GestureLibraries.fromFile(mStoreFile); //在这个overlayView上画手势并设置监听 GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay); overlay.addOnGestureListener(new GesturesProcessor()); private Gesture mGesture; private class GesturesProcessor implements GestureOverlayView.OnGestureListener { public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) { mGesture = null; } public void onGesture(GestureOverlayView overlay, MotionEvent event) { } public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) { //画完得到手势,如果长度过短则清除 mGesture = overlay.getGesture(); if (mGesture.getLength() < LENGTH_THRESHOLD) { overlay.clear(false); } } public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) { } } public void addGesture(View v) { if (mGesture != null) { final TextView input = (TextView) findViewById(R.id.gesture_name); final CharSequence name = input.getText(); if (name.length() == 0) { input.setError(getString(R.string.error_missing_name)); return; } //用一个名字保存gesture sStore.addGesture(name.toString(), mGesture); sStore.save(); } }你可以把文件移到raw目录下。
//跟create时监听不同 mGestureOverlayView.addOnGesturePerformedListener(JamendoApplication .getInstance().getPlayerGestureHandler()); //从raw目录下读取 GestureLibrary mLibrary= GestureLibraries.fromRawResource(context, R.raw.gestures); mLoaded = mLibrary.load(); //覆写的方法 @Override public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) { if (!mLoaded) { if (!load()) { return; } } //识别 ArrayList<Prediction> predictions = mLibrary.recognize(gesture); if (predictions.size() > 0) { Prediction prediction = predictions.get(0); Log.v(JamendoApplication.TAG, "Gesture " + prediction.name + " recognized with score " + prediction.score); if (prediction.score > 2.0) { //分数越高越精确,执行相应的功能 GestureCommand command = getRegister().getCommand( prediction.name); if (command != null) { command.execute(); } } } }具体的执行交给具体的GestureCommand。每个GestureCommand都是放在GestureCommandRegister里的,结构非常清晰。
配置文件:
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gestures" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gestureStrokeType="multiple" android:eventsInterceptionEnabled="false" android:orientation="vertical"> </android.gesture.GestureOverlayView>可以在设置里禁用手势功能
boolean gesturesEnabled = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("gestures", true); mGestureOverlayView.setEnabled(gesturesEnabled);下篇继续分析HomeActivity。