Android应用程序实现欢迎引导页面的方法实现
来源:互联网 发布:java外包公司有前途吗 编辑:程序博客网 时间:2024/05/12 11:43
作者:gaolei_xj更新于 05月04日访问(2064)评论(31)
现在的大多数应用都会有一个欢迎引导页面,
需求分析:
程序安装后第一次启动:
启动页-->功能引导页-->应用主页
以后启动:
启动页-->应用主页
实现原理:
用SharedPreferences实现。
创建一个boolean的变量,默认值为true。
当判断这个变量是true的时候,说明是第一次运行,就跳转到另一个引导页面。
引导页面跳转到最后一张图片时,点击某按钮发生跳转事件,回到MainActivity,此时把变量的值改成false。
引导图效果用ViewPager可以很轻松的实现。
1.布局文件
splash.xml:
1 2 3 4 5 6 7 8 91011121314
<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=".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:
1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
<?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: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(将图片名称改下就行了):
1 2 3 4 5 6 7 8 9101112131415
<?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:layout_centerInParent="true" android:adjustViewBounds="false" android:focusable="true" android:scaleType="centerCrop" android:background="@drawable/guide_350_01" /></RelativeLayout>
what_new_four.xml:
1 2 3 4 5 6 7 8 910111213141516171819202122232425
<?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: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:
1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
package cn.eoe.leigo.splash;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.os.Handler;import android.os.Message;/** * * @{# SplashActivity.java Create on 2013-5-2 下午9:10:01 * * class desc: 启动画面 (1)判断是否是首次加载应用--采取读取SharedPreferences的方法 * (2)是,则进入GuideActivity;否,则进入MainActivity (3)3s后执行(2)操作 * * <p> * Copyright: Copyright(c) 2013 * </p> * @Version 1.0 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a> * * */public class SplashActivity extends Activity { boolean isFirstIn = false; private static final int GO_HOME = 1000; private static final int GO_GUIDE = 1001; // 延迟3秒 private static final long SPLASH_DELAY_MILLIS = 3000; private static final String SHAREDPREFERENCES_NAME = "first_pref"; /** * Handler:跳转到不同界面 */ 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中需要的数据 // 使用SharedPreferences来记录程序的使用次数 SharedPreferences preferences = 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); } } 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, GuideActivity.class); SplashActivity.this.startActivity(intent); SplashActivity.this.finish(); }}
GuideActivity.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117
package cn.eoe.leigo.splash;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;import cn.eoe.leigo.splash.adapter.ViewPagerAdapter;/** * * @{# GuideActivity.java Create on 2013-5-2 下午10:59:08 * * class desc: 引导界面 * * <p> * Copyright: Copyright(c) 2013 * </p> * @Version 1.0 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a> * * */public class GuideActivity extends Activity implements 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(); // 初始化底部小点 initDots(); } private void initViews() { LayoutInflater inflater = LayoutInflater.from(this); views = new ArrayList<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 = new ViewPagerAdapter(views, this); vp = (ViewPager) findViewById(R.id.viewpager); vp.setAdapter(vpAdapter); // 绑定回调 vp.setOnPageChangeListener(this); } private void initDots() { LinearLayout ll = (LinearLayout) findViewById(R.id.ll); dots = new ImageView[views.size()]; // 循环取得小点图片 for (int i = 0; i < views.size(); i++) { dots[i] = (ImageView) ll.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 onPageScrollStateChanged(int arg0) { } // 当当前页面被滑动时调用 @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } // 当新的页面被选中时调用 @Override public void onPageSelected(int arg0) { // 设置底部小点选中状态 setCurrentDot(arg0); }}
ViewPagerAdapter.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127
package cn.eoe.leigo.splash.adapter;import java.util.List;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Parcelable;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import cn.eoe.leigo.splash.MainActivity;import cn.eoe.leigo.splash.R;/** * * @{# ViewPagerAdapter.java Create on 2013-5-2 下午11:03:39 * * class desc: 引导页面适配器 * * <p> * Copyright: Copyright(c) 2013 * </p> * @Version 1.0 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a> * * */public class ViewPagerAdapter extends PagerAdapter { // 界面列表 private List<View> views; private Activity activity; private static final String SHAREDPREFERENCES_NAME = "first_pref"; public ViewPagerAdapter(List<View> views, Activity activity) { this.views = views; this.activity = activity; } // 销毁arg1位置的界面 @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView(views.get(arg1)); } @Override public void finishUpdate(View arg0) { } // 获得当前界面数 @Override public int getCount() { if (views != null) { return views.size(); } return 0; } // 初始化arg1位置的界面 @Override public Object instantiateItem(View arg0, int arg1) { ((ViewPager) arg0).addView(views.get(arg1), 0); if (arg1 == views.size() - 1) { ImageView mStartWeiboImageButton = (ImageView) arg0 .findViewById(R.id.iv_start_weibo); mStartWeiboImageButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 设置已经引导 setGuided(); goHome(); } }); } return views.get(arg1); } private void goHome() { // 跳转 Intent intent = new Intent(activity, MainActivity.class); activity.startActivity(intent); activity.finish(); } /** * * method desc:设置已经引导过了,下次启动不用再次引导 */ private void setGuided() { SharedPreferences preferences = activity.getSharedPreferences( SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE); Editor editor = preferences.edit(); // 存入数据 editor.putBoolean("isFirstIn", false); // 提交修改 editor.commit(); } // 判断是否由对象生成界面 @Override public boolean isViewFromObject(View arg0, Object arg1) { return (arg0 == arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { }}
- Android应用程序实现欢迎引导页面的方法实现
- Android应用程序实现欢迎引导页面的方法实现
- Android应用程序实现欢迎引导页面的方法实现
- Android应用程序实现欢迎引导页面的方法实现
- Android应用程序实现欢迎引导页面的方法实现
- Android应用程序实现欢迎引导页面的方法实现
- Android应用程序实现欢迎引导页面的方法实现
- Android应用程序实现欢迎引导页面的方法实现
- Android应用程序实现欢迎引导页面的方法实现
- Android应用程序实现欢迎引导页面的方法实现
- Android之ViewPager实现欢迎引导页面
- unity3d for Android实现欢迎引导页面的功能
- Android引导页面的实现
- ViewPager实现欢迎引导页面
- ViewPage实现欢迎引导页面
- android UI进阶之用ViewPager实现欢迎引导页面
- android UI进阶之用ViewPager实现欢迎引导页面
- android UI进阶之用ViewPager实现欢迎引导页面
- shell 间接引用(indirect reference) \$${a} 和 ${!a}
- CentOS防火墙开启80端口
- VPS是什么意思
- android命令input的用法
- 由异或实现两个数的交换到RAID技术的数据恢复
- Android应用程序实现欢迎引导页面的方法实现
- oracle问题解决办法(转)
- 命名空间“Microsoft”中不存在类型或命名空间名称“Office ”(是缺少程序集引用吗?)
- 下楼梯
- ubuntu用户操作
- PL/SQL Developer结合oracle精简客户端使用配置
- 线程状态的转换
- 黑马程序员——视图
- D-BUS基础