Skip to content

Commit a317d7c

Browse files
committed
WheelView源码解析注释
1 parent c8daab7 commit a317d7c

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void onClick(View v) {
7878
private void initTimePicker() {
7979
//控制时间范围(如果不设置范围,则使用默认时间1900-2100年,此段代码可注释)
8080
Calendar selectedDate = Calendar.getInstance();
81-
/*selectedDate.set(2013,2,29);*/
81+
selectedDate.set(2013,2,29);
8282
Calendar startDate = Calendar.getInstance();
8383
startDate.set(2013,1,23);
8484
Calendar endDate = Calendar.getInstance();

pickerview/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
defaultConfig {
1313
minSdkVersion 9
1414
targetSdkVersion 25
15-
versionCode 11
16-
versionName "3.0.7"
15+
versionCode 12
16+
versionName "3.0.8"
1717
}
1818
buildTypes {
1919
release {
@@ -39,7 +39,7 @@ publish {
3939
userOrg = 'contrarywind'//bintray.com 用户名/组织名 user/org name
4040
groupId = 'com.contrarywind'//JCenter上显示的路径 path
4141
artifactId = 'Android-PickerView'//项目名称 project name
42-
publishVersion = '3.0.7'//版本号 version code
42+
publishVersion = '3.0.8'//版本号 version code
4343
desc = 'this is a pickerview for android'//项目描述 description
4444
website = 'https://github.com/Contrarywind/Android-PickerView' //项目网址链接 link
4545
}

pickerview/src/main/java/com/bigkoo/pickerview/lib/SmoothScrollTimerTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public final void run() {
2121
if (realTotalOffset == Integer.MAX_VALUE) {
2222
realTotalOffset = offset;
2323
}
24-
//把要滚动的范围细分成十小份,按是小份单位来重绘
24+
//把要滚动的范围细分成10小份,按10小份单位来重绘
2525
realOffset = (int) ((float) realTotalOffset * 0.1F);
2626

2727
if (realOffset == 0) {

pickerview/src/main/java/com/bigkoo/pickerview/lib/WheelView.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void smoothScroll(ACTION action) {
251251
cancelFuture();
252252
if (action == ACTION.FLING || action == ACTION.DAGGLE) {
253253
mOffset = (int) ((totalScrollY % itemHeight + itemHeight) % itemHeight);
254-
if ((float) mOffset > itemHeight / 2.0F) {
254+
if ((float) mOffset > itemHeight / 2.0F) {//如果超过Item高度的一半,滚动到下一个Item去
255255
mOffset = (int) (itemHeight - (float) mOffset);
256256
} else {
257257
mOffset = -mOffset;
@@ -606,13 +606,20 @@ public boolean onTouchEvent(MotionEvent event) {
606606
break;
607607
//完成滑动,手指离开屏幕
608608
case MotionEvent.ACTION_UP:
609+
609610
default:
610-
if (!eventConsumed) {
611+
if (!eventConsumed) {//屏幕点击或者拖拽事件
611612
float y = event.getY();
612-
double l = Math.acos((radius - y) / radius) * radius;
613-
int circlePosition = (int) ((l + itemHeight / 2) / itemHeight);
614-
613+
// 由于之前是有向右偏移90度,所以 实际弧度范围为α2 =π/2-α (α=[0,π])
614+
// 根据cosα = sin(π/2-α)
615+
// (radius - y) / radius = sinα2 = sin(π/2-α) = cosα
616+
// arccos(cosα)= α
617+
// 所以 弧长 L = α*R
618+
double L = Math.acos((radius - y) / radius) * radius;
619+
//item0 有一半是在不可见区域,所以需要加上 itemHeight / 2
620+
int circlePosition = (int) ((L + itemHeight / 2) / itemHeight);
615621
float extraOffset = (totalScrollY % itemHeight + itemHeight) % itemHeight;
622+
//已滑动的弧长值
616623
mOffset = (int) ((circlePosition - itemsVisible / 2) * itemHeight - extraOffset);
617624

618625
if ((System.currentTimeMillis() - startTime) > 120) {

0 commit comments

Comments
 (0)