android 引导界面的实现

现在越来越多程序都有引导页面了。

网上资料不全。现在自己实现下。

下图是总的目录结构。

 

/**
* 实现
* @author dujinyang
*
*/

 

 

 

顺序是: OneAcitivity  –>MainActivity -> TwoActivity

然后第2次进去就是:OneActivity -> TwoActivity

代码里都有注释的了,这里就不多说了。

 

 

OneActivity的代码如下:

 

[java] view plaincopyprint?

  1. package cn.djy.activity;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. /**
  8.  * 开机动画
  9.  * @author Administrator
  10.  *
  11.  */
  12. public class OneActivity extends Activity {
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         // TODO Auto-generated method stub
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.one);
  18.         boolean flag=this.isFirstEnter(getApplicationContext(), this.getClass().getName());
  19.         if(flag)
  20.             mHandler.sendEmptyMessageDelayed(SWITCH_GUIDACTIVITY,4000);
  21.         else
  22.             mHandler.sendEmptyMessageDelayed(SWITCH_TWOACTIVITY, 4000);
  23.     }
  24.     //***********************************************************************
  25.     //判断应用是否初次加载,读取SharedPreferences 的字段
  26.     //***********************************************************************
  27.     private static final String SHAREDPREFERENCES_NAME=”yang”;
  28.     private static final String KEY_GUIDE_ACTIVITY=”Open”;
  29.     /**
  30.      * mResultStr.equalsIgnoreCase(“false”) 返回FALSE
  31.      * TRUE 则为空值
  32.      * @param context
  33.      * @param className
  34.      * @return boolean
  35.      */
  36.     private boolean isFirstEnter(Context context,String className){
  37.         if(context==null || className ==null || “”.equalsIgnoreCase(className)) return false;
  38.         String mResultStr=context.getSharedPreferences(SHAREDPREFERENCES_NAME,Context.MODE_WORLD_READABLE).getString(KEY_GUIDE_ACTIVITY, “”);
  39.         if(mResultStr.equalsIgnoreCase(“false”))
  40.             return false;
  41.         else
  42.             return true;
  43.     }
  44.     //****************************************
  45.     //Handler:跳转至不同页面
  46.     //****************************************
  47.     private final static int SWITCH_TWOACTIVITY=1000; //主页
  48.     private final static int SWITCH_GUIDACTIVITY=1001; //滑动手势
  49.     private Handler mHandler=new Handler(){
  50.         public void handleMessage(android.os.Message msg) {
  51.             switch (msg.what) {
  52.             case SWITCH_TWOACTIVITY:
  53.                     Intent intent=new Intent();
  54.                     intent.setClass(OneActivity.this,TwoActivity.class);
  55.                     OneActivity.this.startActivity(intent);
  56.                 //  OneActivity.this.finish();
  57.                 break;
  58.             case SWITCH_GUIDACTIVITY:
  59.                     Intent intents=new Intent();
  60.                     intents.setClass(OneActivity.this,MainActivity.class);
  61.                     OneActivity.this.startActivity(intents);
  62.                 //  OneActivity.this.finish();
  63.                     break;
  64.             }
  65.             super.handleMessage(msg);
  66.         };
  67.     };
  68. }

 

MainActivity的代码如下:

 

[java] view plaincopyprint?

  1. package cn.djy.activity;
  2. import java.util.ArrayList;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.os.Parcelable;
  8. import android.support.v4.view.PagerAdapter;
  9. import android.support.v4.view.ViewPager;
  10. import android.support.v4.view.ViewPager.OnPageChangeListener;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.view.ViewGroup;
  15. import android.view.ViewGroup.LayoutParams;
  16. import android.widget.Button;
  17. import android.widget.ImageView;
  18. import android.widget.TwoLineListItem;
  19. /**
  20.  * 实现
  21.  * @author dujinyang
  22.  *
  23.  */
  24. public class MainActivity extends Activity {
  25.     private ViewPager _viewpager;
  26.     /**
  27.      * 分页显示的View数组
  28.      */
  29.     private ArrayList<View> _pagerlist;
  30.     private ImageView image;
  31.     /**
  32.      * 小圆点的图片 放进数组
  33.      */
  34.     private ImageView[] _imageViews;
  35.     //包裹滑动的图片Linearlayout
  36.     private ViewGroup _group;
  37.     //包裹小圆点的Linearlayout
  38.     private ViewGroup _viewPonits;
  39.     /** Called when the activity is first created. */
  40.     @Override
  41.     public void onCreate(Bundle savedInstanceState) {
  42.         super.onCreate(savedInstanceState);
  43.         setContentView(R.layout.start);
  44.         LayoutInflater inflater=getLayoutInflater();
  45.         _pagerlist =new ArrayList<View>();
  46.         _pagerlist.add(inflater.inflate(R.layout.viewpager_page1,null));
  47.         _pagerlist.add(inflater.inflate(R.layout.viewpager_page2,null));
  48.         //创建Imageviews数组,大小是要显示的图片数量
  49.         _imageViews =new ImageView[_pagerlist.size()];
  50. //        ViewGroup
  51.         _group =(ViewGroup) inflater.inflate(R.layout.start, null);//加载MAIN.XML
  52.         _viewPonits=(ViewGroup) _group.findViewById(R.id.icon_circle);//加载小圆点的图片
  53.         _viewpager =(ViewPager) _group.findViewById(R.id.guidePages);//android.support.v4.view.ViewPager
  54.         //添加小圆点的图片
  55.         for (int i = 0; i < _imageViews.length; i++) {
  56.              image= new ImageView(this);
  57.              //设置小圆点的imageview的参数
  58.              image.setLayoutParams(new LayoutParams(20,20));//宽高均为20
  59.              image.setPadding(20, 0, 20, 0);
  60.              _imageViews[i]=image;
  61.              //第一张是选中状态
  62.              if(i==0){
  63.                  _imageViews[i].setBackgroundResource(R.drawable.a2a);
  64.              }else{
  65.                  _imageViews[i].setBackgroundResource(R.drawable.a1a);
  66.              }
  67.              _viewPonits.addView(_imageViews[i]);
  68.         }
  69.         //显示视图
  70.         setContentView(_group);
  71.         _viewpager.setAdapter(new GuidePageAdapter());
  72.         _viewpager.setOnPageChangeListener(new GuidePageChangeListener());
  73.     }
  74.     void setGuided(){
  75.         SharedPreferences shared=getSharedPreferences(“yang”,0);
  76.         SharedPreferences.Editor editor=shared.edit();
  77.         editor.putString(“Open”, “false”);
  78.         editor.commit();
  79.     }
  80.     class GuidePageAdapter extends PagerAdapter{
  81.         @Override
  82.         public int getCount() {
  83.             return _pagerlist.size();
  84.         }
  85.         //initalization position page
  86.         @Override
  87.         public Object instantiateItem(View container, int position) {
  88.             ((ViewPager)container).addView(_pagerlist.get(position));
  89.             if(position==1){
  90.                 Button bt=(Button) container.findViewById(R.id.bt_close);
  91.                 bt.setOnClickListener(new OnClickListener() {
  92.                     @Override
  93.                     public void onClick(View v) {
  94.                         //设置已经引导
  95.                         setGuided();
  96.                         //Intent
  97.                         Intent intent=new Intent();
  98.                         intent.setClass(MainActivity.this,TwoActivity.class );
  99.                         MainActivity.this.startActivity(intent);
  100.                         MainActivity.this.finish();
  101.                     }
  102.                 });
  103.             }
  104.             return _pagerlist.get(position);
  105.         }
  106.         //判断是否由对象生成页面
  107.         @Override
  108.         public boolean isViewFromObject(View view, Object obj) {
  109.             return view==obj;
  110.         }
  111.         //销毁页面的位置
  112.         @Override
  113.         public void destroyItem(View container, int position, Object object) {
  114.             ((ViewPager)container).removeView(_pagerlist.get(position));
  115.         }
  116.         @Override
  117.         public void finishUpdate(View container) {
  118.             //finish
  119.         }
  120.         @Override
  121.         public void startUpdate(View container) {
  122.             //start
  123.         }
  124.         @Override
  125.         public int getItemPosition(Object object) {
  126.             //getItemPosition
  127.             return super.getItemPosition(object);
  128.         }
  129.         @Override
  130.         public void restoreState(Parcelable state, ClassLoader loader) {
  131.             super.restoreState(state, loader);
  132.             //restore
  133.         }
  134.         @Override
  135.         public Parcelable saveState() {
  136.             return super.saveState();
  137.             //save
  138.         }
  139.     }
  140.     class GuidePageChangeListener implements OnPageChangeListener{
  141.         @Override
  142.         public void onPageScrollStateChanged(int arg0) {
  143.         }
  144.         @Override
  145.         public void onPageScrolled(int arg0, float arg1, int arg2) {
  146.         }
  147.         @Override
  148.         public void onPageSelected(int position) {
  149.             for (int i = 0; i < _imageViews.length; i++) {
  150.                     _imageViews[position].setBackgroundResource(R.drawable.a2a);
  151.                     if(position!=i)
  152.                         _imageViews[i].setBackgroundResource(R.drawable.a1a);
  153.             }
  154.         }
  155.     }
  156. }

 

 

 

最后是TWOActivity:这个Activity是显示的内容。

 

 

[java] view plaincopyprint?

  1. package cn.djy.activity;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class TwoActivity extends Activity{
  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.end);
  9.     }
  10. }

 

 

 

主要代码完成。

里面涉及的drawable图片是小圆点图片。

 

下面贴出XML的。

start.xml

 

[css] view plaincopyprint?

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <FrameLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:orientation=”vertical”
  4.     android:layout_width=”fill_parent”
  5.     android:layout_height=”fill_parent”
  6.     >
  7.      <LinearLayout android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” android:id=”@+id/linear_viewpager”>
  8.         <android.support.v4.view.ViewPager   android:id=”@+id/guidePages” android:layout_width=”fill_parent”  android:layout_height=”wrap_content”/>
  9.      </LinearLayout>
  10.      <LinearLayout android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” android:id=”@+id/linear_circle”>
  11.          <RelativeLayout android:layout_width=”fill_parent” android:layout_height=”wrap_content” >
  12.                 <LinearLayout android:id=”@+id/icon_circle”
  13.                               android:layout_width=”fill_parent”
  14.                               android:layout_height=”wrap_content”
  15.                               android:layout_marginBottom=”40dip”
  16.                               android:layout_alignParentBottom=”true”
  17.                               android:gravity=”center_vertical”
  18.                               android:orientation=”horizontal”/>
  19.          </RelativeLayout>
  20.      </LinearLayout>
  21. </FrameLayout>

 

 

其实是用Group把子项包起来作为显示:

 

那子项为:

 

viewpager_page1.xml

 

 

[css] view plaincopyprint?

  1. <LinearLayout
  2.   xmlns:android=”http://schemas.android.com/apk/res/android”
  3.   android:layout_width=”match_parent”
  4.   android:layout_height=”match_parent”  android:orientation=”vertical”>
  5.    <TextView android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:gravity=”center” android:text=”adfasfasfweqwwfqw'”
  6.    />
  7.  </LinearLayout>

 

viewpager_page2.xml

[css] view plaincopyprint?

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout
  3.   xmlns:android=”http://schemas.android.com/apk/res/android”
  4.   android:layout_width=”match_parent”
  5.   android:layout_height=”match_parent”
  6.   android:orientation=”vertical”
  7.   android:gravity=”center”>
  8.   <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”pages222222222222222222″></TextView>
  9.   <Button android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”asdfi14234124″ android:id=”@+id/bt_close”></Button>
  10. </LinearLayout>

 

总的代码完成。

希望大家互相学习 互相共享。

标签