Two ways to implement image carousel effect on Android _android

Source: Internet
Author: User

In the process of using the app, we often see the effect of the upper banner picture Carousel, so today we'll learn a few ways to do this in android:

The first: The use of animation methods to achieve: (cumbersome code)

This release needs: Two animation effects, a layout, a main class to implement, not to say, look at the code:

The public class Iamgetractivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the './Public created ImageView
Iew;
Public ImageView ImageView2;
Public Animation Animation1;
Public Animation Animation2;
public TextView text;
public Boolean juage = true; public int images[] = new int[] {r.drawable.icon, r.drawable.expriment, R.drawable.changer, R.drawable.dataline, R.drawa
Ble.preffitication};
public int count = 0;
Public Handler Handler = new Handler (); Public Runnable Runnable = new Runnable () {@Override public void run () {//TODO auto-generated the method stub Animationset
AnimationSet1 = new Animationset (true);
Animationset AnimationSet2 = new Animationset (true);
Imageview2.setvisibility (0); Translateanimation ta = new Translateanimation (animation.relative_to_self, 0f, Animation.relative_to_self, -1f,
Animation.relative_to_self, 0f, Animation.relative_to_self, 0f);
Ta.setduration (2000);
Animationset1.addanimation (TA);
Animationset1.setfillafter (TRUE); TA = new TranslateanImation (Animation.relative_to_self, 1.0f, Animation.relative_to_self, 0f, Animation.relative_to_self, 0f,
Animation.relative_to_self, 0f);
Ta.setduration (2000);
Animationset2.addanimation (TA);
Animationset2.setfillafter (TRUE);
Iamgeview out ImageView2 come in imageview.startanimation (ANIMATIONSET1);
Imageview2.startanimation (AnimationSet2);
Imageview.setbackgroundresource (images[count% 5]);
count++;
Imageview2.setbackgroundresource (images[count% 5]);
Text.settext (String.valueof (count));
if (juage) handler.postdelayed (runnable, 6000);
LOG.I (handler, handler);
}
}; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.main);
ImageView = (ImageView) Findviewbyid (R.id.imageview);
ImageView2 = (ImageView) Findviewbyid (R.ID.IMAGEVIEW2);
text= (TextView) Findviewbyid (R.id.text);
Text.settext (String.valueof (count));
Hide the Iamgeview first, then display imageview2.setvisibility (4);
Handler.postdelayed (Runnable, 2000); public void OnPause () {Juage = false;
Super.onpause (); }
}

Layout code:

Android:orientation=vertical
android:layout_width=fill_parent
android:layout_height=fill_parent
android:id=@+id/rl>
android:id=@+id/imageview
android:layout_width=fill_parent
android: background= @drawable/icon
android:layout_below=@+id/rl
android:layout_height=120dp/>
android: Id=@+id/imageview2
android:layout_width=fill_parent
android:background= @drawable/expriment
Android:layout_below=@+id/rl
android:layout_height=120dp/>
android:id=@+id/text
android: Layout_width=fill_parent
android:layout_height=wrap_content
android:layout_below= @id/imageview/>

Second: Using Viewflipper to implement the carousel of the picture

A multiple-page management control with an Android system that enables automatic interface switching:

First you need to add view for Viewflipper

(1) Static import: Import directly in Layout layout file

(2) Dynamic import: AddView () method

Viewplipper Common methods:

Setinanimation: Set the animation to use when view enters the screen

Setoutanimation: Set the animation to use when view exits the screen

Shownext: Call this function to display the next view inside the Viewflipper

Showprevious: Call this function to display the previous view inside the Viewflipper

Setflipinterval: Sets the time interval between view switches

Startflipping Use the time interval set above to start switching all view, the switch loops

Stopflipping: Stop View Toggle

With so much to say, what are we going to achieve today?

(1) using Viewflipper to realize the carousel of pictures

(2) Viewflipper to support gesture sliding

We need to prepare a few pictures first: Put the picture into the drawable

Create two animations: Creates a new folder under the Res create two more XML:

Left_in:
android:duration=5000
android:fromxdelta=100%p
android:toxdelta=0/>
left_out:
Android:fromxdelta=0
android:toxdelta=-100%p
android:duration=5000/>

A layout file:

Xmlns:tools=http://schemas.android.com/tools
android:layout_width=match_parent
android:layout_height= Match_parent
tools:context=. mainactivity >
android:id=@+id/flipper
android:layout_width=fill_parent
android:layout_height= Fill_parent/>

A main class:

public class Mainactivity extends activity {
private viewflipper flipper;
Private int[] Resid = {R.DRAWABLE.PC1,R.DRAWABLE.PC2,R.DRAWABLE.PC3,R.DRAWABLE.PC4};
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Flipper = (viewflipper) Findviewbyid (r.id.flipper);
* * * Dynamic Import mode for viewflipper add child view
* * *
/for (int i = 0; i < resid.length; i++) {
Flipper.addview (getim Ageview (Resid[i]));
*
* * for Viewflipper to add animation effect
* * *
/Flipper.setinanimation (this, r.anim.left_in);
Flipper.setoutanimation (this, r.anim.left_out);
Flipper.setflipinterval (5000);
Flipper.startflipping ();
}
Private ImageView Getimageview (int resid) {
ImageView image = new ImageView (this);
Image.setbackgroundresource (RESID);
return image;
}

So this will achieve a picture polling the function of the effect of

We can also add clicks, sliding effects:

We also need to add two sliding effects to the right:

Right_in:
android:fromxdelta=0
android:toxdelta=-100%p
android:duration=2000/>
right_out:
android:fromxdelta=100%p
android:toxdelta=0
android:duration=2000/>

Then we also need to add in the main class (if you do not want the image to play automatically, just want to use gestures to achieve the image playback so you need to "add animation effect for Viewflipper" delete):

Publibooleaontouchevent (motionevenevent{
/todauto-generatemethostub
switc (event.getaction () {
Casmotionevent.action_down:
startevent.getx ();
break;
casmotionevent.action_move://determine whether to slide left or right
I (Event.getx (start100{
flipper.setinanimation ( THISR.ANIM.LEFT_IN);
Flipper.setoutanimation (thisR.anim.left_out);
Flipper.showprevious ();
} Elsi (Startevent.getx (100{
flipper.setinanimation (thisR.anim.right_in);
Flipper.setoutanimation (thisR.anim.right_out);
Flipper.shownext ();
}
CASMOTIONEVENT.ACTION_UP: Break
;
Retursuper.ontouchevent (event);
}

This way we use our viewflipper to complete the picture polling function is done.

The above is a small set for everyone to share the Android image carousel effect of two methods, I hope you like.

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.