Source code analysis of Jamendo open-source online music player (2)

Source: Internet
Author: User

Iii. HomeActivity

HomeActivity is a central navigation place with a central navigation activity. The jump of each sub-module must first return to this position. Other activities must call their static method launch () to switch to this activity ().
Main. xml when layout files. The overall structure is divided into ViewFlipper and ListView. ListView is set under GestureOverlayView. It is described in several parts.
1. ViewFlipper is actually a FrameLayout. You can use setDisplayedChild (int whichchild) or showNext () to display the sub-layout. For in-depth usage, such as adding an animation, you can search by yourself.
2. gestures
The com. teleca. jamendo. gestures package contains some gesture-related processing code.
Class diagram:

Create and perform of gesture
Here, gesture is created and put in the raw directory. How to create a gesture? There is a GestureBuilder demo in SDK demos to teach you how to create gesture.
Follow these steps to create a gesture:
[Java]
// A new file named gestures under the SD card directory
File mStoreFile = new File (Environment. getExternalStorageDirectory (), "gestures ");
// Read from the file
GestureLibrary sStore = GestureLibraries. fromFile (mStoreFile );
// Draw a gesture on the overlayView and set the listener
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 ){
// Get the gesture after painting. Clear the gesture if the length is too short.
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;
}
// Save gesture with a name
SStore. addGesture (name. toString (), mGesture );
SStore. save ();
}
}
You can move the file to the raw directory.
Perform is required after the create operation, that is, to recognize this gesture and grant it certain functions, such as playing and pausing.
Basic steps:
[Java]
// The Listener is different from that for create.
MGestureOverlayView. addongesturew.medlistener (JamendoApplication
. GetInstance (). getPlayerGestureHandler ());
// Read from raw directory
GestureLibrary mLibrary = GestureLibraries. fromRawResource (context, R. raw. gestures );
MLoaded = mLibrary. load ();
// Override method
@ Override
Public void ongesturesponmed (GestureOverlayView overlay, Gesture gesture ){
If (! MLoaded ){
If (! Load ()){
Return;
}
}
// Recognition
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 ){
// The higher the score, the more accurate the score is.
GestureCommand command = getRegister (). getCommand (
Prediction. name );
If (command! = Null ){
Command.exe cute ();
}
}
}
}
The specific execution is handed over to the specific GestureCommand. Each GestureCommand is placed in GestureCommandRegister with a clear structure.
Configuration file:
[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>
You can disable the gesture function in settings.
[Java]
Boolean gesturesEnabled = PreferenceManager. getdefasharsharedpreferences (this). getBoolean ("gestures", true );
MGestureOverlayView. setEnabled (gesturesEnabled );
Next, continue to analyze HomeActivity.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.