Skip to content

Commit 16ba9d3

Browse files
committed
更新源码
1 parent 66f77c3 commit 16ba9d3

2 files changed

Lines changed: 367 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)