3 are the same, one is the picture, the other is the text
Click the previous, Next, or left, right swipe
Activity_main.xml
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/linearlayout1"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" > <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Previous" /> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Next" /> </LinearLayout> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <ViewswitcherAndroid:id= "@+id/viewswitcher1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <ImageViewAndroid:id= "@+id/imageview1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" /> </Viewswitcher> </LinearLayout></LinearLayout>
View Code
2 animations
Slide_in_right.xml
<?XML version= "1.0" encoding= "Utf-8"?> <Setxmlns:android= "Http://schemas.android.com/apk/res/android"> <!--set the animation to be dragged in from the right android:duration specifies the duration of the animation - <TranslateAndroid:fromxdelta= "100%p"Android:toxdelta= "0"android:duration= "@android: Integer/config_mediumanimtime" /> </Set>
View Code
Slide_out_left.xml
<?XML version= "1.0" encoding= "Utf-8"?> <Setxmlns:android= "Http://schemas.android.com/apk/res/android"> <!--set the animation android:duration to drag from the left to specify the duration of the animation - <TranslateAndroid:fromxdelta= "0"Android:toxdelta= " -100%p"android:duration= "@android: Integer/config_mediumanimtime" /> </Set>
View Code
Mainactivity.java
Public classMainactivityextendsactivity{//define a picture resource int[] pics=New int[]{R.DRAWABLE.PIC1,R.DRAWABLE.PIC2,R.DRAWABLE.PIC3}; //to determine if the finger is left or right floatX1; floatx2; Viewswitcher ViewSwitcher1; ImageView ImageView1; //Image Index intIndex=0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ViewSwitcher1=(Viewswitcher) Findviewbyid (r.id.viewswitcher1); Button BTN1=(Button) Findviewbyid (R.id.button1); Button btn2=(Button) Findviewbyid (R.id.button2); ImageView1=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); //Default First pictureImageview1.setimageresource (pics[0]); //the previous oneBtn1.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {pre (); } }); //Next OneBtn2.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {next (); } }); //Identify gesturesViewswitcher1.setontouchlistener (NewOntouchlistener () { Public BooleanOnTouch (View V, motionevent event) {//the coordinates when the finger is pressed if(event.getaction () = =Motionevent.action_down) {X1=Event.getx (); } //coordinates when the finger is released Else if(event.getaction () = =motionevent.action_up) {X2=Event.getx (); //Swipe left if(x1-x2>0) {LOG.V ("", "swipe left"); Next (); } //Swipe Right Else if(x2-x1>0) {LOG.V ("", "swipe right"); Pre (); } } return true; } }); } voidNext () {//animating the component display process for ViewswitcherViewswitcher1.setinanimation ( This, R.anim.slide_in_right); //animating the component hide process for ViewswitcherViewswitcher1.setoutanimation ( This, R.anim.slide_out_left); if(index==2) index=0; ElseIndex=index+1; Imageview1.setimageresource (Pics[index]); Viewswitcher1.shownext (); } voidPre () {//animating the component display process for ViewswitcherViewswitcher1.setinanimation ( This, Android. R.anim.slide_in_left); //animating the component hide process for ViewswitcherViewswitcher1.setoutanimation ( This, Android. R.anim.slide_out_right); if(index==0) index=2; ElseIndex=index-1; Imageview1.setimageresource (Pics[index]); Viewswitcher1.showprevious (); } }
View Code
Android Learning-Interface-ui-viewswitcher, Imageswitcher and Textswitcher