MyViewPager.class
public class MyViewPager extends ViewPager {
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setMyScroller();
}
/**
*/
private void setMyScroller() {
try {
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyScroller extends Scroller {
public MyScroller(Context context) {
super(context, new DecelerateInterpolator());
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, 600);
}
}
WelcomeActivity.class
public class WelcomeActivity extends Activity {
private MyViewPager mViewPager;
private List<ImageView> imageViews;
private List<View> adViews;
private ImageView[] mPageDotArray = new ImageView[5];
private int[] ids4PageDot = new int[5];
private View view1, view2, view3, view4, view5;
private static int PHOTO_CHANGE_TIME = 2000;// 广告滚动时间间隔
private Runnable runnable;
private PagerAdapter mPagerAdapter;
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initLayout();
initData();
initLinstener();
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
mViewPager.setOnTouchListener(new MyOnTouchListener());
mViewPager.setCurrentItem(adViews.size() * 50 - 1);
}
@Override
public void onStart() {
mHandler.post(runnable);
super.onStart();
}
@Override
public void initLayout() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcome);
super.initLayout();
}
@Override
public void initData() {
ids4PageDot = new int[] { R.id.page1, R.id.page2, R.id.page3, R.id.page4,
R.id.page5 };
initView(mPageDotArray, ids4PageDot);
LayoutInflater mLi = LayoutInflater.from(this);
view1 = mLi.inflate(R.layout.adview1, null, false);
view2 = mLi.inflate(R.layout.adview2, null, false);
view3 = mLi.inflate(R.layout.adview3, null, false);
view4 = mLi.inflate(R.layout.adview4, null, false);
view5 = mLi.inflate(R.layout.adview5, null, false);
adViews = Arrays.asList(view1, view2, view3, view4, view5);
imageViews = Arrays.asList(mPageDotArray);
mHandler = new Handler();
mViewPager = (MyViewPager) findViewById(R.id.viewpager);
runnable = new MyRunnable();
mPagerAdapter = new MyPageAdapter();
}
@Override
public void initLinstener() {
}
/**
* 离开该界面时停止线程反复调用
*/
@Override
protected void onPause() {
mHandler.removeCallbacks(runnable);
super.onPause();
}
class MyRunnable implements Runnable {
int a = 0;
@Override
public void run() {
int index = mViewPager.getCurrentItem();
mViewPager.setCurrentItem(index + 1);
mHandler.postDelayed(runnable, PHOTO_CHANGE_TIME);
}
}
class MyPageAdapter extends PagerAdapter {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public void destroyItem(View container, int position, Object object) {
((MyViewPager) container).removeView(adViews.get(position % adViews.size()));
}
@Override
public Object instantiateItem(View container, int position) {
((MyViewPager) container).addView(adViews.get(position % adViews.size()));
return adViews.get(position % adViews.size());
}
}
class MyOnPageChangeListener implements OnPageChangeListener {
// 三种状态SCROLL_STATE_DRAGGING,托动状态,值为1;
// SCROLL_STATE_IDLE,空闲状态,值为0
// SCROLL_STATE_SETTLING,稳定状态,值为2
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
for (ImageView iv : imageViews) {
iv.setImageDrawable(getResources().getDrawable(R.drawable.pagerdot));
}
imageViews.get(arg0 % adViews.size()).setImageDrawable(
getResources().getDrawable(R.drawable.pagerdot_current));
}
}
class MyOnTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case KeyEvent.ACTION_UP: {
mHandler.postDelayed(runnable, PHOTO_CHANGE_TIME);
break;
}
case KeyEvent.ACTION_DOWN: {
mHandler.removeCallbacks(runnable);
break;
}
}
return false;
}
}
}
xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="142"
android:background="#f2f2f2"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="279" >
<com.ccy.ui.MyViewPager
android:id="@+id/viewpager"
style="@style/fillParent" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagertitle"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:background="#0000"
android:textColor="#ffff" />
</com.ccy.ui.MyViewPager>
<ImageView
android:id="@+id/page5"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="ture"
android:layout_marginBottom="10dp"
android:layout_marginRight="6dp"
android:scaleType="matrix"
android:background="@drawable/pagerdot"
android:contentDescription="@string/ad1" />
<ImageView
android:id="@+id/page4"
android:layout_alignParentBottom="ture"
android:layout_marginBottom="10dp"
android:layout_marginRight="6dp"
android:scaleType="matrix"
android:layout_toLeftOf="@id/page5"
android:background="@drawable/pagerdot"
android:contentDescription="@string/ad1" />
<ImageView
android:id="@+id/page3"
android:layout_alignParentBottom="ture"
android:layout_marginBottom="10dp"
android:layout_marginRight="6dp"
android:scaleType="matrix"
android:layout_toLeftOf="@id/page4"
android:background="@drawable/pagerdot"
android:contentDescription="@string/ad1" />
<ImageView
android:id="@+id/page2"
android:layout_alignParentBottom="ture"
android:layout_marginBottom="10dp"
android:layout_marginRight="6dp"
android:scaleType="matrix"
android:layout_toLeftOf="@id/page3"
android:background="@drawable/pagerdot"
android:contentDescription="@string/ad1" />
<ImageView
android:id="@+id/page1"
android:layout_alignParentBottom="ture"
android:layout_marginBottom="10dp"
android:layout_marginRight="6dp"
android:scaleType="matrix"
android:layout_toLeftOf="@id/page2"
android:background="@drawable/pagerdot_current"
android:contentDescription="@string/ad1" />
</RelativeLayout>
</LinearLayout>
这样就可以,随考随用了!效果图稍后奉上