In some scenarios, We need to display a series of pages to users. For example, if we are developing a cartoon watching application, we may need to display a cartoon image to users. Users can use their fingers to slide the screen and switch between the previous cartoon and the next cartoon. In this case, viewflipper is a good choice.
1) view switching control-Overview of viewflipper
The viewfilpper class inherits from the viewanimator class. The viewanimator class inherits from framelayout.
View the source code of the viewanimator class. We can see that this class is mainly used to provide animation effects for the view switching. This class has the following animation-related methods.
Setinanimation: Set the animation used when the view enters the screen. This method has two overload Methods: You can directly input the animation object or the resourceid of the specified animation file.
Setoutanimation: Specifies the animation used when the view exits the screen. Use the same method as setinanimation.
Shownext: Call this method to display the next view in framelayout.
Showprevious: Call this method to display the previous view in framelayout.
View the source code of viewflipper. viewflipper is mainly used to automatically switch views. This class provides the following main methods.
Setfilpinterval: Set the view switching interval. The parameter is in milliseconds.
Startflipping: Start view switching. The interval is the number of intervals set in the preceding method. The switchover will be performed cyclically.
Stopflipping: Stop view switching.
Setautostart: Set whether to start automatically. If it is set to "true", the view switch starts automatically when viewflipper is displayed.
In general, we use the viewfilpper class to implement view switching without using its parent class viewanimator.
2) Implement slide-gesturedetector Introduction
If you want to achieve the sliding flip effect, you need to know another class: Android. View. gesturedetector class. The gesturedetector class can be used to detect various gesture events. This class has two callback interfaces to notify specific events respectively.
Gesturedetector. ondoubletaplistener: Used to notify doubletap events, similar to double-click events on a PC.
Gesturedetector. ongesturelistener: Used to notify normal gesture events. This interface has six callback methods. For details, see the API. Here, the onfling () method is used to determine the sliding.
3) specific implementation
The following code snippet details how to slide pages:
Public class viewflipperactivity extends activity implements ongesturelistener {
Private Static final int fling_min_distance = 100;
Private viewflipper Flipper;
Private gesturedetector detector;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. viewflipper );
// Register a gesturedetector
Detector = new gesturedetector (this );
Flipper = (viewflipper) findviewbyid (R. Id. viewflipper );
Imageview image1 = new imageview (this );
Image1.setbackgroundresource (R. drawable. image1 );
// Add the first view
Flipper. addview (image1 );
Imageview image2 = new imageview (this );
Image2.setbackgroundresource (R. drawable. image2 );
// Add the second view
Flipper. addview (image2 );
}
@ Override
Public Boolean ontouchevent (motionevent event ){
// Send the touch screen event to the gesture recognition class for processing
Return this. detector. ontouchevent (event );
}
@ Override
Public Boolean ondown (motionevent e ){
Return false;
}
@ Override
Public void onshowpress (motionevent e ){
}
@ Override
Public Boolean onsingletapup (motionevent e ){
Return false;
}
@ Override
Public Boolean onscroll (motionevent E1, motionevent E2, float distancex,
Float distancey ){
Return false;
}
@ Override
Public void onlongpress (motionevent e ){
}
@ Override
Public Boolean onfling (motionevent E1, motionevent E2, float velocityx,
Float velocityy ){
If (e1.getx ()-e2.getx ()> fling_min_distance ){
// Set the animation effect for the view to enter and exit
This. Flipper. setinanimation (animationutils. loadanimation (this,
R. anim. left_in ));
This. Flipper. setoutanimation (animationutils. loadanimation (this,
R. anim. left_out ));
This. Flipper. shownext ();
Return true;
}
If (e1.getx ()-e2.getx () <-fling_min_distance ){
This. Flipper. setinanimation (animationutils. loadanimation (this,
R. anim. right_in ));
This. Flipper. setoutanimation (animationutils. loadanimation (this,
R. anim. right_out ));
This. Flipper. showprevious ();
Return true;
}
Return false;
}
}
In this Code, two iamgeviews (used to display images) are created and added to viewflipper. After the program runs, When you slide to the left with your fingers on the screen, the previous image is displayed. When you slide to the right with your fingers on the screen, the next image is displayed. The main code for sliding switching is in the onfling () method. When you press the touch screen and move it quickly, the event is triggered. In this code example, the sliding distance of the finger is calculated. If the sliding distance is greater than 100 pixels, the switchover is performed. Otherwise, no switchover is performed.
As you can see, the onfling () method has four parameters. The code above E1 and E2 is used for better understanding. What are the functions of the velocityx and velocityy parameters? Velocityx and velocityy are actually moving speeds on the X and Y axes, measured in pixels/s. Based on these two parameters, you can determine the sliding speed for more processing.
To display the sliding effect, the setinanimation () and setoutanimation () Methods of viewflipper are called to set the animation of the view entry and exit. We will not go into details about the use of animations or the specific XML file code here.
In addition, add additional topics based on the above Code.
In the XML layout file, we can set both pixel PX and dp (or DIP ).
In general, we will choose to use DP to ensure consistent layout on mobile phones with different screen resolutions. However, in the Code, DP cannot be directly used.
Take the above Code as an example. The Code defines that the sliding distance threshold is 100 pixels. This will lead to different effects on mobile phones with different resolutions. For example, on the X model and on the X model, you need to slide your fingers to switch the view. Therefore, we recommend that you do not use pixels or DP in the code.
Since DP cannot be used directly, you need to convert from Px to DP. In fact, there are Formulas Between PX and DP that can be converted to each other. In front of my blog (http://blog.csdn.net/arui319/article/details/6777133) has been written, you can directly refer.