現在的安卓應用,如果是第一次安裝應用,首次啓動,進入應用後都會展示幾張功能引導圖。下面我們就來封裝實現這個常用的功能。
先列一下。我們實現的功能點。
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源碼。
下面是源碼效果圖.