广告循环轮播

package com.example.js_kftd_35.banneradcricle;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements Runnable,ViewPager.OnPageChangeListener{
    private final static int DEFAUTL_PAGE = 5;
    private final static int MAX_PAGE = 100;

    private ViewPager mViewPager;
    private int mBannerCount = 0;//表示当前在第几页
    private ViewPagerAdapter mViewPagerAdapter;
    private boolean isUserTouch = false;//用户是否触摸屏幕
    private Timer mTimer = new Timer();
    private TimerTask mTimeTask = new TimerTask() {
        @Override
        public void run() {
            if(!isUserTouch){
                mBannerCount = (mBannerCount+1)%100;
                runOnUiThread(MainActivity.this);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPagerAdapter = new ViewPagerAdapter(this);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int mEvent = event.getAction();
                if (mEvent == MotionEvent.ACTION_DOWN || mEvent == MotionEvent.ACTION_MOVE) {
                    isUserTouch = true;
                } else if (mEvent == MotionEvent.ACTION_UP) {
                    isUserTouch = false;
                }
                return false;
            }
        });
        mTimer.schedule(mTimeTask, 5000, 3000);
    }

    @Override
    public void run() {
        if (mBannerCount == MAX_PAGE-1) {
            mViewPager.setCurrentItem( DEFAUTL_PAGE-1, false);
        } else {
            mViewPager.setCurrentItem(mBannerCount);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mBannerCount = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public class ViewPagerAdapter extends PagerAdapter {
        int[] image = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d,R.mipmap.ic_launcher};
        Activity activity;
        ImageView mImageView;

        public ViewPagerAdapter(Activity activity){
            this.activity = activity;
        }

        @Override
        public int getCount() {
            return MAX_PAGE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            position = position%DEFAUTL_PAGE;
            View view = activity.getLayoutInflater().inflate(R.layout.image,null);
            mImageView = (ImageView) view.findViewById(R.id.image);
            mImageView.setImageResource(image[position]);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            int position = mViewPager.getCurrentItem();
            if(position == 0){
                position = DEFAUTL_PAGE;
                mViewPager.setCurrentItem(position,false);
            }else if(position == MAX_PAGE-1){
                position = DEFAUTL_PAGE-1;
                mViewPager.setCurrentItem(position,false);
            }
        }

    }

}

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.js_kftd_35.banneradcricle.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="100dp">
    </android.support.v4.view.ViewPager>
</RelativeLayout>

<?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">
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>


你可能感兴趣的:(广告循环轮播)