The use of Viewflipper in Android and the setting of animation effect example detailed _android

Source: Internet
Author: User

This article illustrates the use and animation effects of Viewflipper in Android. Share to everyone for your reference, specific as follows:

Talking about sliding, in fact, the way to achieve a lot of sliding around, there are viewpaer, custom implementation viewgroup,gallery can achieve this effect. Here do the next viewfliper to achieve the effect of sliding left and right.
The following techniques are used:

1, Viewflipper
2, Gesturedetector
3, Animation

The main is that these three classes at work.

Viewflipper, think of it as a container, you can put a lot of view in this container, let it show to the user, although it only shows one view at a time, I feel that its use is better as an advertising display, such as a shopping site like the display of advertising scrolling. For example, you can set the automatic playback function, you can scroll ads!

Principle: Sliding left to right is mainly dependent on gestures to control, gestures to the right to call the Viewflipper.shownext () method, the same, sliding left will call the Viewflipper.showprevious () method.

The effect chart is as follows:

Next, go directly to the code:

First, add the Viewflipper label to the layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:background=" @drawable/pa Ttern1 "> <button android:id=" @+id/treasure_chest_btn "android:layout_width=" Wrap_content "Android : layout_height= "Wrap_content" android:layout_alignparentright= "true" android:background= "@drawable/treasure"/&G 
  T <viewflipper android:id= "@+id/viewflipper" android:layout_width= fill_parent "android:layout_height=" Wra P_content "android:layout_below=" @id/treasure_chest_btn "> </ViewFlipper> <button android:id=" @+id/garbage_btn "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_a" Lignparentleft= "true" android:layout_below= "@id/viewflipper" android:background= "@drawable/garbage"/> </

 Relativelayout>

The second is to define animations in Anim:

Slide_left_in.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android > 
  <!-- 
   picture conversion position move animation effect  
   start left animation 
   Fromxdelta: X coordinate position when animation begins 
   Toxdelta: x coordinates 
   at end of animation Duration: Animation duration 
  --> 
  <translate 
    android:duration= "i" 
    android:fromxdelta= "100%p" 
    Android:toxdelta= "0"/> 
</set>

Slide_left_out.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  <translate 
    android:duration=" 
    android:fromxdelta= "0" 
    android:toxdelta= " -100%p"/> 
</set>

Slide_right_in.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  <translate 
    android:duration=" 
    android:fromxdelta= " -100%p" 
    Android:toxdelta = "0"/> 
</set>

Slide_right_out.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  <translate 
    android:duration=" 
    android:fromxdelta= "0" 
    android:toxdelta= "100%p"/> 
</set>

Finally, the implementation of the activity:

Package net.loonggg.demo.slider; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.view.GestureDetector; 
Import Android.view.GestureDetector.OnGestureListener; 
Import Android.view.LayoutInflater; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import Android.view.View.OnTouchListener; 
Import Android.view.Window; 
Import android.view.animation.Animation; 
Import Android.view.animation.Animation.AnimationListener; 
Import Android.view.animation.AnimationSet; 
Import Android.view.animation.AnimationUtils; 
Import Android.widget.Button; 
Import Android.widget.ImageView; 
Import Android.widget.TextView; 
Import Android.widget.ViewFlipper;  public class Mainactivity extends activity implements Ongesturelistener, Ontouchlistener {private gesturedetector 
  detector; 
  Private Viewflipper Myviewflipper; Private int[] IMGs = {r.drawable.img0, r.drawable.img1, R.drawable.img2, R.DRAWABLE.IMG3, R.DRAWABLE.IMG4, R.drawa 
  BLE.IMG5}; Private int displayedchildindex = 0; 
  Private Button treasure_btn; 
  Private Button garbage_btn; 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Requestwindowfeature (Window.feature_no_title); 
    Setcontentview (R.layout.main); 
    Myviewflipper = (viewflipper) Findviewbyid (r.id.viewflipper); 
    TREASURE_BTN = (Button) Findviewbyid (R.ID.TREASURE_CHEST_BTN); 
    GARBAGE_BTN = (Button) Findviewbyid (R.ID.GARBAGE_BTN); 
    Myviewflipper.setontouchlistener (this); Myviewflipper.setlongclickable (TRUE);/set long by Event//Myviewflipper.setautostart (TRUE),//Set whether to play automatically, default does not play automatically detector 
    = new Gesturedetector (this); 
  Addflipperview (); /** * Dynamically add view to Flipperview */private void Addflipperview () {for (int i = 0; i < imgs.length; i+ 
      +) {View view = Layoutinflater.from (this). Inflate (r.layout.myviewflipper, NULL); TextView title = (TextView) View.findviewbyid (r.id.view_tiTLE); 
      Title.settext ("Avatar"); 
      ImageView IV = (ImageView) View.findviewbyid (r.id.view_image); 
      Iv.setbackgroundresource (Imgs[i]); 
    Myviewflipper.addview (view); 
  @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, Mot Ionevent E2, float Velocityx, float velocityy) {//returns the index of the child view currently being displayed Displayedchildindex = MYVIEWFLIPPER.G 
    Etdisplayedchild (); 
    Final ImageView IV = (ImageView) myviewflipper.getchildat (Displayedchildindex). Findviewbyid (R.id.view_image); if (E2.getx ()-E1.getx () >= && E1.getY ()-e2.gety () >=) {Animationset Outanim = (animationset) animationutils.loadanimation (th 
      is, r.anim.collect_treasure); 
      Iv.startanimation (Outanim); 
        Outanim.setanimationlistener (New Animationlistener () {public void Onanimationstart (Animation Animation) { The public void Onanimationrepeat (Animation Animation) {} is public void Onanimationend (animatio 
          n animation) {Shownextview ();  
          Animationset Downanim = (animationset) animationutils. Loadanimation (Mainactivity.this, R.anim.up_or_down); 
          Treasure_btn.setanimation (Downanim); 
        Iv.clearanimation (); 
    } 
      }); else if (E1.getx ()-E2.getx () >= && e2.gety ()-e1.gety () >=) {Animationset = (an 
      Imationset) animationutils.loadanimation (this, r.anim.throw_garbage); 
      Iv.startanimation (Outanim); Outanim.setanimationlistener (NewAnimationlistener () {public void Onanimationstart (Animation Animation) {} public void Onanima Tionrepeat (Animation Animation) {} public void Onanimationend (Animation Animation) {Shownex 
          Tview (); 
                  Animationset Leftanim = (animationset) animationutils. Loadanimation (Mainactivity.this, 
          R.anim.left_or_right); 
          Garbage_btn.setanimation (Leftanim); 
        Iv.clearanimation (); 
    } 
      }); 
    else if (E1.getx ()-E2.getx () >=) {Shownextview (); 
    else if (E2.getx ()-E1.getx () >=) {Showpreviousview (); 
  return false; } private void Showpreviousview () {myviewflipper.setinanimation animationutils.loadanimation (this, R.ani 
    m.slide_right_in)); 
    Myviewflipper.setoutanimation (Animationutils.loadanimation (this, r.anim.slide_right_out)); 
  Myviewflipper.showprevious (); } private void ShowneXtview () {myviewflipper.setinanimation (Animationutils.loadanimation (this, r.anim.slide_left_in)); 
    Myviewflipper.setoutanimation (Animationutils.loadanimation (this, r.anim.slide_left_out)); 
  Myviewflipper.shownext (); 
  @Override public boolean Ontouch (View v., motionevent event) {return detector.ontouchevent (event); } 
}

I hope this article will help you with your Android programming.

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.