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

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

<?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

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源码下载地址

你可能感兴趣的:(使用ViewPager实现循环滚动播放图片广告)