您的位置:首页 > 移动开发 > Android开发

Android欢迎页+引导页实现

2015-09-15 19:37 507 查看
需求分析:

程序安装后第一次启动:

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

以后启动:

启动页–>应用主页

实现原理:

用SharedPreferences实现。

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

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

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

SplashFrame.java启动页

public class SplashFrame 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 = 3000;
public static final String SHAREDPREFERENCES_NAME = "first_pref";
public static final String IS_FIRST_IN="isFirstIn";
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);
setContentView(R.layout.splash);
init();
}

private void init() {
SharedPreferences preferences = getSharedPreferences(SHAREDPREFERENCES_NAME, MODE_PRIVATE);
isFirstIn = preferences.getBoolean(IS_FIRST_IN, true);
if (isFirstIn) {
mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);

} else {
mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
}
}

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

private void goGuide() {
Intent intent = new Intent(SplashFrame.this, GuideFrame.class);
SplashFrame.this.startActivity(intent);
SplashFrame.this.finish();
}
}


splash.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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:adjustViewBounds="true"
android:background="@drawable/main_preview_loading"
android:scaleType="centerCrop"/>
</RelativeLayout>


GuideFrame.java引导页

public class GuideFrame extends AActivity implements ViewPager.OnPageChangeListener {
private ViewPager vp;
private ViewPagerAdapter vpAdapter;
private List<View> views;
// 底部小点图片
private ImageView[] dots;
// 记录当前选中位置
private int currentIndex;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide);
// 初始化页面
initViews();
}

private void initViews() {
//引导图片资源
int[] pics = {R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3, R.drawable.guide_4};
//引导图背景颜色
int[] bgColor = {R.color.guide_bg_1, R.color.guide_bg_2, R.color.guide_bg_3, R.color.guide_bg_4};
views = new ArrayList<View>();
for (int i = 0; i < pics.length; i++) {
// 初始化引导图片列表
View view = LayoutInflater.from(this).inflate(R.layout.guide_pager, null);
view.setBackgroundResource(bgColor[i]);
ImageView guideImage = (ImageView) view.findViewById(R.id.guide_image);
ImageView skip = (ImageView) view.findViewById(R.id.skip);
ImageView inTo = (ImageView) view.findViewById(R.id.iv_start);
guideImage.setImageResource(pics[i]);

if (i == pics.length - 1) {
skip.setVisibility(View.INVISIBLE);
inTo.setVisibility(View.VISIBLE);

} else {
skip.setVisibility(View.VISIBLE);
inTo.setVisibility(View.INVISIBLE);
}
views.add(view);
}

// 初始化Adapter
vpAdapter = new ViewPagerAdapter(views, this);

vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(vpAdapter);
// 绑定回调
vp.addOnPageChangeListener(this);
// 初始化底部小点
initDots();
}

private void initDots() {
LinearLayout dotLayout = (LinearLayout) findViewById(R.id.dot_layout);

dots = new ImageView[views.size()];
// 循环取得小点图片
for (int i = 0; i < views.size(); i++) {
dots[i] = (ImageV
d9c2
iew) dotLayout.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) {

}

class ViewPagerAdapter extends PagerAdapter {
//引导界面列表
private List<View> views;
private Activity activity;

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

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

//当前界面数
@Override
public int getCount() {
if (views != null) {
return views.size();
}

return 0;
}

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

// 初始化position位置的界面
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(views.get(position), 0);
if (position != views.size() - 1) {
ImageView imageView = (ImageView) container.findViewById(R.id.skip);
imageOnclick(imageView);
} else {
ImageView iv = (ImageView) container.findViewById(R.id.iv_start);
imageOnclick(iv);
}

return views.get(position);
}

private void imageOnclick(ImageView view) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goHome();
//设置已经引导
setGuided();
}
});
}

private void goHome() {
Intent intent = new Intent(activity, MainFrame.class);
activity.startActivity(intent);
activity.finish();
}

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


guide.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/dot_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:orientation="horizontal">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="11dip"
android:src="@drawable/dot"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="11dip"
android:src="@drawable/dot"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="11dip"
android:src="@drawable/dot"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="11dip"
android:src="@drawable/dot"/>
</LinearLayout>
</RelativeLayout>


guide_one.xml布局………

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/guide_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:adjustViewBounds="false"
android:focusable="true"
android:scaleType="centerInside"
android:src="@drawable/guide_1"/>

<ImageView
android:id="@+id/skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/hall_info_padding"
android:layout_marginTop="@dimen/hall_info_padding"
android:src="@drawable/skip"/>

<ImageView
android:id="@+id/iv_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:src="@drawable/into"/>
</RelativeLayout>


最后不要忘记在清单文件中注册activity。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 应用