1+ package com .sloop .canvas ;
2+
3+ import android .content .Context ;
4+ import android .graphics .Bitmap ;
5+ import android .graphics .BitmapFactory ;
6+ import android .graphics .Canvas ;
7+ import android .graphics .Paint ;
8+ import android .graphics .Rect ;
9+ import android .os .Handler ;
10+ import android .os .Message ;
11+ import android .util .AttributeSet ;
12+ import android .util .Log ;
13+ import android .view .View ;
14+
15+ /**
16+ * <ul type="disc">
17+ * <li>Author: Sloop</li>
18+ * <li>Version: v1.0.0</li>
19+ * <li>Copyright: Copyright (c) 2015</li>
20+ * <li>Date: 2016/2/5</li>
21+ * <li><a href="http://www.sloop.icoc.cc" target="_blank">作者网站</a> </li>
22+ * <li><a href="http://weibo.com/5459430586" target="_blank">作者微博</a> </li>
23+ * <li><a href="https://github.com/GcsSloop" target="_blank">作者GitHub</a> </li>
24+ * </ul>
25+ */
26+ public class CheckView extends View {
27+
28+ private static final int ANIM_NULL = 0 ; //动画状态-没有
29+ private static final int ANIM_CHECK = 1 ; //动画状态-开启
30+ private static final int ANIM_UNCHECK = 2 ; //动画状态-结束
31+
32+ private Context mContext ; // 上下文
33+ private int mWidth , mHeight ; // 宽高
34+ private Handler mHandler ; // handler
35+
36+ private Paint mPaint ;
37+ private Bitmap okBitmap ;
38+
39+ private int animCurrentPage = -1 ; // 当前页码
40+ private int animMaxPage = 13 ; // 总页数
41+ private int animDuration = 500 ; // 动画时长
42+ private int animState = ANIM_NULL ; // 动画状态
43+
44+ private boolean isCheck = false ; // 是否只选中状态
45+
46+ public CheckView (Context context ) {
47+ super (context , null );
48+
49+ }
50+
51+ public CheckView (Context context , AttributeSet attrs ) {
52+ super (context , attrs );
53+ init (context );
54+ }
55+
56+ /**
57+ * 初始化
58+ * @param context
59+ */
60+ private void init (Context context ) {
61+ mContext = context ;
62+
63+ mPaint = new Paint ();
64+ mPaint .setColor (0xffFF5317 );
65+ mPaint .setStyle (Paint .Style .FILL );
66+ mPaint .setAntiAlias (true );
67+
68+ okBitmap = BitmapFactory .decodeResource (mContext .getResources (), R .drawable .checkres );
69+
70+ mHandler = new Handler () {
71+ @ Override
72+ public void handleMessage (Message msg ) {
73+ super .handleMessage (msg );
74+ if (animCurrentPage < animMaxPage && animCurrentPage >= 0 ) {
75+ invalidate ();
76+ if (animState == ANIM_NULL )
77+ return ;
78+ if (animState == ANIM_CHECK ) {
79+
80+ animCurrentPage ++;
81+ } else if (animState == ANIM_UNCHECK ) {
82+ animCurrentPage --;
83+ }
84+
85+ this .sendEmptyMessageDelayed (0 , animDuration / animMaxPage );
86+ Log .e ("AAA" , "Count=" + animCurrentPage );
87+ } else {
88+ if (isCheck ) {
89+ animCurrentPage = animMaxPage - 1 ;
90+ } else {
91+ animCurrentPage = -1 ;
92+ }
93+ invalidate ();
94+ animState = ANIM_NULL ;
95+ }
96+ }
97+ };
98+ }
99+
100+
101+ /**
102+ * View大小确定
103+ * @param w
104+ * @param h
105+ * @param oldw
106+ * @param oldh
107+ */
108+ @ Override
109+ protected void onSizeChanged (int w , int h , int oldw , int oldh ) {
110+ super .onSizeChanged (w , h , oldw , oldh );
111+ mWidth = w ;
112+ mHeight = h ;
113+ }
114+
115+ /**
116+ * 绘制内容
117+ * @param canvas
118+ */
119+ @ Override
120+ protected void onDraw (Canvas canvas ) {
121+ super .onDraw (canvas );
122+
123+ // 移动坐标系到画布中央
124+ canvas .translate (mWidth / 2 , mHeight / 2 );
125+
126+ // 绘制背景圆形
127+ canvas .drawCircle (0 , 0 , 240 , mPaint );
128+
129+ // 得出图像边长
130+ int sideLength = okBitmap .getHeight ();
131+
132+ // 得到图像选区 和 实际绘制位置
133+ Rect src = new Rect (sideLength * animCurrentPage , 0 , sideLength * (animCurrentPage + 1 ), sideLength );
134+ Rect dst = new Rect (-200 , -200 , 200 , 200 );
135+
136+ // 绘制
137+ canvas .drawBitmap (okBitmap , src , dst , null );
138+ }
139+
140+
141+ /**
142+ * 选择
143+ */
144+ public void check () {
145+ if (animState != ANIM_NULL || isCheck )
146+ return ;
147+ animState = ANIM_CHECK ;
148+ animCurrentPage = 0 ;
149+ mHandler .sendEmptyMessageDelayed (0 , animDuration / animMaxPage );
150+ isCheck = true ;
151+ }
152+
153+ /**
154+ * 取消选择
155+ */
156+ public void unCheck () {
157+ if (animState != ANIM_NULL || (!isCheck ))
158+ return ;
159+ animState = ANIM_UNCHECK ;
160+ animCurrentPage = animMaxPage - 1 ;
161+ mHandler .sendEmptyMessageDelayed (0 , animDuration / animMaxPage );
162+ isCheck = false ;
163+ }
164+
165+ /**
166+ * 设置动画时长
167+ * @param animDuration
168+ */
169+ public void setAnimDuration (int animDuration ) {
170+ if (animDuration <= 0 )
171+ return ;
172+ this .animDuration = animDuration ;
173+ }
174+
175+ /**
176+ * 设置背景圆形颜色
177+ * @param color
178+ */
179+ public void setBackgroundColor (int color ){
180+ mPaint .setColor (color );
181+ }
182+ }
0 commit comments