使用ViewPager实现循环滚动播放图片广告

时间:2021-07-25 17:56:28

首先还是主布局文件:activity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "vertical"  >
     
     <android.support.v4.view.ViewPager
         android:id= "@+id/main_mView"
         android:layout_width= "fill_parent"
         android:layout_height= "0dp"
         android:layout_weight= "1.5"
         ></android.support.v4.view.ViewPager>
     
</LinearLayout>

java代码实现:MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package  com.action;
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.os.Bundle;
import  android.os.Handler;
import  android.support.v4.app.FragmentActivity;
import  android.support.v4.view.PagerAdapter;
import  android.support.v4.view.ViewPager;
import  android.view.LayoutInflater;
import  android.view.View;
import  android.view.ViewGroup;
import  android.widget.ImageView;
import  android.widget.ImageView.ScaleType;
 
 
 
public  class  MainActivity  extends  FragmentActivity {
     
     //主布局ViewPager;
         private  ViewPager mViewPage;
         private  PagerAdapter mAdapter;
         
         private  List<ImageView> lsimage; //滑动图片的集合
         private  int [] imagid; //图片ID
         private  int  imagindex; //当前图片的索引号
         private  ScheduledExecutorService ses; //获取当前系统时间,为自动滑动确定时间
         
         // 切换当前显示的图片  
         private  Handler handler =  new  Handler() {  
             public  void  handleMessage(android.os.Message msg) {  
                 mViewPage.setCurrentItem(imagindex); // 切换当前显示的图片  
             };  
         }; 
     
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         
         //图片的ID
         imagid= new  int []{R.drawable.qq,R.drawable.img_frame_background,R.drawable.ic_launcher,R.drawable.home_btn_bg};
         
         //图片的集合
         lsimage= new  ArrayList<ImageView>();
         //初始化图片资源(既通过图片ID拿到图片资源加入到图片集合中)
         for  ( int  i =  0 ; i < imagid.length; i++) {
             
             ImageView imageView= new  ImageView( this );
             
             imageView.setImageResource(imagid[i]);
             
             imageView.setScaleType(ScaleType.CENTER_CROP);
             
             //加入集合
             lsimage.add(imageView);
             
         }
         
         
         //============ViewPager的创建===============================
                 //主布局ViewPager获取
                 
                 mViewPage=(ViewPager)findViewById(R.id.main_mView);
                 
                 
                 
                 //创建ViewPager适配器
                 mAdapter= new  PagerAdapter() {
                     
                     @Override
                     public  boolean  isViewFromObject(View arg0, Object arg1) {
                         // TODO Auto-generated method stub
                         return  arg0==arg1;
                     }
                     
                     @Override
                     public  int  getCount() {
                         // TODO Auto-generated method stub
                         return  lsimage.size();
                     }
                     
                     //创建这个适配器时,除了要强制重写以上两个方法外,还得重写一下两个方法
                     
                     //这个方法是摧毁view
                     @Override
                     public  void  destroyItem(View container,  int  position, Object object) {
                         // TODO Auto-generated method stub
                         ((ViewPager) container).removeView(lsimage.get(position));
                     }
                     
                     //这个方法是初始化view
                     @Override
                     public  Object instantiateItem(ViewGroup container,  int  position) {
                         // TODO Auto-generated method stub
 
                         //需要先获取View
                         View view=lsimage.get(position);
                         //然后将所有的view添加到ViewGroup这个存放view的容器中
                         container.addView(view);
                         //返回View
                         return  view;
                         
                     }
                     
                 };
                 //给View添加适配器
                 mViewPage.setAdapter(mAdapter);
                 
                 //=================================================================
     }
     
     //当Activity启动时,才自动切换
     @Override
     protected  void  onStart() {
         
         ses=Executors.newSingleThreadScheduledExecutor();
         //当Activity显示出来时,每过两秒钟切换图片,具体的切换方法交个ScrollTask();
         ses.scheduleAtFixedRate( new  ScrollTask(),  1 2 , TimeUnit.SECONDS);
         super .onStart();
     }
     
     //当Activity停止时,停止切换,保证应用不会无故暂用内存
     @Override
     protected  void  onStop() {
         
         ses.shutdown();
         super .onStop();
     }
     
     
     //执行切换任务
     private  class  ScrollTask  implements  Runnable{
         public  void  run() {
             
             //使用线程同步
             synchronized (mViewPage){
                 
                 imagindex=(imagindex+ 1 )%lsimage.size();
                 
                 handler.obtainMessage().sendToTarget();
             }
         }
     }
 
}


完工是不是很简单

源码案例下载地址:http://download.csdn.net/detail/symahll2011/8879083源码下载地址

3个例子下载:http://download.csdn.net/detail/zhaihaohao1/8909873

转载地址:http://my.oschina.net/symahll/blog/475638