Android实现引导页并滑动跳转到主界面

public class SplashGuideActivity extends Activity implements
		OnPageChangeListener, OnClickListener {

	private ViewPager viewPager;
	private ViewPagerAdapter viewPagerAdapter;
	private List views;

	public GestureDetector mGestureDetector;

	private int currentItem = 0; 
	private int flaggingWidth;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.guide);

		slideToMain();
		// 获取分辨率
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		flaggingWidth = dm.widthPixels / 3;
		initViewPager();
	}

	private void initViewPager() {
		LayoutInflater inflater = LayoutInflater.from(this);

		views = new ArrayList();

		View guide_one = inflater.inflate(R.layout.guide_one, null);
		View guide_two = inflater.inflate(R.layout.guide_two, null);
		View guide_three = inflater.inflate(R.layout.guide_three, null);
		View guide_four = inflater.inflate(R.layout.guide_four, null);

		guide_four.setOnClickListener(this);

		views.add(guide_one);
		views.add(guide_two);
		views.add(guide_three);
		views.add(guide_four);

		viewPagerAdapter = new ViewPagerAdapter();
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		viewPager.setAdapter(viewPagerAdapter);

		viewPager.setOnPageChangeListener(this);
	}

	@Override
	public void onClick(View v) {
		SharedPreferencesUtil.getInstance(this)
				.setBoolean("isFirstOpen", false);
		// Intent intent = new Intent(this, MainActivity.class);
		// startActivity(intent);
		// this.finish();
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		currentItem = position;
		System.out.println("------" + currentItem);
	}

	public class ViewPagerAdapter extends PagerAdapter {

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

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(views.get(position));
			return views.get(position);
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}

	private void slipToMain(){
		mGestureDetector = new GestureDetector(this,
				new GestureDetector.SimpleOnGestureListener() {

					@Override
					public boolean onFling(MotionEvent e1, MotionEvent e2,
							float velocityX, float velocityY) {
						if (currentItem == 3) {
							if ((e1.getRawX() - e2.getRawX()) >= flaggingWidth) {

								Intent intent = new Intent(
										SplashGuideActivity.this,
										MainActivity.class);
								startActivity(intent);
								finish();
								return true;
							}
						}
						return false;
					}

				});
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mGestureDetector.onTouchEvent(event);
		return super.onTouchEvent(event);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		if (mGestureDetector.onTouchEvent(event)) {
			event.setAction(MotionEvent.ACTION_CANCEL);
		}
		return super.dispatchTouchEvent(event);
	}
}

你可能感兴趣的:(Android)