Android ViewPager图片自动轮播

Android 04/17 阅读 330 views次 人气 0
摘要:

public class MainActivity extends Activity {
    private ViewPager pager;
    private ImageView mDian1, mDian2, mDian3, mDian4, img;
    private List<View> list = new ArrayList<View>();
    private int[] image = {R.drawable.suc1, R.drawable.suc2, R.drawable.suc3, R.drawable.suc4};
    private View[] views = new View[image.length];
    private boolean TouchFlag = false;// 触摸锁
    private boolean isFlag = true;// 关闭广告轮播的标志
    private int index = 0;// 当前页面索引值
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initList();
        startTime();
    }

    private void initView() {
        pager = (ViewPager) findViewById(R.id.pager);
        mDian1 = (ImageView) findViewById(R.id.mDian1);
        mDian2 = (ImageView) findViewById(R.id.mDian2);
        mDian3 = (ImageView) findViewById(R.id.mDian3);
        mDian4 = (ImageView) findViewById(R.id.mDian4);
        pager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                index = arg0;
                Log.e("", "当前显示的图片索引"+arg0);
                clean();
                switch (arg0%views.length) {
                case 0:
                    mDian1.setImageResource(R.drawable.vote_n_can_y);
                    break;

                case 1:
                    mDian2.setImageResource(R.drawable.vote_n_can_y);
                    break;

                case 2:
                    mDian3.setImageResource(R.drawable.vote_n_can_y);
                    break;

                case 3:
                    mDian4.setImageResource(R.drawable.vote_n_can_y);
                    break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
                Log.e("", "当前索引"+arg0);
                Log.e("", "当前偏移量"+arg1);
                Log.e("", "当前偏移像素"+arg2);
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
                // 滑动
                Log.e("", "111111"+ViewPager.SCROLL_STATE_DRAGGING);
                // 停止
                Log.e("", "000000"+ViewPager.SCROLL_STATE_IDLE);
                // 沉降
                Log.e("", "222222"+ViewPager.SCROLL_STATE_SETTLING);
                if (arg0 == ViewPager.SCROLL_STATE_IDLE) {
                    TouchFlag = false;
                }else {
                    TouchFlag = true;
                }
            }
        });

        pager.setAdapter(new MyAdapter());
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                if (TouchFlag) 
                    return ;
                if (!isFlag)
                    return ;
                index++;
                    pager.setCurrentItem(index);
            }
        }
    };

    private void initList() {
        for (int i = 0; i < image.length; i++) {
            View v = View.inflate(this, R.layout.view_main, null);
            img = (ImageView) v.findViewById(R.id.mImg);
            img.setImageResource(image[i]);
            list.add(v);    
            views[i] = v;
        }
    }

    public class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager)container).removeView(views[position%list.size()]);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager)container).addView(views[position%list.size()]);
            return views[position%list.size()];
        }
    }

    private void clean(){
        mDian1.setImageResource(R.drawable.vote_n_can_n);
        mDian2.setImageResource(R.drawable.vote_n_can_n);
        mDian3.setImageResource(R.drawable.vote_n_can_n);
        mDian4.setImageResource(R.drawable.vote_n_can_n);
    }

    private class TimeThread extends Thread{
        @Override
        public void run() {
            while (isFlag) {
                try {
                    Thread .sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(0);
            }
        }
    }

    private void startTime(){
        new TimeThread().start();
    }

    private void closeTime(){
        isFlag = false;
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        closeTime();
    }

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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=".MainActivity">

    <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" />
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:layout_marginBottom="25dp" android:orientation="horizontal" >
        <ImageView android:id="@+id/mDian1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/vote_n_can_y" />
        <ImageView android:id="@+id/mDian2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/vote_n_can_n" />
        <ImageView android:id="@+id/mDian3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/vote_n_can_n" />
        <ImageView android:id="@+id/mDian4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/vote_n_can_n" />
    </LinearLayout>


</RelativeLayout>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

图片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" >

    <ImageView android:id="@+id/mImg" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" />

</LinearLayout>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

小奋斗文章

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

评论

该文章不支持评论!

分享到: