当前位置:   article > 正文

android 给自己的app编写 用户引导(UserGuider)_产品的user_guide怎么做

产品的user_guide怎么做

    有些第三方应用(如QQ,微信等),安装后第一次使用都有个用户引导,提示用户怎么用及版本升级后新功能介绍。用 Hierarchy Viewer 工具看用户引导 是用哪些layout组成的。常见的QQ,微信 用的 Gallery,百度输入法用的是 FrameLayout。

    下面就以 Gallery 实现用户引导为例。

     先来图吧,有图有真相!

 

 

 

    这个熟悉吧,仿微信当前最新版的欢迎页。安装后第一次启动这个用户引导,本例用读写SharedPrefences判断是否第一次进(代码片段):

  1. boolean firstLogin = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(FLAG_FIRST_LOGIN, true);
  2. if (firstLogin) {
  3. startActivity(new Intent("com.xyz.guider.GuiderActivity"));
  4. }


    完成用户引导后把键值 first 写 false:

  1. SharedPreferences sp = PreferenceManager
  2. .getDefaultSharedPreferences(mContext);
  3. Editor edit = sp.edit();
  4. edit.putBoolean(FLAG_FIRST_LOGIN, false);
  5. edit.commit();


    下面来贴关键代码。

    用户引导布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/main_layout"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="@drawable/background_holo_dark" >
  7. <com.xyz.guider.XyzGallery
  8. android:id="@+id/what_news_gallery"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:clickable="true"
  12. android:focusable="true"
  13. android:spacing="0.0dip"
  14. android:unselectedAlpha="1.2" />
  15. <LinearLayout
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent"
  18. android:layout_gravity="center"
  19. android:background="@null" >
  20. <com.xyz.guider.PageControlView
  21. android:id="@+id/what_news_page_control"
  22. android:layout_width="fill_parent"
  23. android:layout_height="fill_parent"
  24. android:layout_marginBottom="17.5dip"
  25. android:background="@null"
  26. android:gravity="bottom|center" />
  27. </LinearLayout>
  28. <RelativeLayout
  29. xmlns:android="http://schemas.android.com/apk/res/android"
  30. android:id="@+id/mm_door"
  31. android:layout_width="fill_parent"
  32. android:layout_height="fill_parent"
  33. android:background="@drawable/background_holo_dark"
  34. android:orientation="horizontal"
  35. android:visibility="gone" >
  36. <ImageView
  37. android:id="@+id/mm_left"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_centerInParent="true"
  41. android:adjustViewBounds="true"
  42. android:scaleType="fitEnd"
  43. android:src="@drawable/whatsnew_08_01" />
  44. <ImageView
  45. android:id="@+id/mm_right"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_centerInParent="true"
  49. android:adjustViewBounds="true"
  50. android:scaleType="fitEnd"
  51. android:src="@drawable/whatsnew_08_02" />
  52. </RelativeLayout>
  53. </FrameLayout>



    首先继承个Gallery的类如XyzGallery:

  1. package com.xyz.guider;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.KeyEvent;
  5. import android.view.MotionEvent;
  6. import android.widget.Gallery;
  7. public class XyzGallery extends Gallery {
  8. public XyzGallery(Context context) {
  9. super(context);
  10. // TODO Auto-generated constructor stub
  11. setStaticTransformationsEnabled(true);
  12. }
  13. public XyzGallery(Context context, AttributeSet attrs) {
  14. super(context, attrs);
  15. // TODO Auto-generated constructor stub
  16. setStaticTransformationsEnabled(true);
  17. }
  18. public XyzGallery(Context context, AttributeSet attrs, int defStyle) {
  19. super(context, attrs, defStyle);
  20. // TODO Auto-generated constructor stub
  21. setStaticTransformationsEnabled(true);
  22. }
  23. @Override
  24. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  25. float velocityY) {
  26. // TODO Auto-generated method stub
  27. if (velocityX > 0) {
  28. onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
  29. } else {
  30. onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
  31. }
  32. return true;
  33. }
  34. }


    有了Gallery,还要个Gallery的适配器,作为GuiderActivity的内部类,GuiderActivity主要作用是呈现并控制 用户引导 界面:

  1. package com.xyz.guider;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.content.SharedPreferences.Editor;
  7. import android.os.Bundle;
  8. import android.preference.PreferenceManager;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.view.View.OnClickListener;
  13. import android.view.WindowManager;
  14. import android.view.animation.Animation;
  15. import android.view.animation.Animation.AnimationListener;
  16. import android.view.animation.AnimationUtils;
  17. import android.widget.AdapterView;
  18. import android.widget.AdapterView.OnItemSelectedListener;
  19. import android.widget.BaseAdapter;
  20. import android.widget.Button;
  21. import android.widget.ImageView;
  22. public class GuiderActivity extends Activity implements OnItemSelectedListener {
  23. private View mViewDoor = null;
  24. private ImageView mImageLeft = null;
  25. private ImageView mImageRight = null;
  26. private XyzGallery mGallery = null;
  27. private GalleryAdapter mAdapter = null;
  28. private PageControlView mIndicateView = null;
  29. private static final String FLAG_FIRST_LOGIN = "first";
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. // TODO Auto-generated method stub
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.whats_news);
  35. mGallery = (XyzGallery) findViewById(R.id.what_news_gallery);
  36. mAdapter = new GalleryAdapter(this);
  37. mGallery.setFadingEdgeLength(0);
  38. mGallery.setSpacing(-1);
  39. mGallery.setAdapter(mAdapter);
  40. mGallery.setOnItemSelectedListener(this);
  41. mIndicateView = (PageControlView) findViewById(R.id.what_news_page_control);
  42. mIndicateView.setIndication(mGallery.getCount(), 0);
  43. mViewDoor = findViewById(R.id.mm_door);
  44. mImageLeft = (ImageView) findViewById(R.id.mm_left);
  45. mImageRight = (ImageView) findViewById(R.id.mm_right);
  46. }
  47. private class GalleryAdapter extends BaseAdapter implements
  48. OnClickListener, AnimationListener {
  49. private Context mContext;
  50. private LayoutInflater mInfater = null;
  51. private int[] mLayouts = new int[] {
  52. R.layout.whats_news_gallery_fornew_one,
  53. R.layout.whats_news_gallery_fornew_two,
  54. R.layout.whats_news_gallery_fornew_three,
  55. R.layout.whats_news_gallery_fornew_four };
  56. public GalleryAdapter(Context ctx) {
  57. mContext = ctx;
  58. mInfater = (LayoutInflater) mContext
  59. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  60. }
  61. @Override
  62. public int getCount() {
  63. // TODO Auto-generated method stub
  64. return mLayouts.length;
  65. }
  66. @Override
  67. public Object getItem(int position) {
  68. // TODO Auto-generated method stub
  69. return Integer.valueOf(position);
  70. }
  71. @Override
  72. public long getItemId(int position) {
  73. // TODO Auto-generated method stub
  74. return position;
  75. }
  76. @Override
  77. public View getView(int position, View convertView, ViewGroup parent) {
  78. // TODO Auto-generated method stub
  79. if (convertView == null) {
  80. convertView = mInfater.inflate(mLayouts[position], null);
  81. if (position == 3) {
  82. Button btn = (Button) convertView
  83. .findViewById(R.id.whats_new_start_btn);
  84. btn.setOnClickListener(this);
  85. }
  86. }
  87. return convertView;
  88. }
  89. @Override
  90. public void onClick(View v) {
  91. // TODO Auto-generated method stub
  92. mViewDoor.setVisibility(View.VISIBLE);
  93. mImageLeft.startAnimation(setAnimation(R.anim.slide_left));
  94. mImageRight.startAnimation(setAnimation(R.anim.slide_right));
  95. }
  96. private Animation setAnimation(int resId) {
  97. Animation anim = AnimationUtils.loadAnimation(mContext, resId);
  98. anim.setAnimationListener(this);
  99. return anim;
  100. }
  101. @Override
  102. public void onAnimationStart(Animation animation) {
  103. // TODO Auto-generated method stub
  104. }
  105. @Override
  106. public void onAnimationEnd(Animation animation) {
  107. // TODO Auto-generated method stub
  108. mImageLeft.setVisibility(View.GONE);
  109. mImageRight.setVisibility(View.GONE);
  110. SharedPreferences sp = PreferenceManager
  111. .getDefaultSharedPreferences(mContext);
  112. Editor edit = sp.edit();
  113. edit.putBoolean(FLAG_FIRST_LOGIN, false);
  114. edit.commit();
  115. GuiderActivity.this.finish();
  116. }
  117. @Override
  118. public void onAnimationRepeat(Animation animation) {
  119. // TODO Auto-generated method stub
  120. }
  121. }
  122. @Override
  123. public void onItemSelected(AdapterView<?> parent, View view, int position,
  124. long id) {
  125. // TODO Auto-generated method stub
  126. if (mIndicateView != null) {
  127. mIndicateView.setIndication(parent.getCount(), position);
  128. }
  129. }
  130. @Override
  131. public void onNothingSelected(AdapterView<?> parent) {
  132. // TODO Auto-generated method stub
  133. }
  134. }


    还有个就是 用户引导 的指示器,共有几页,当前是那一页,实现起来很简单:

  1. package com.xyz.guider;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.ImageView;
  5. import android.widget.LinearLayout;
  6. public class PageControlView extends LinearLayout {
  7. private Context mContext;
  8. public PageControlView(Context ctx) {
  9. super(ctx);
  10. // TODO Auto-generated constructor stub
  11. mContext = ctx;
  12. }
  13. public PageControlView(Context ctx, AttributeSet attrs) {
  14. super(ctx, attrs);
  15. // TODO Auto-generated constructor stub
  16. mContext = ctx;
  17. }
  18. public void setIndication(int cnt, int index) {
  19. if (index < 0 || index > cnt)
  20. index = 0;
  21. removeAllViews();
  22. for (int i = 0; i < cnt; i++) {
  23. ImageView iv = new ImageView(mContext);
  24. iv.setImageResource(index == i ? R.drawable.page_indicator_focused
  25. : R.drawable.page_indicator_unfocused);
  26. if (i != 0 || i != cnt - 1) {
  27. iv.setPadding(4, 0, 4, 0);
  28. }
  29. addView(iv);
  30. }
  31. }
  32. }


     剩下的就是些xml文件,就不贴啦。

   免分下载原码路径:http://download.csdn.net/detail/zhouyuanjing/4866714

     ~~完~~


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/136723?site
推荐阅读
相关标签
  

闽ICP备14008679号