ViewPager + Handler + Timer: simple ad carousel effect, viewpagerhandler
The basic idea is to put a ViewPager in Avtivity, and then implement the linkage effect through listening. I will not talk about the reason for the code in detail.
MainActivity. java
1 package com. example. administrator. imageviewlunbodemo; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 import android. OS. handler; 6 import android. OS. message; 7 import android. support. v4.view. pagerAdapter; 8 import android. support. v4.view. viewPager; 9 import android. view. layoutInflater; 10 import android. view. view; 11 import android. view. viewGroup; 12 import android. widget. imageView; 13 14 import java. util. arrayList; 15 import java. util. list; 16 import java. util. timer; 17 import java. util. timerTask; 18 19 20 public class MainActivity extends Activity {21 22 private ViewPager myViewPager; 23 private List <View> myContiontar = new ArrayList <> (); // viewPager data source 24 private PagerAdapter myPagerAdapter; // if you have a data source, you must have an adapter 25 26 private ImageView imageView1; 27 private ImageView imageView2; 28 private ImageView imageView3; 29 private ImageView imageView4; 30 private ImageView imageView5; 31 32 private Timer mTimer; 33 private Timertask mTimertask; 34 35 @ Override 36 protected void onCreate (Bundle savedInstanceState) {37 super. onCreate (savedInstanceState); 38 setContentView (R. layout. activity_main); 39 40 initViews (); // initialize various views 41 42 myViewPager. setOnPageChangeListener (new ViewPager. onPageChangeListener () {43 @ Override 44 public void onPageScrolled (int I, float v, int i2) {45 46} 47 48 @ Override 49 public void onPageSelected (int I) {50 selectImageId (I); 51} 52 53 @ Override 54 public void onPageScrollStateChanged (int I) {55 56} 57}); 58 59 mTimertask = new Timertask (); 60 mTimer = new Timer (); 61 mTimer. schedule (mTimertask,); 62 // select the first image 63 // imageView1.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_select); 64 65 66} 67 68 // initialize various views 69 private void initViews () {70 // Replace the xml file with View 71 myViewPager = (ViewPager) findViewById (R. id. viewpager); 72 73 imageView1 = (ImageView) findViewById (R. id. first_fragment_down_image1); 74 imageView2 = (ImageView) findViewById (R. id. first_fragment_down_image2); 75 imageView3 = (ImageView) findViewById (R. id. first_fragment_down_image3); 76 imageView4 = (ImageView) findViewById (R. id. first_fragment_down_image4); 77 imageView5 = (ImageView) findViewById (R. id. first_fragment_down_image5); 78 79 // create five views to get four imageviews 80 view view1 = LayoutInflater. from (getApplicationContext ()). inflate (R. layout. lunbo_image1, null); 81 View view2 = LayoutInflater. from (getApplicationContext ()). inflate (R. layout. lunbo_image2, null); 82 View view3 = LayoutInflater. from (getApplicationContext ()). inflate (R. layout. lunbo_image3, null); 83 View view4 = LayoutInflater. from (getApplicationContext ()). inflate (R. layout. lunbo_image4, null); 84 View view5 = LayoutInflater. from (getApplicationContext ()). inflate (R. layout. lunbo_image5, null); 85 // Add 86 myContiontar to the container. add (view1); 87 myContiontar. add (view2); 88 myContiontar. add (view3); 89 myContiontar. add (view4); 90 myContiontar. add (view5); 91 // initialize the adapter 92 myPagerAdapter = new PagerAdapter () {93 // return the displayed number of items 94 @ Override 95 public int getCount () {96 return myContiontar. size (); 97} 98 99 @ Override100 public boolean isViewFromObject (View view, Object o) {101 return view = o; 102} 103 // during slide switching, remove the current component 104 @ Override105 public void destroyItem (ViewGroup container, int position, Object object) {106 container. removeView (myContiontar. get (position); 107} 108 // component generated when no sliding occurs 109 @ Override110 public Object instantiateItem (ViewGroup container, int position) {111 container. addView (myContiontar. get (position); 112 return myContiontar. get (position); 113} 114}; 115 // set adapter 116 myViewPager. setAdapter (myPagerAdapter); 117} 118 119 // select the image 120 private void selectImageId (int I) {121 initImageBackGround (); 122 switch (I) {123 case 0: 124 imageView1.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_select); 125 break; 126 case imageView2.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_select); 128 break; 129 case 130 imageView3.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_select); 131 break; 132 case export imageView4.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_select); 134 break; 135 case imageView5.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_select); 137 break; 138} 139} 140 // initialize the background of all images 141 private void initImageBackGround () {142 imageView1.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_normal); 143 imageView2.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_normal); 144 imageView3.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_normal); 145 imageView4.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_normal); 146 imageView5.setBackgroundResource (R. drawable. wallet_coin_purse_guide_purse_dot_normal); 147} 148 149 int count = 0; 150 private Handler mhandler = new Handler () {151 152 @ Override153 public void handleMessage (Message msg) {154 super. handleMessage (msg); 155 if (msg. what = 0x111) {156 // operation 157 count ++; 158 myViewPager. setCurrentItem (count % 5); 159} 160} 161}; 162 163 // create a Timertask164 class Timertask extends TimerTask {165 166 @ Override167 public void run () {168 mhandler. sendEmptyMessage (0x111); // send an empty message 169} 170} 171}
The main layout file contains a ViewPager and five imageviews)
Activity_main.xml
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" android:gravity="center|bottom"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:orientation="horizontal" android:layout_gravity="center|bottom"> <ImageView android:id="@+id/first_fragment_down_image1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/> <ImageView android:id="@+id/first_fragment_down_image2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/> <ImageView android:id="@+id/first_fragment_down_image3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/> <ImageView android:id="@+id/first_fragment_down_image4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/> <ImageView android:id="@+id/first_fragment_down_image5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/wallet_coin_purse_guide_purse_dot_normal"/> </LinearLayout> </FrameLayout> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2.5" android:background="@color/fitst_fragment_image_color"/></LinearLayout>
In ViewPager, I created five layout files, which are very simple. There is only one ImageView to display images.
Images. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/first_fragment_lunbo_image1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/image1" /></LinearLayout>
Then, when Timer is used to send a message every two seconds to notify ViewPager to update, a simple image carousel effect is formed.
: