Sliding page flipping in Android-using Viewflipper (convert between DP and PX)

Source: Internet
Author: User

Enable sliding page flipping in Android-convert between DP and PX using VIEWFLIPPER (convert between DP and PX) in an XML layout file, we can either set the PX or set the DP (or DIP). In general, we will choose to use DP, which will ensure that the layout of different screen resolutions on the machine is consistent. But in the code, how to deal with it? Many of the methods in the control provide only a method for setting PX, such as setpadding, and do not provide a way to set the DP. At this point, if you need to set the DP, it is necessary to convert the DP into PX.    The following is an application class that facilitates the conversion between PX and DP.            Import Android.content.Context;  Public classDensityutil {/** * According to the phone's resolution from the DP unit to the PX (pixel)*/           Public Static intDIP2PX (Context context,floatdpvalue) {FinalfloatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Dpvalue * scale +0.5f); }                /** * According to the resolution of the phone from PX (pixel) to the unit to become DP*/           Public Static intPx2dip (Context context,floatpxvalue) {FinalfloatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Pxvalue/scale +0.5f);            }} import Android.content.Context;  Public classDensityutil {/** * According to the phone's resolution from the DP unit to the PX (pixel)*/           Public Static intDIP2PX (Context context,floatdpvalue) {FinalfloatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Dpvalue * scale +0.5f); }                /** * According to the resolution of the phone from PX (pixel) to the unit to become DP*/           Public Static intPx2dip (Context context,floatpxvalue) {FinalfloatScale =context.getresources (). Getdisplaymetrics (). density; return(int) (Pxvalue/scale +0.5f); }      }       Public classViewflipperactivity extends Activity implements Ongesturelistener {Private StaticFinalintFling_min_distance = -; PrivateViewflipper Flipper; PrivateGesturedetector Detector; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);              Setcontentview (R.layout.viewflipper); //Register a Gesturedetectordetector =NewGesturedetector ( This); Flipper=(Viewflipper) Findviewbyid (r.id.viewflipper); ImageView Image1=NewImageView ( This);              Image1.setbackgroundresource (R.DRAWABLE.IMAGE1); //Add first ViewFlipper.addview (Image1); ImageView Image2=NewImageView ( This);              Image2.setbackgroundresource (R.drawable.image2); //Add a second viewFlipper.addview (Image2); } @Override PublicBoolean ontouchevent (motioneventEvent) {              //Hand- over touch-screen events to gesture Recognition class processing            return  This. Detector.ontouchevent (Event); } @Override PublicBoolean Ondown (Motionevent e) {return false; } @Override Public voidonshowpress (Motionevent e) {} @Override PublicBoolean onsingletapup (Motionevent e) {return false; } @Override PublicBoolean onscroll (Motionevent E1, motionevent E2,floatDistancex,floatDistancey) {              return false; } @Override Public voidonlongpress (Motionevent e) {} @Override PublicBoolean onfling (Motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {              if(E1.getx ()-E2.getx () >fling_min_distance) {                  //set the animation effect for view entry 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 Iamgeview (used to display the image) were created and added to the Viewflipper. When the program runs, when you swipe left on the screen with your finger, the previous picture is displayed and the next picture is displayed with your finger on the screen to the right. The main code to implement the sliding switch is in the Onfling () method, where the user presses the touchscreen and then releases it quickly, triggering the event. In this code example, the distance of the finger slide is calculated, if the sliding distance is greater than 100 pixels, do a toggle action, otherwise do not do any switching action. As can be seen, the onfling () method has four parameters, E1 and E2 above code used, better understanding. What are the parameters Velocityx and velocityy used for? Velocityx and Velocityy are actually moving speeds on the x and Y axes, in pixels/seconds. Combined with these two parameters, the speed of the slide can be judged, thus doing more processing. To show the effect of sliding, the Viewflipper's Setinanimation () and Setoutanimation () methods are called to set the animation of the view entry and exit. For the use of animation, here no longer repeat, and no longer give specific XML file code. Also, talk about additional topics based on the above code. In the XML layout file, we can either set the pixel PX or set the DP (or DIP). In general, we will choose to use DP, so that the different screen resolution of the phone on the same layout. However, in code, it is generally not possible to use DP directly. Take the code above for example, the code defines a sliding distance threshold of 100 pixels. This results in different resolutions on the phone. For example, in the 240x320 model, and on the 480x800 model, want to switch the view, the finger sliding distance is different. Therefore, in general, it is recommended in the code, do not use pixels, also use DP. Now that you can't use DP directly, you need to convert from PX to DP. 

Sliding page flipping in Android-using Viewflipper (convert between DP and PX)

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.