标签:
注册的时候,偶尔会遇到如下图的场景:
上图涉及到的概念就是“分步注册”。
为什么要分步注册?分步注册有什么好处?问题的答案因人而异,见仁见智。
在智能机流行起来之前,网上冲浪的主要媒介就是PC机,此时的显示屏幕足够大,即使某个网站的注册条目有十几条,只要美工合理排版,也能够获得清爽的交互体验,反而是将注册条目分类放到多个页面增加了鼠标点击的麻烦。
与之相对的是,智能机现在占据了我们娱乐的大部分时间,打游戏,听音乐,看电影,读书籍等等活动都选择了在4~5英寸的手机上完成。此时,注册登录等项目如果像原来那样设计成一页十几条,填写资料填到页末都可能会忘了自己到底填了什么,因此有必要分开来注册。比较好的实践是,注册的时候只要求用户提供必需的用户名,密码两项,其他用户相关信息留待首次登陆后完善个人资料。
SubstepView就是基于上述想法实现的“分步视图”,下面介绍具体的细节。
安装方法
在Android Studio中,以导入Module的方式添加SubstepView依赖(jCenter Remote Library Dependency等待更新...)。
基本用法
1 <org.warnier.zhang.widget.SubstepView 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" />
设计思想
SubstepView的核心思想与ViewPagerIndicator基本一致。
源代码
1 package org.warnier.zhang.widget; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 import android.support.v4.view.ViewPager; 8 import android.util.AttributeSet; 9 import android.util.DisplayMetrics; 10 import android.view.View; 11 import android.view.WindowManager; 12 13 public class SubstepView extends View implements ViewPager.OnPageChangeListener { 14 private ViewPager mViewPager; 15 private Paint mPaint; 16 private Paint mPaintSelected; 17 private Paint mPaintText; 18 private int mCurrentPage; 19 private float mWidth; 20 21 public SubstepView(Context context) { 22 this(context, null); 23 } 24 25 public SubstepView(Context context, AttributeSet attrs) { 26 this(context, attrs, 0); 27 } 28 29 public SubstepView(Context context, AttributeSet attrs, int defStyleAttr) { 30 super(context, attrs, defStyleAttr); 31 32 //Retrieve the style attributes. 33 34 //Customize the paint. 35 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 36 mPaint.setStrokeWidth(8); 37 mPaint.setColor(Color.parseColor("#B6B6B6")); 38 mPaintSelected = new Paint(Paint.ANTI_ALIAS_FLAG); 39 mPaintSelected.setStrokeWidth(8); 40 mPaintSelected.setColor(Color.parseColor("#4CAF50")); 41 mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG); 42 mPaintText.setTextSize(48); 43 mPaintText.setColor(Color.WHITE); 44 45 //Retrieve the width of screen. 46 WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 47 DisplayMetrics displayMetrics = new DisplayMetrics(); 48 windowManager.getDefaultDisplay().getMetrics(displayMetrics); 49 mWidth = displayMetrics.widthPixels; 50 } 51 52 @Override 53 protected void onDraw(Canvas canvas) { 54 super.onDraw(canvas); 55 if (mViewPager == null) { 56 return; 57 } 58 final int count = mViewPager.getAdapter().getCount(); 59 if (count == 0) { 60 return; 61 } 62 float width = mWidth / (count + 1); 63 float height = 48; 64 for (int i = 0; i < count; i++) { 65 if (i > 0) { 66 canvas.drawLine(width * i + height, height, width * (i + 1), height, (mCurrentPage >= i) ? mPaintSelected : mPaint); 67 } 68 canvas.drawCircle(width * (i + 1), height, height, (mCurrentPage >= i) ? mPaintSelected : mPaint); 69 //It does‘t work well when trying to draw text in the center of circle. 70 canvas.drawText("" + (i + 1), width * (i + 1) - 12, height + 12, mPaintText); 71 } 72 } 73 74 public void setViewPager(ViewPager viewPager) { 75 if (mViewPager == viewPager) { 76 return; 77 } 78 if (mViewPager != null) { 79 mViewPager.setOnPageChangeListener(null); 80 } 81 if (mViewPager.getAdapter() == null) { 82 throw new IllegalStateException("ViewPager does‘t have an adapter instance."); 83 } 84 mViewPager = viewPager; 85 mViewPager.setOnPageChangeListener(this); 86 invalidate(); 87 } 88 89 /** 90 * Add the custom implementation of ViewPager.OnPageChangeListener. 91 */ 92 @Override 93 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 94 //Do some operations if need. 95 } 96 97 @Override 98 public void onPageSelected(int position) { 99 mCurrentPage = position; 100 invalidate(); 101 } 102 103 @Override 104 public void onPageScrollStateChanged(int state) { 105 //Do some operations if need. 106 } 107 }
效果截图
标签:
原文地址:http://www.cnblogs.com/warnier-zhang/p/5252437.html