码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager控件实现欢迎页

时间:2015-04-09 16:57:33      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.owen.welcomeviewpage;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import android.app.Activity;
  7 import android.content.Intent;
  8 import android.os.Bundle;
  9 import android.support.v4.view.PagerAdapter;
 10 import android.support.v4.view.ViewPager;
 11 import android.support.v4.view.ViewPager.OnPageChangeListener;
 12 import android.view.View;
 13 import android.view.ViewGroup;
 14 import android.view.Window;
 15 import android.widget.Button;
 16 import android.widget.ImageView;
 17 import android.widget.ImageView.ScaleType;
 18 
 19 public class MainActivity extends Activity {
 20     private ViewPager mViewPager;
 21     /** 三张引导页面的图片 */
 22     private int[] mImgIds = new int[] { R.drawable.viewpage_1,
 23             R.drawable.viewpage_2, R.drawable.viewpage_3 };
 24     /** 图片资源容器 */
 25     private List<ImageView> mImageViews = new ArrayList<ImageView>();
 26 
 27     @Override
 28     protected void onCreate(Bundle savedInstanceState) {
 29         super.onCreate(savedInstanceState);
 30 
 31         requestWindowFeature(Window.FEATURE_NO_TITLE);
 32         setContentView(R.layout.activity_main);
 33 
 34         initData();
 35         if (isFirstLaunchApp()) {
 36             initViewPager();
 37         } else {
 38             jumpToIndexActivity();
 39         }
 40     }
 41 
 42     private void initData() {
 43         for (int imgId : mImgIds) {
 44             ImageView imageView = new ImageView(getApplicationContext());
 45             imageView.setScaleType(ScaleType.CENTER_CROP);
 46             imageView.setImageResource(imgId);
 47             
 48             mImageViews.add(imageView);
 49         }
 50     }
 51 
 52     /**
 53      * 判断是否是第一次启动App
 54      * 
 55      * @return 是-true 否-false
 56      */
 57     private boolean isFirstLaunchApp() {
 58         // TODO 读/写 SharePreference
 59         return true;
 60     }
 61     
 62     /**
 63      * 是否已登录
 64      * 
 65      * @return 是-true 否-false
 66      */
 67     private boolean isLogined() {
 68         return false;
 69     }
 70     
 71     /**
 72      * 跳转到App主界面
 73      */
 74     private void jumpToIndexActivity() {
 75         if (isLogined()) {
 76             // TODO 启动主界面
 77         } else {
 78             jumpToLoginActivity();
 79         }
 80         
 81         finish();
 82     }
 83     
 84     /**
 85      * 跳转到登录页面
 86      */
 87     private void jumpToLoginActivity() {
 88         Intent intent = new Intent(MainActivity.this, WelcomeActivity.class);
 89         startActivity(intent);
 90         
 91         finish();
 92     }
 93     
 94     private void initViewPager() {
 95         mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
 96 
 97         mViewPager.setAdapter(new PagerAdapter() {
 98 
 99             @Override
100             public Object instantiateItem(ViewGroup container, int position) {
101                 container.addView(mImageViews.get(position));
102                 return mImageViews.get(position);
103             }
104 
105             @Override
106             public void destroyItem(ViewGroup container, int position,
107                     Object object) {
108                 container.removeView(mImageViews.get(position));
109             }
110 
111             @Override
112             public boolean isViewFromObject(View view, Object object) {
113                 return view == object;
114             }
115 
116             @Override
117             public int getCount() {
118                 return mImgIds.length;
119             }
120         });
121 
122         mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
123 
124             @Override
125             public void onPageSelected(int position) {}
126 
127             @Override
128             public void onPageScrolled(int position, float positionOffset, 
129                     int positionOffsetPixels) {}
130 
131             /**
132              * 设置Button只有到最后一页的时候才出现
133              */
134             @Override
135             public void onPageScrollStateChanged(int state) {
136                 boolean misScrolled = false;
137                 Button btnStart = (Button) findViewById(R.id.btnStart);
138                 btnStart.setOnClickListener(new View.OnClickListener() {
139                     
140                     @Override
141                     public void onClick(View v) {
142                         jumpToLoginActivity();
143                     }
144                 });
145                 
146                 switch (state) {
147                 case ViewPager.SCROLL_STATE_DRAGGING:
148                     misScrolled = false;
149                     btnStart.setVisibility(View.INVISIBLE);
150                     break;
151                 case ViewPager.SCROLL_STATE_SETTLING:
152                     misScrolled = true;
153                     btnStart.setVisibility(View.INVISIBLE);
154                     break;
155                 case ViewPager.SCROLL_STATE_IDLE:
156                     if (mViewPager.getCurrentItem() == mViewPager.getAdapter()
157                             .getCount() - 1 && !misScrolled) {
158                         btnStart.setVisibility(View.VISIBLE);
159                     }
160                     misScrolled = true;
161                     break;
162                 }
163             }
164         });
165     }
166 
167 }

 

ViewPager控件实现欢迎页

标签:

原文地址:http://www.cnblogs.com/stayfoolishstayhungry/p/4410027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!