Android应用程序实现欢迎引导页面的方法实现

现在的大多数应用都会有一个欢迎引导页面,

需求分析:

程序安装后第一次启动:

启动页-->功能引导页-->应用主页

以后启动:

启动页-->应用主页

实现原理:

用SharedPreferences实现。

创建一个boolean的变量,默认值为true。

当判断这个变量是true的时候,说明是第一次运行,就跳转到另一个引导页面。

引导页面跳转到最后一张图片时,点击某按钮发生跳转事件,回到MainActivity,此时把变量的值改成false。

引导图效果用ViewPager可以很轻松的实现。

1.布局文件

splash.xml:

<RelativeLayoutxmlns: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"

tools:context=".SplashActivity">

<ImageView

android:layout_width="match_parent"

android:layout_height="match_parent"

android:adjustViewBounds="true"

android:background="@drawable/welcome_android"

android:scaleType="centerCrop"/>

</RelativeLayout>

复制代码

guide.xml:

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<android.support.v4.view.ViewPager

android:id="@+id/viewpager"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

<LinearLayout

android:id="@+id/ll"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:layout_marginBottom="24.0dp"

android:orientation="horizontal">

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:clickable="true"

android:padding="15.0dip"

android:src="@drawable/dot"/>

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:clickable="true"

android:padding="15.0dip"

android:src="@drawable/dot"/>

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:clickable="true"

android:padding="15.0dip"

android:src="@drawable/dot"/>

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:clickable="true"

android:padding="15.0dip"

android:src="@drawable/dot"/>

</LinearLayout>

</RelativeLayout>

复制代码

main_activity,.xml默认

what_new_one.xml、what_new_two.xml、what_new_three.xml(将图片名称改下就行了):

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<ImageView

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_centerInParent="true"

android:adjustViewBounds="false"

android:focusable="true"

android:scaleType="centerCrop"

android:background="@drawable/guide_350_01"/>

</RelativeLayout>

复制代码

what_new_four.xml:

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<ImageView

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_centerInParent="true"

android:adjustViewBounds="false"

android:background="@drawable/guide_350_04"

android:focusable="true"

android:scaleType="centerCrop"/>

<ImageView

android:id="@+id/iv_start_weibo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:layout_marginBottom="108dp"

android:background="@drawable/whats_new_start_btn"

android:focusable="true"/>

</RelativeLayout>

复制代码

MainActivity.java不变

SplashActivity.java:

packagecn.eoe.leigo.splash;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.content.SharedPreferences;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

/**

*

*@{#SplashActivity.javaCreateon2013-5-2下午9:10:01

*

*classdesc:启动画面(1)判断是否是首次加载应用--采取读取SharedPreferences的方法

*(2)是,则进入GuideActivity;否,则进入MainActivity(3)3s后执行(2)操作

*

*<p>

*Copyright:Copyright(c)2013

*</p>

*@Version1.0

*@Author<ahref="mailto:gaolei_xj@163.com">Leo</a>

*

*

*/

publicclassSplashActivityextendsActivity{

booleanisFirstIn=false;

privatestaticfinalintGO_HOME=1000;

privatestaticfinalintGO_GUIDE=1001;

//延迟3秒

privatestaticfinallongSPLASH_DELAY_MILLIS=3000;

privatestaticfinalStringSHAREDPREFERENCES_name="first_pref";

/**

*Handler:跳转到不同界面

*/

privateHandlermHandler=newHandler(){

@Override

publicvoidhandleMessage(Messagemsg){

switch(msg.what){

caseGO_HOME:

goHome();

break;

caseGO_GUIDE:

goGuide();

break;

}

super.handleMessage(msg);

}

};

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.splash);

init();

}

privatevoidinit(){

//读取SharedPreferences中需要的数据

//使用SharedPreferences来记录程序的使用次数

SharedPreferencespreferences=getSharedPreferences(

SHAREDPREFERENCES_NAME,MODE_PRIVATE);

//取得相应的值,如果没有该值,说明还未写入,用true作为默认值

isFirstIn=preferences.getBoolean("isFirstIn",true);

//判断程序与第几次运行,如果是第一次运行则跳转到引导界面,否则跳转到主界面

if(!isFirstIn){

//使用Handler的postDelayed方法,3秒后执行跳转到MainActivity

mHandler.sendEmptyMessageDelayed(GO_HOME,SPLASH_DELAY_MILLIS);

}else{

mHandler.sendEmptyMessageDelayed(GO_GUIDE,SPLASH_DELAY_MILLIS);

}

}

privatevoidgoHome(){

Intentintent=newIntent(SplashActivity.this,MainActivity.class);

SplashActivity.this.startActivity(intent);

SplashActivity.this.finish();

}

privatevoidgoGuide(){

Intentintent=newIntent(SplashActivity.this,GuideActivity.class);

SplashActivity.this.startActivity(intent);

SplashActivity.this.finish();

}

}

复制代码

GuideActivity.java:

packagecn.eoe.leigo.splash;

importjava.util.ArrayList;

importjava.util.List;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.support.v4.view.ViewPager;

importandroid.support.v4.view.ViewPager.OnPageChangeListener;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.widget.ImageView;

importandroid.widget.LinearLayout;

importcn.eoe.leigo.splash.adapter.ViewPagerAdapter;

/**

*

*@{#GuideActivity.javaCreateon2013-5-2下午10:59:08

*

*classdesc:引导界面

*

*<p>

*Copyright:Copyright(c)2013

*</p>

*@Version1.0

*@Author<ahref="mailto:gaolei_xj@163.com">Leo</a>

*

*

*/

publicclassGuideActivityextendsActivityimplementsOnPageChangeListener{

privateViewPagervp;

privateViewPagerAdaptervpAdapter;

privateList<View>views;

//底部小点图片

privateImageView[]dots;

//记录当前选中位置

privateintcurrentIndex;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.guide);

//初始化页面

initViews();

//初始化底部小点

initDots();

}

privatevoidinitViews(){

LayoutInflaterinflater=LayoutInflater.from(this);

views=newArrayList<View>();

//初始化引导图片列表

views.add(inflater.inflate(R.layout.what_new_one,null));

views.add(inflater.inflate(R.layout.what_new_two,null));

views.add(inflater.inflate(R.layout.what_new_three,null));

views.add(inflater.inflate(R.layout.what_new_four,null));

//初始化Adapter

vpAdapter=newViewPagerAdapter(views,this);

vp=(ViewPager)findViewById(R.id.viewpager);

vp.setAdapter(vpAdapter);

//绑定回调

vp.setOnPageChangeListener(this);

}

privatevoidinitDots(){

LinearLayoutll=(LinearLayout)findViewById(R.id.ll);

dots=newImageView[views.size()];

//循环取得小点图片

for(inti=0;i<views.size();i++){

dots[i]=(ImageView)ll.getChildAt(i);

dots[i].setEnabled(true);//都设为灰色

}

currentIndex=0;

dots[currentIndex].setEnabled(false);//设置为白色,即选中状态

}

privatevoidsetCurrentDot(intposition){

if(position<0||position>views.size()-1

||currentIndex==position){

return;

}

dots[position].setEnabled(false);

dots[currentIndex].setEnabled(true);

currentIndex=position;

}

//当滑动状态改变时调用

@Override

publicvoidonPageScrollStateChanged(intarg0){

}

//当当前页面被滑动时调用

@Override

publicvoidonPageScrolled(intarg0,floatarg1,intarg2){

}

//当新的页面被选中时调用

@Override

publicvoidonPageSelected(intarg0){

//设置底部小点选中状态

setCurrentDot(arg0);

}

}

复制代码

ViewPagerAdapter.java:

packagecn.eoe.leigo.splash.adapter;

importjava.util.List;

importandroid.app.Activity;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.content.SharedPreferences;

importandroid.content.SharedPreferences.Editor;

importandroid.os.Parcelable;

importandroid.support.v4.view.PagerAdapter;

importandroid.support.v4.view.ViewPager;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.ImageView;

importcn.eoe.leigo.splash.MainActivity;

importcn.eoe.leigo.splash.R;

/**

*

*@{#ViewPagerAdapter.javaCreateon2013-5-2下午11:03:39

*

*classdesc:引导页面适配器

*

*<p>

*Copyright:Copyright(c)2013

*</p>

*@Version1.0

*@Author<ahref="mailto:gaolei_xj@163.com">Leo</a>

*

*

*/

publicclassViewPagerAdapterextendsPagerAdapter{

//界面列表

privateList<View>views;

privateActivityactivity;

privatestaticfinalStringSHAREDPREFERENCES_name="first_pref";

publicViewPagerAdapter(List<View>views,Activityactivity){

this.views=views;

this.activity=activity;

}

//销毁arg1位置的界面

@Override

publicvoiddestroyItem(Viewarg0,intarg1,Objectarg2){

((ViewPager)arg0).removeView(views.get(arg1));

}

@Override

publicvoidfinishUpdate(Viewarg0){

}

//获得当前界面数

@Override

publicintgetCount(){

if(views!=null){

returnviews.size();

}

return0;

}

//初始化arg1位置的界面

@Override

publicObjectinstantiateItem(Viewarg0,intarg1){

((ViewPager)arg0).addView(views.get(arg1),0);

if(arg1==views.size()-1){

ImageViewmStartWeiboImageButton=(ImageView)arg0

.findViewById(R.id.iv_start_weibo);

mStartWeiboImageButton.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewv){

//设置已经引导

setGuided();

goHome();

}

});

}

returnviews.get(arg1);

}

privatevoidgoHome(){

//跳转

Intentintent=newIntent(activity,MainActivity.class);

activity.startActivity(intent);

activity.finish();

}

/**

*

*methoddesc:设置已经引导过了,下次启动不用再次引导

*/

privatevoidsetGuided(){

SharedPreferencespreferences=activity.getSharedPreferences(

SHAREDPREFERENCES_NAME,Context.MODE_PRIVATE);

Editoreditor=preferences.edit();

//存入数据

editor.putBoolean("isFirstIn",false);

//提交修改

editor.commit();

}

//判断是否由对象生成界面

@Override

publicbooleanisViewFromObject(Viewarg0,Objectarg1){

return(arg0==arg1);

}

@Override

publicvoidrestoreState(Parcelablearg0,ClassLoaderarg1){

}

@Override

publicParcelablesaveState(){

returnnull;

}

@Override

publicvoidstartUpdate(Viewarg0){

}

}

复制代码

相关推荐