This example is done with Viewpager, support automatic sliding and manual sliding, not only Youku, there are actually many mall and portal sites have similar implementations:
Specific ideas:
1. You need to add Android-support-v4.jar in the project to use the Viewpager control.
2. Automatic Image switching: You can use a timer or scheduledexecutorservice, which can be implemented in multiple ways.
At the same time, switch the dots at the bottom (Park point)
3.handler+message mechanism to update the UI, this is believed to be very skilled, no longer describe
4. Some of the details of the implementation: note the optimizations in this example: the automatic switchover of the picture initiates the other thread, and the thread is opened in the OnStart () method, in the visible to invisible state of the activity, that is, the thread is stopped in the OnStop () method. Otherwise, the timer does not stop, or repeatedly open, will cause a large memory consumption, a long time on the program will be collapsed. Also, there is a lag in the process of jumping to other activity.
Take a look at the following and the specific code:
The engineering structure is as follows:
Main.xml:
Then is the specific layout file and code implementation:
Main.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" #FFFFFF "a ndroid:orientation= "vertical" > <relativelayout android:layout_width= "fill_parent" Android:layout_h eight= "40dip" android:background= "@drawable/title_bk" > <imagebutton android:id= "@+id/btn_b Ack "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:bac kground= "@drawable/btn_back_selector" android:src= "@drawable/btn_back"/> <view Android : id= "@+id/line0" android:layout_width= "1px" android:layout_height= "Fill_parent" android:l ayout_torightof= "@id/btn_back" android:background= "#aa11264f"/> <view android:layout_w Idth= "1px" android:layout_height= "Fill_parent" android:layout_torightof= "@id/line0" android:background= "#009ad6"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a Ndroid:layout_centerinparent= "true" android:text= "Youku Client" android:textcolor= "#FFFFFF" Android Oid:textsize= "20sp"/> </RelativeLayout> <framelayout android:layout_width= "Fill_parent" a ndroid:layout_height= "140dip" > <android.support.v4.view.viewpager android:id= "@+id/vp" Android:layout_width= "Fill_parent" android:layout_height= "fill_parent"/> <linearlayout Android:layout_width= "Fill_parent" android:layout_height= "35dip" android:layout_gravity= "Bottom" Android:background= "#33000000" android:gravity= "center" android:orientation= "vertical" ; <textvieW android:id= "@+id/tv_title" android:layout_width= "Wrap_content" Android:layo ut_height= "Wrap_content" android:text= "National Courtyard Campus Area name embodies" android:textcolor= "#ffffff"/> <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "android:layout_margintop=" 3dip "android:gravity=" center "> <view Android:id= "@+id/v_dot0" style= "@style/dot_style" Android:backgrou nd= "@drawable/dot_focused"/> <view android:id= "@+id/v_dot1" St yle= "@style/dot_style"/> <view android:id= "@+id/v_dot2" style= "@style/dot_style"/> <view android:id= "@+id/v_dot3" style= "@st Yle/dot_style "/> <view android:id= "@+id/v_dot4" style= "@style/dot_style"/> </LinearLayout> </LinearLayout> </FrameLayout></LinearLayout>
Myviewpageractivity:
Package Com.tony.viewpager;import Java.util.arraylist;import Java.util.list;import java.util.concurrent.Executors; Import Java.util.concurrent.scheduledexecutorservice;import Java.util.concurrent.timeunit;import Android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.os.Parcelable;import Android.support.v4.view.pageradapter;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.view;import Android.widget.ImageView; Import Android.widget.imageview.scaletype;import android.widget.textview;/** * Youku android client picture swipe left and right * */public class Myviewpageractivity extends Activity {private Viewpager Viewpager;//ANDROID-SUPPORT-V4 sliding components private list< Imageview> imageviews; Sliding picture Collection Private string[] titles; Picture title private int[] Imageresid; Picture idprivate list<view> Dots; The picture title body of those points private TextView tv_title;private int currentitem = 0; Index number of the current picture//an executorservice that can schedule CommanDS to run after a given delay,//or to execute periodically.private scheduledexecutorservice scheduledexecutorservice;//cut Change the currently displayed picture private Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) { Viewpager.setcurrentitem (CurrentItem);//Toggle the currently displayed picture}; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main) Imageresid = new int[] {r.drawable.a, r.drawable.b, r.drawable.c, R.DRAWABLE.D, R.drawable.e};titles = NE W string[imageresid.length];titles[0] = "Gong Li is not vulgar, I can not vulgar"; titles[1] = "The tree is back!" Re-sing the classic songs of the People Chorus "; titles[2] =" Secret Beijing movie How to Upgrade "; titles[3] =" Le video network TV version of the large delivery "; Titles[4] =" anti-killing of hot-blooded cock "; imageviews = new arraylist< Imageview> ();//Initialize picture resource for (int i = 0; i < imageresid.length; i++) {ImageView ImageView = new ImageView (this); Imagev Iew.setimageresource (Imageresid[i]); Imageview.setscaletype (Scaletype.center_crop); ImageViews.add (ImageView);} Dots = new Arraylist<view> ();d Ots.add (Findviewbyid (r.id.v_dot0));d oTs.add (Findviewbyid (R.ID.V_DOT1));d Ots.add (Findviewbyid (r.id.v_dot2));d Ots.add (Findviewbyid (R.ID.V_DOT3)); Dots.add (Findviewbyid (R.ID.V_DOT4)); tv_title = (TextView) Findviewbyid (r.id.tv_title); Tv_title.settext (Titles[0]) ;//viewpager = (Viewpager) Findviewbyid (R.ID.VP); Viewpager.setadapter (new Myadapter ());//Set the adapter to fill the Viewpager page// Set up a listener to call Viewpager.setonpagechangelistener (new Mypagechangelistener ()) when the page in Viewpager is changed;} @Overrideprotected void OnStart () {scheduledexecutorservice = Executors.newsinglethreadscheduledexecutor ();// Once the activity is displayed, switch the picture to display Scheduledexecutorservice.scheduleatfixedrate (new Scrolltask (), 1, 2, timeunit.seconds) every two seconds; Super.onstart ();} @Overrideprotected void OnStop () {//Stop switching when activity is not visible scheduledexecutorservice.shutdown (); Super.onstop ();} /** * Line Shift Task * * @author Administrator * */private class Scrolltask implements Runnable {public void run () {synchronized (Viewpager) {System.out.println ("CurrentItem:" + currentitem); CurrentItem = (CurrentItem + 1)% Imageviews. Size (); Handler.obtainmessage (). Sendtotarget (); Toggle picture through Handler}}}/** * call when the state of the page in Viewpager is changed * * @author Administrator * */private class Mypagechangelistener Implem Ents onpagechangelistener {private int oldposition = 0;/** * This method is invoked when a new page becomes selected. * Position:position Index of the new selected page. */public void onpageselected (int position) {CurrentItem = Position;tv_title.settext (titles[position]);d Ots.get ( oldposition). Setbackgroundresource (R.drawable.dot_normal);d ots.get (position). Setbackgroundresource ( r.drawable.dot_focused); oldposition = position;} public void onpagescrollstatechanged (int arg0) {}public void onpagescrolled (int arg0, float arg1, int arg2) {}}/** * fill vie Wpager Page Adapter * * @author Administrator * */private class Myadapter extends Pageradapter {@Overridepublic int getcount () {return imageresid.length;} @Overridepublic Object Instantiateitem (View arg0, int arg1) {((Viewpager) arg0). AddView (Imageviews.get (arg1)); return ImagevieWs.get (arg1);} @Overridepublic void Destroyitem (view arg0, int arg1, Object arg2) {(Viewpager) arg0). Removeview ((view) arg2);} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic void Restorestate (parcelable arg0, ClassLoader arg1) {} @Overridepublic parcelable saveState () {return null;} @Overridepublic void Startupdate (view arg0) {} @Overridepublic void Finishupdate (view arg0) {}}}
Under the drawable directory
Btn_back_selector.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/btn_top_pressed "android:state_focused=" true "></item > <item android:drawable= "@drawable/btn_top_pressed" android:state_pressed= "true" ></item> <item android:drawable= "@drawable/btn_top_pressed" android:state_selected= "true" ></item> <item android:drawable= "@drawable/title_bk" ></item></selector>
Btn_top_pressed.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" android:shape= "Rectangle" > <gradient android:angle= "android:endcolor=" #009ad6 " android:startcolor= "#11264f"/></shape>
Dot_focused.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" android:shape= "Oval" > <solid android:color= "#aaFFFFFF"/> <corners android:radius= "5dip" /></shape>
Dot_normal.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" android:shape= "Oval" > <solid android:color= "#33000000"/> <corners android:radius= "5dip "/></shape>
Title_bk.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" Android:shape= "Rectangle" > <gradient android:angle= "+" android:endcolor= "#11264f" Android : startcolor= "#009ad6"/></shape>
Source:
http://download.csdn.net/detail/t12x3456/4751688
High profile cool Android client image swipe left and right (auto toggle)