Main.xml
<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools"android:layout_width="Match_ Parent "android:layout_height="match_parent "tools:context=". Main " > <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:layout_centerhorizontal="true"android:layout_centervertical="true" Android:text="@string/hello_world" /> </relativelayout>
Main.java
package com.wty.mydemoviewflipper;import android.app.Activity;import android.os.Bundle;import com.wty.mydemoviewflipper.wiget.ViewFlipperView;publicclass Main extends Activity { protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new ViewFlipperView(this)); }}
Iadimages.java
Packagecom. Wty. Mydemoviewflipper. Wiget;Importcom. Wty. Mydemoviewflipper. R;/** * Picture Information interface * @author vhreal * */Public interface Iadimages {/** * Advertising pictures * *Int[] Adimages = {R. drawable. IMG, R. drawable. IMGGeneva, R. drawable. IMGGeneva, R. drawable. IMG, R. drawable. IMG, R. drawable. IMG, R. drawable. IMG, R. drawable. IMG, R. drawable. IMG, R. drawable. IMG10};}
Viewflipperview.java
PackageCom.wty.mydemoviewflipper.wiget;ImportAndroid.content.Context;Importandroid.view.Gravity;ImportAndroid.view.MotionEvent;ImportAndroid.view.animation.Animation;ImportAndroid.view.animation.TranslateAnimation;ImportAndroid.widget.FrameLayout;ImportAndroid.widget.ImageView;ImportAndroid.widget.LinearLayout;ImportAndroid.widget.ViewFlipper;ImportCOM.WTY.MYDEMOVIEWFLIPPER.R;/** * This is a wiget similar to the viewflipper. * @author vhreal * * * * Public class viewflipperview extends framelayout implements Iadimages { PrivateContext context;//Caller's context Private intCurrentadimgindex;//Current AD image index PrivateAnimation left2rightinanimation;//Advertising images go from left to right into screen animations PrivateAnimation left2rightoutanimation;//Advertising pictures go out from left to right screen animations PrivateAnimation right2leftinanimation;//advertising images from right to left into screen animations PrivateAnimation right2leftoutanimation;//Advertising pictures go out from right to left screen animations Private intAnimationduration = -;//Animation takes time 1000 milliseconds PrivateViewflipper Mviewflipper;//Sliding page controls PrivateLinearLayout mtiplinearlayout;//Bottom point control Private floatStartX =0;x-coordinate when touch action is down Private floatEndX =0;//Touch action up x-coordinate Public Viewflipperview(Context context) {Super(context); This. Context = Context; Setview (); }/** * Show View */ Private void Setview(){//Initialize intScreenWidth = Getresources (). Getdisplaymetrics (). Widthpixels; Mviewflipper =NewViewflipper (context); Mtiplinearlayout =NewLinearLayout (context);//Initialize animationLeft2rightinanimation =NewTranslateanimation (-screenwidth,0,0,0); Left2rightinanimation.setduration (animationduration); Left2rightoutanimation =NewTranslateanimation (0, ScreenWidth,0,0); Left2rightoutanimation.setduration (animationduration); Right2leftinanimation =NewTranslateanimation (ScreenWidth,0,0,0); Right2leftinanimation.setduration (animationduration); Right2leftoutanimation =NewTranslateanimation (0,-screenwidth,0,0); Right2leftoutanimation.setduration (animationduration);//Add ad images to Viewflipper for(intI=0; i<adimages.length; i++) {ImageView image =NewImageView (context); Image.setimageresource (Adimages[i]); Mviewflipper.addview (image); } addview (Mviewflipper);//Add a picture index point dynamically for(intI=0; i<adimages.length; i++) {ImageView image =NewImageView (context);if(i = =0) {Image.setimageresource (r.drawable.point_selected); }Else{Image.setimageresource (r.drawable.point_normal); } image.setpadding (5,0,5, -); Mtiplinearlayout.addview (image); }//placed in the lower left cornerMtiplinearlayout.setgravity (Gravity.bottom | Gravity.center_horizontal); AddView (mtiplinearlayout); }@Override Public Boolean ontouchevent(Motionevent event) {Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:startX = Event.getx (); Break; CaseMotionEvent.ACTION_UP:endX = Event.getx ();//Save the previous point firstImageView Lasttipimageview = (ImageView) mtiplinearlayout.getchildat (Currentadimgindex);if(Currentadimgindex >0&& endx > StartX) {//View the previous page of adsMviewflipper.setinanimation (left2rightinanimation); Mviewflipper.setoutanimation (left2rightoutanimation); Mviewflipper.showprevious (); currentadimgindex--;if(Currentadimgindex <0) {Currentadimgindex =0; } }if(Currentadimgindex < adimages.length-1&& EndX < StartX) {//View the next page of adsMviewflipper.setinanimation (right2leftinanimation); Mviewflipper.setoutanimation (right2leftoutanimation); Mviewflipper.shownext (); currentadimgindex++;if(Currentadimgindex > adimages.length-1) {Currentadimgindex = adimages.length-1; } }//Change the state of the bottom point according to CurrentadimgindexImageView Currtipimageview = (ImageView) mtiplinearlayout.getchildat (Currentadimgindex); Lasttipimageview.setimageresource (R.drawable.point_normal); Currtipimageview.setimageresource (r.drawable.point_selected); Break; }return true; }}
A simple example of viewflipper in Android