Jamendo開源線上音樂播放器源碼分析(二)

來源:互聯網
上載者:User

三、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的基本步驟:
[java]
//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完就要perform,即識別這個手勢並賦予它一定的功能,播放、暫停等。
基本步驟:
[java] 
//跟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裡的,結構非常清晰。
設定檔:
[html]
<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> 
可以在設定裡禁用手勢功能
[java] 
boolean gesturesEnabled = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("gestures", true); 
mGestureOverlayView.setEnabled(gesturesEnabled); 
下篇繼續分析HomeActivity。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.