Android開發 實現一般應用常有的 首次啓動 展示引導圖功能

現在的安卓應用,如果是第一次安裝應用,首次啓動,進入應用後都會展示幾張功能引導圖。下面我們就來封裝實現這個常用的功能。

先列一下。我們實現的功能點。

1.用戶首次安裝應用,啓動應用,進入首頁時,要顯示我們的引導遮罩。

2.如果是已經看過引導圖的用戶(比如他是第二次啓動),就不展示了

3.如果我們應用在1.0版本時,需要展示3個引導圖,2.0版本時,需要展示五張新圖。這個時候,如果用戶非首次使用應用(如,他使用過1.0版本了),這個時候也要提示2.0新的引導圖。就是引導圖也有版本記錄。

 

下面把關鍵類貼出來做一個大體說明:

 

  1. /** 
  2.  * @Description: 引導圖助手類 
  3.  * @author yanzw 
  4.  * @date 2012-11-30上午11:12:46 
  5.  */ 
  6. public class GuideHelper { 
  7.     private Activity context; 
  8.     private ViewGroup rootLayout; 
  9.     private ScrollLayout scrollLayout; 
  10.     private int[] guideResIds = {R.drawable.guide_help1, R.drawable.guide_help2, R.drawable.guide_help3, R.drawable.guide_help4}; 
  11.     private static final String GUIDE_VERSION_NAME = "GUIDEVERSION"
  12.     private static final int  GUIDE_VERSION_CODE = 2
  13.      
  14.     public GuideHelper(Context context){ 
  15.         this.context = (Activity)context; 
  16.         createGuideLayout(); 
  17.         initGuideView(); 
  18.     } 
  19.      
  20.     /** 
  21.      * @Description: 創建引導圖層 
  22.      * @param @return 
  23.      * @return ViewGroup 
  24.      * @throws 
  25.      */ 
  26.     private void createGuideLayout(){ 
  27.         ViewGroup rootView = (ViewGroup) context.getWindow().getDecorView(); 
  28.         LayoutInflater lf = context.getLayoutInflater(); 
  29.         rootLayout = (ViewGroup) lf.inflate(R.layout.guide_helper, null); 
  30.         scrollLayout = (ScrollLayout) rootLayout.findViewById(R.id.scroll_layout); 
  31.         rootView.addView(rootLayout); 
  32.     } 
  33.      
  34.     /** 
  35.      * @Description: 初始化引導視圖 
  36.      * @param  
  37.      * @return void 
  38.      * @throws 
  39.      */ 
  40.     public void initGuideView() { 
  41.         for(int resId : guideResIds){ 
  42.             scrollLayout.addView(makeGuideView(resId)); 
  43.         } 
  44.         View top_right_btn = rootLayout.findViewById(R.id.top_right_btn); 
  45.         top_right_btn.setOnClickListener(new View.OnClickListener() { 
  46.             @Override 
  47.             public void onClick(View v) { 
  48.                 closeGuide(); 
  49.             } 
  50.         }); 
  51.         scrollLayout.setPageEndListener(new PageEndListener(){ 
  52.  
  53.             @Override 
  54.             public void scrollEnd() { 
  55.                 closeGuide(); 
  56.             } 
  57.              
  58.         }); 
  59.     } 
  60.      
  61.     /** 
  62.      * @Description: 生成每個引導視圖 
  63.      * @param @param resId 
  64.      * @param @return 
  65.      * @return View 
  66.      * @throws 
  67.      */ 
  68.     public View makeGuideView(int resId){ 
  69.         ImageView guideView = new ImageView(context); 
  70.         guideView.setImageResource(resId); 
  71.         guideView.setPadding(10101010); 
  72.         return guideView; 
  73.     } 
  74.      
  75.     /** 
  76.      * @Description: 打開引導層 
  77.      * @param  
  78.      * @return void 
  79.      * @throws 
  80.      */ 
  81.     public void openGuide(){ 
  82.         if(guideCheck()){ 
  83.             rootLayout.setVisibility(View.VISIBLE); 
  84.         } 
  85.     } 
  86.      
  87.     /** 
  88.      * @Description: 關閉引導層 
  89.      * @param  
  90.      * @return void 
  91.      * @throws 
  92.      */ 
  93.     public void closeGuide(){ 
  94.          
  95.         AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.2f); 
  96.         alphaAnim.setDuration(500); 
  97.         ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
  98.         scaleAnim.setDuration(500); 
  99.         AnimationSet AnimSet = new AnimationSet(false); 
  100.         AnimSet.setDuration(500); 
  101.         AnimSet.addAnimation(scaleAnim); 
  102.         AnimSet.addAnimation(alphaAnim); 
  103.          
  104.         AnimSet.setAnimationListener(new AnimationListener(){ 
  105.  
  106.             @Override 
  107.             public void onAnimationStart(Animation animation) { 
  108.                  
  109.             } 
  110.  
  111.             @Override 
  112.             public void onAnimationEnd(Animation animation) { 
  113.                 rootLayout.clearAnimation(); 
  114.                 rootLayout.setVisibility(View.GONE); 
  115.                 saveGuideVersion(); 
  116.             } 
  117.  
  118.             @Override 
  119.             public void onAnimationRepeat(Animation animation) { 
  120.                  
  121.             } 
  122.              
  123.         }); 
  124.          
  125.         rootLayout.startAnimation(AnimSet); 
  126.     } 
  127.      
  128.     /** 
  129.      * @Description: 保存引導版本記錄 
  130.      * @param  
  131.      * @return void 
  132.      * @throws 
  133.      */ 
  134.     private void saveGuideVersion() { 
  135.         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
  136.         Editor edit = sp.edit(); 
  137.         edit.putInt(GUIDE_VERSION_NAME, GUIDE_VERSION_CODE); 
  138.         edit.commit(); 
  139.     } 
  140.      
  141.     /** 
  142.      * @Description: 檢測引導圖版本,判斷是否啓動引導 
  143.      * @param @return 
  144.      * @return boolean 
  145.      * @throws 
  146.      */ 
  147.     private boolean guideCheck(){ 
  148.         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
  149.         int guideVer = sp.getInt(GUIDE_VERSION_NAME, 0); 
  150.         if(GUIDE_VERSION_CODE > 0 && GUIDE_VERSION_CODE > guideVer){ 
  151.             return true
  152.         } else { 
  153.             return false
  154.         } 
  155.     } 

調用代碼:

  1. GuideHelper guideHelper = new GuideHelper(this); 
  2. guideHelper.openGuide(); 

代碼基本說明上面都有了。是不是很簡單啊。哈哈,用最簡單的方法去完成功能,就OK了。還看不懂的評論提問吧。

還是按老規矩,附上DOME源碼。

下面是源碼效果圖.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章