Android开发 实现一般应用常有的 首次启动 展示引导图功能
现在的安卓应用,如果是第一次安装应用,首次启动,进入应用后都会展示几张功能引导图。下面我们就来封装实现这个常用的功能。
先列一下。我们实现的功能点。
1.用户首次安装应用,启动应用,进入首页时,要显示我们的引导遮罩。
2.如果是已经看过引导图的用户(比如他是第二次启动),就不展示了
3.如果我们应用在1.0版本时,需要展示3个引导图,2.0版本时,需要展示五张新图。这个时候,如果用户非首次使用应用(如,他使用过1.0版本了),这个时候也要提示2.0新的引导图。就是引导图也有版本记录。
下面把关键类贴出来做一个大体说明:
- /**
- * @Description: 引导图助手类
- * @author yanzw
- * @date 2012-11-30上午11:12:46
- */
- public class GuideHelper {
- private Activity context;
- private ViewGroup rootLayout;
- private ScrollLayout scrollLayout;
- private int[] guideResIds = {R.drawable.guide_help1, R.drawable.guide_help2, R.drawable.guide_help3, R.drawable.guide_help4};
- private static final String GUIDE_VERSION_NAME = "GUIDEVERSION";
- private static final int GUIDE_VERSION_CODE = 2;
- public GuideHelper(Context context){
- this.context = (Activity)context;
- createGuideLayout();
- initGuideView();
- }
- /**
- * @Description: 创建引导图层
- * @param @return
- * @return ViewGroup
- * @throws
- */
- private void createGuideLayout(){
- ViewGroup rootView = (ViewGroup) context.getWindow().getDecorView();
- LayoutInflater lf = context.getLayoutInflater();
- rootLayout = (ViewGroup) lf.inflate(R.layout.guide_helper, null);
- scrollLayout = (ScrollLayout) rootLayout.findViewById(R.id.scroll_layout);
- rootView.addView(rootLayout);
- }
- /**
- * @Description: 初始化引导视图
- * @param
- * @return void
- * @throws
- */
- public void initGuideView() {
- for(int resId : guideResIds){
- scrollLayout.addView(makeGuideView(resId));
- }
- View top_right_btn = rootLayout.findViewById(R.id.top_right_btn);
- top_right_btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- closeGuide();
- }
- });
- scrollLayout.setPageEndListener(new PageEndListener(){
- @Override
- public void scrollEnd() {
- closeGuide();
- }
- });
- }
- /**
- * @Description: 生成每个引导视图
- * @param @param resId
- * @param @return
- * @return View
- * @throws
- */
- public View makeGuideView(int resId){
- ImageView guideView = new ImageView(context);
- guideView.setImageResource(resId);
- guideView.setPadding(10, 10, 10, 10);
- return guideView;
- }
- /**
- * @Description: 打开引导层
- * @param
- * @return void
- * @throws
- */
- public void openGuide(){
- if(guideCheck()){
- rootLayout.setVisibility(View.VISIBLE);
- }
- }
- /**
- * @Description: 关闭引导层
- * @param
- * @return void
- * @throws
- */
- public void closeGuide(){
- AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.2f);
- alphaAnim.setDuration(500);
- ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- scaleAnim.setDuration(500);
- AnimationSet AnimSet = new AnimationSet(false);
- AnimSet.setDuration(500);
- AnimSet.addAnimation(scaleAnim);
- AnimSet.addAnimation(alphaAnim);
- AnimSet.setAnimationListener(new AnimationListener(){
- @Override
- public void onAnimationStart(Animation animation) {
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- rootLayout.clearAnimation();
- rootLayout.setVisibility(View.GONE);
- saveGuideVersion();
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
- });
- rootLayout.startAnimation(AnimSet);
- }
- /**
- * @Description: 保存引导版本记录
- * @param
- * @return void
- * @throws
- */
- private void saveGuideVersion() {
- SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
- Editor edit = sp.edit();
- edit.putInt(GUIDE_VERSION_NAME, GUIDE_VERSION_CODE);
- edit.commit();
- }
- /**
- * @Description: 检测引导图版本,判断是否启动引导
- * @param @return
- * @return boolean
- * @throws
- */
- private boolean guideCheck(){
- SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
- int guideVer = sp.getInt(GUIDE_VERSION_NAME, 0);
- if(GUIDE_VERSION_CODE > 0 && GUIDE_VERSION_CODE > guideVer){
- return true;
- } else {
- return false;
- }
- }
- }
调用代码:
- GuideHelper guideHelper = new GuideHelper(this);
- guideHelper.openGuide();
代码基本说明上面都有了。是不是很简单啊。哈哈,用最简单的方法去完成功能,就OK了。还看不懂的评论提问吧。
还是按老规矩,附上DOME源码。
下面是源码效果图.
转载于:https://blog.51cto.com/stars/1075285