首页 > 代码库 > 闪屏页+引导页

闪屏页+引导页

实现原理:

用SharedPreferences实现。
创建一个boolean的变量,默认值为true。
当判断这个变量是true的时候,说明是第一次运行,就跳转到另一个引导页面,否则进入主页面。

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

引导图效果用ViewPager实现。

下面附上代码:

splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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="com.zn.Activity.SplashActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="@drawable/w01"
android:scaleType="centerCrop" />

</RelativeLayout>

guid.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.zn.Activity.GuidActivity">
<android.support.v4.view.ViewPager
android:id="@+id/guid_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/viewgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38.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="http://www.mamicode.com/@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="http://www.mamicode.com/@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="http://www.mamicode.com/@drawable/dot" />
</LinearLayout>
</RelativeLayout>
引导页布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="false"
android:focusable="true"
android:scaleType="centerCrop"
android:background="@drawable/guide01"/>
</LinearLayout>
闪屏页activity:
package com.zn.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;

/**
* Created by admin on 2016/8/3.
*/
public class SplashActivity extends Activity {
boolean isFirstIn = false;
private static final int GO_HOME = 1000;
private static final int GO_GUIDE = 1001;

private static final long SPLASH_DELAY_MILLIS = 1000;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case GO_HOME:
goHome();
break;
case GO_GUIDE:
goGuide();
break;
}

super.handleMessage(msg);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去标题栏
setContentView(R.layout.activity_splash);
init();
}

private void init() {
// 读取SharedPreferences中需要的数据
// 使用SharedPreferences来记录程序的使用次数
SharedPreferences preferences = getSharedPreferences(
Icontes.SHAREDPREFERENCES_NAME, MODE_PRIVATE);

// 取得相应的值,如果没有该值,说明还未写入,用true作为默认值
isFirstIn = preferences.getBoolean("isFirstIn", true);
// 判断程序与第几次运行,如果是第一次运行则跳转到引导界面,否则跳转到主界面
if (!isFirstIn) {
// 使用Handler的postDelayed方法,1秒后执行跳转到MainActivity
mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
} else {
mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
}

}

private void goHome() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}

private void goGuide() {
Intent intent = new Intent(SplashActivity.this, GuidActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
}
引导页activity:
package com.zn.Activity;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zn.Adapter.ViewPagerApter;

import java.util.ArrayList;
import java.util.List;

public class GuidActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
private ViewPager viewPager;
private ViewPagerApter viewPagerApter;
private List<View> views;

//底部小圆点图片
private ImageView[] dots;
//记录当前选中的位置
private int currentIndex;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guid);

//初始化页面
initViews();
//初始化底部小圆点
initDots();

}

private void initViews(){
LayoutInflater inflater=LayoutInflater.from(this);
views=new ArrayList<View>();
//初始化引导页图片列表
views.add(inflater.inflate(R.layout.layout_item1,null));
views.add(inflater.inflate(R.layout.layout_item2,null));
views.add(inflater.inflate(R.layout.layout_item5,null));

//初始化Adapter
viewPagerApter=new ViewPagerApter(views,this);

viewPager= (ViewPager) findViewById(R.id.guid_viewpager);
viewPager.setAdapter(viewPagerApter);
//绑定回调
viewPager.setOnPageChangeListener(this);


}

private void initDots(){
LinearLayout linearLayout= (android.widget.LinearLayout) findViewById(R.id.viewgroup);
dots=new ImageView[views.size()];

//循环获取小点的图片
for (int i=0;i<views.size();i++){
dots[i]= (ImageView) linearLayout.getChildAt(i);
dots[i].setEnabled(true);//

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

}

private void setCurrentDot(int position){
if (position<0||position>views.size()-1||currentIndex==position){
return;

}
dots[position].setEnabled(false);
dots[currentIndex].setEnabled(true);
currentIndex=position;
}

//当前页面滑动时调用
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

//当前页面选中时调用
@Override
public void onPageSelected(int position) {
//设置底部小圆点选中状态
setCurrentDot(position);

}

//滑动状态改变时调用
@Override
public void onPageScrollStateChanged(int state) {

}
}
适配器:
package com.zn.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.zn.Activity.Icontes;
import com.zn.Activity.MainActivity;
import com.zn.Activity.R;

import java.util.List;

/**
* Created by admin on 2016/8/3.
*/
public class ViewPagerApter extends PagerAdapter {
private List<View> views;
private Activity activity;


public ViewPagerApter(List<View> views, Activity activity) {
this.views = views;
this.activity = activity;
}

//销毁position位置的界面
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView(views.get(position));
}

//获得当前的页面数
@Override
public int getCount() {
if (views != null) {
return views.size();
}
return 0;
}

@Override
public void finishUpdate(ViewGroup container) {

}

//初始化position的界面
@Override
public Object instantiateItem(ViewGroup container, int position) {
((ViewPager) container).addView(views.get(position), 0);
if (position == views.size() - 1) {
ImageView mStartWeiboImageButton = (ImageView) container.findViewById(R.id.iv_start_weibo);
mStartWeiboImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置已经引导
setGuided();
SharedPreferences preferences = activity.getSharedPreferences(
Icontes.SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("isFirstIn", false);
editor.commit();
goHome();
}
});
}
return views.get(position);
}

private void goHome() {
//跳转
Intent intent = new Intent(activity, MainActivity.class);
activity.startActivity(intent);
activity.finish();
}

private void setGuided() {
SharedPreferences preferences = activity.getSharedPreferences(Icontes.SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
//存入数据
editor.putBoolean("isFirstIn", false);
//提交修改
editor.commit();
}

//判断是否由对象生成界面
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {

}

@Override
public Parcelable saveState() {
return null;
}

@Override
public void startUpdate(ViewGroup container) {


}
}小圆点xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/dark_dot" android:state_enabled="true"/>
<item android:drawable="@drawable/white_dot" android:state_enabled="false"/>

</selector>

开始体验按钮:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/tiyan" />
<item android:drawable="@drawable/tiyan" />
</selector>



闪屏页+引导页