Skip to content

Commit 2ff5698

Browse files
author
jie.wu
committed
优化代码,修复了一些bug
1 parent 8e69d57 commit 2ff5698

16 files changed

Lines changed: 314 additions & 309 deletions

File tree

.idea/gradle.xml

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 24 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,20 @@
3838
android:id="@+id/wv_year"
3939
android:layout_width="wrap_content"
4040
android:layout_height="wrap_content"
41-
app:toward="left"
4241
style="@style/DatePickerStyle"/>
4342

4443
<com.cncoderx.wheelview.Wheel3DView
4544
android:id="@+id/wv_month"
4645
android:layout_width="wrap_content"
4746
android:layout_height="wrap_content"
48-
app:cyclic="true"
47+
app:wheelCyclic="true"
4948
style="@style/DatePickerStyle"/>
5049

5150
<com.cncoderx.wheelview.Wheel3DView
5251
android:id="@+id/wv_day"
5352
android:layout_width="wrap_content"
5453
android:layout_height="wrap_content"
55-
app:cyclic="true"
56-
app:toward="right"
54+
app:wheelCyclic="true"
5755
style="@style/DatePickerStyle"/>
5856
</LinearLayout>
5957
</RelativeLayout>

app/src/main/res/layout/activity_wheel_view_test.xml

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,46 @@
66
android:layout_height="match_parent"
77
android:gravity="center">
88

9+
<!--<attr name="wheelCyclic" format="boolean"/>-->
10+
<!--<attr name="wheelEntries" format="reference"/>-->
11+
<!--<attr name="wheelItemCount" format="integer"/>-->
12+
<!--<attr name="wheelItemWidth" format="dimension"/>-->
13+
<!--<attr name="wheelItemHeight" format="dimension"/>-->
14+
<!--<attr name="wheelTextSize" format="dimension"/>-->
15+
<!--<attr name="wheelTextColor" format="color"/>-->
16+
<!--<attr name="wheelSelectedTextColor" format="color"/>-->
17+
<!--<attr name="wheelDividerColor" format="color"/>-->
18+
<!--<attr name="wheelHighlightColor" format="color"/>-->
19+
920
<com.cncoderx.wheelview.WheelView
1021
android:id="@+id/wheel"
1122
android:layout_width="wrap_content"
1223
android:layout_height="wrap_content"
13-
app:cyclic="true"
14-
app:entries="@array/default_array"
15-
app:visibleItems="9"
16-
app:toward="none"
17-
app:textSize="24sp"
18-
app:lineSpace="20dp"/>
24+
app:wheelCyclic="true"
25+
app:wheelEntries="@array/default_array"
26+
app:wheelItemCount="9"
27+
app:wheelItemWidth="160dp"
28+
app:wheelItemHeight="40dp"
29+
app:wheelTextSize="@dimen/wheel_text_size"
30+
app:wheelTextColor="@color/wheel_text_color"
31+
app:wheelSelectedTextColor="@color/wheel_selected_text_color"
32+
app:wheelDividerColor="@color/wheel_divider_color"
33+
app:wheelHighlightColor="@color/wheel_highlight_color"/>
1934

2035
<com.cncoderx.wheelview.Wheel3DView
2136
android:id="@+id/wheel3d"
2237
android:layout_width="wrap_content"
2338
android:layout_height="wrap_content"
2439
android:layout_marginLeft="20dp"
25-
app:cyclic="true"
26-
app:entries="@array/default_array"
27-
app:visibleItems="9"
28-
app:toward="none"
29-
app:textSize="24sp"
30-
app:lineSpace="20dp"/>
40+
app:wheelCyclic="true"
41+
app:wheelEntries="@array/default_array"
42+
app:wheelItemCount="9"
43+
app:wheelItemWidth="160dp"
44+
app:wheelItemHeight="40dp"
45+
app:wheelTextSize="@dimen/wheel_text_size"
46+
app:wheelTextColor="@color/wheel_text_color"
47+
app:wheelSelectedTextColor="@color/wheel_selected_text_color"
48+
app:wheelDividerColor="@color/wheel_divider_color"
49+
app:wheelHighlightColor="@color/wheel_highlight_color" />
3150

3251
</LinearLayout>

app/src/main/res/values/styles.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<resources>
22

33
<style name="DatePickerStyle">
4-
<item name="lineSpace">12dp</item>
5-
<item name="textSize">24sp</item>
6-
<item name="selectedColor">#fff</item>
7-
<item name="unselectedColor">#606060</item>
8-
<item name="divider">@drawable/date_picker_divider</item>
4+
<item name="wheelTextSize">24sp</item>
5+
<item name="wheelTextColor">#606060</item>
6+
<item name="wheelSelectedTextColor">#fff</item>
7+
<item name="wheelDividerColor">#3d3d3d</item>
98
</style>
109
</resources>

library/bintray.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'com.jfrog.bintray'
44
def siteUrl = 'https://github.com/CNCoderX/WheelView'
55
def gitUrl = 'https://github.com/CNCoderX/WheelView.git'
66

7-
version = "1.2.2"
7+
version = "1.2.4"
88
group = "com.cncoderx.wheelview"
99
def libName = "WheelView"
1010
def libDesc = "like ios wheel selector"

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 14
99
targetSdkVersion 21
1010
versionCode 2
11-
versionName "1.2.2"
11+
versionName "1.2.4"
1212
}
1313
buildTypes {
1414
release {

library/src/main/java/com/cncoderx/wheelview/Wheel3DView.java

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.graphics.Camera;
55
import android.graphics.Canvas;
66
import android.graphics.Matrix;
7+
import android.graphics.Paint;
78
import android.util.AttributeSet;
89

910
/**
@@ -23,45 +24,26 @@ public Wheel3DView(Context context, AttributeSet attrs) {
2324
mMatrix = new Matrix();
2425
}
2526

26-
/**
27-
* @return 控件的预算宽度
28-
*/
29-
public int getPrefWidth() {
30-
int prefWidth = super.getPrefWidth();
31-
int innerHeight = (int) (itemHeight * getVisibleItems() * 2 / Math.PI);
32-
int towardRange = (int) (Math.sin(Math.PI / 48) * innerHeight);
33-
// 必须增加滚轮的内边距,否则当toward不为none时文字显示不全
34-
prefWidth += towardRange;
35-
return prefWidth;
36-
}
37-
38-
/**
39-
* @return 控件的预算高度
40-
*/
27+
@Override
4128
public int getPrefHeight() {
4229
int padding = getPaddingTop() + getPaddingBottom();
43-
int innerHeight = (int) (itemHeight * getVisibleItems() * 2 / Math.PI);
30+
int innerHeight = (int) (mItemHeight * mItemCount * 2 / Math.PI);
4431
return innerHeight + padding;
4532
}
4633

47-
/**
48-
* 根据控件的测量高度,计算可见项的数量
49-
*/
50-
public int getPrefVisibleItems() {
51-
int innerHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
52-
return (int) (innerHeight * Math.PI / itemHeight / 2);
53-
}
54-
5534
protected void drawItem(Canvas canvas, int index, int offset) {
5635
CharSequence text = getCharSequence(index);
5736
if (text == null) return;
5837
// 滚轮的半径
5938
final int r = (getHeight() - getPaddingTop() - getPaddingBottom()) / 2;
6039
// 和中间选项的距离
61-
final int range = (index - mScroller.getItemIndex()) * itemHeight - offset;
40+
final int range = (index - mScroller.getItemIndex()) * mItemHeight - offset;
6241
// 当滑动的角度和y轴垂直时(此时文字已经显示为一条线),不绘制文字
6342
if (Math.abs(range) > r * Math.PI / 2) return;
6443

44+
final int centerX = mClipRectMiddle.centerX();
45+
final int centerY = mClipRectMiddle.centerY();
46+
6547
final double angle = (double) range / r;
6648
// 绕x轴滚动的角度
6749
float rotate = (float) Math.toDegrees(-angle);
@@ -76,62 +58,69 @@ protected void drawItem(Canvas canvas, int index, int offset) {
7658
// 透明度
7759
int alpha = (int) (Math.cos(angle) * 255);
7860

79-
int clipLeft = getPaddingLeft();
80-
int clipRight = getWidth() - getPaddingRight();
81-
int clipTop = getPaddingTop();
82-
int clipBottom = getHeight() - getPaddingBottom();
83-
84-
// 绘制两条分界线之间的文字
85-
if (Math.abs(range) <= 0) {
86-
mPaint.setColor(getSelectedColor());
87-
canvas.save();
88-
canvas.translate(refractX, 0);
89-
canvas.clipRect(clipLeft, upperLimit, clipRight, lowerLimit);
90-
drawText(canvas, text, 0, translateY, translateZ, rotate);
91-
canvas.restore();
92-
}
9361
// 绘制与下分界线相交的文字
94-
else if (range > 0 && range < itemHeight) {
95-
mPaint.setColor(getSelectedColor());
62+
if (range > 0 && range < mItemHeight) {
9663
canvas.save();
9764
canvas.translate(refractX, 0);
98-
canvas.clipRect(clipLeft, upperLimit, clipRight, lowerLimit);
99-
drawText(canvas, text, 0, translateY, translateZ, rotate);
65+
canvas.clipRect(mClipRectMiddle);
66+
drawText(canvas, text, centerX, centerY, 0, translateY, translateZ, rotate, mSelectedTextPaint);
10067
canvas.restore();
10168

102-
mPaint.setColor(getUnselectedColor());
103-
mPaint.setAlpha(alpha);
69+
mTextPaint.setAlpha(alpha);
10470
canvas.save();
105-
canvas.clipRect(clipLeft, lowerLimit, clipRight, clipBottom);
106-
drawText(canvas, text, 0, translateY, translateZ, rotate);
71+
canvas.clipRect(mClipRectBottom);
72+
drawText(canvas, text, centerX, centerY, 0, translateY, translateZ, rotate, mTextPaint);
73+
canvas.restore();
74+
}
75+
// 绘制下分界线下方的文字
76+
else if (range >= mItemHeight) {
77+
mTextPaint.setAlpha(alpha);
78+
canvas.save();
79+
canvas.clipRect(mClipRectBottom);
80+
drawText(canvas, text, centerX, centerY, 0, translateY, translateZ, rotate, mTextPaint);
10781
canvas.restore();
10882
}
10983
// 绘制与上分界线相交的文字
110-
else if (range < 0 && range > -itemHeight) {
111-
mPaint.setColor(getSelectedColor());
84+
else if (range < 0 && range > -mItemHeight) {
11285
canvas.save();
11386
canvas.translate(refractX, 0);
114-
canvas.clipRect(clipLeft, upperLimit, clipRight, lowerLimit);
115-
drawText(canvas, text, 0, translateY, translateZ, rotate);
87+
canvas.clipRect(mClipRectMiddle);
88+
drawText(canvas, text, centerX, centerY, 0, translateY, translateZ, rotate, mSelectedTextPaint);
11689
canvas.restore();
11790

118-
mPaint.setColor(getUnselectedColor());
119-
mPaint.setAlpha(alpha);
91+
mTextPaint.setAlpha(alpha);
92+
canvas.save();
93+
canvas.clipRect(mClipRectTop);
94+
drawText(canvas, text, centerX, centerY, 0, translateY, translateZ, rotate, mTextPaint);
95+
canvas.restore();
96+
}
97+
// 绘制上分界线上方的文字
98+
else if (range <= -mItemHeight) {
99+
mTextPaint.setAlpha(alpha);
120100
canvas.save();
121-
canvas.clipRect(clipLeft, clipTop, clipRight, upperLimit);
122-
drawText(canvas, text, 0, translateY, translateZ, rotate);
101+
canvas.clipRect(mClipRectTop);
102+
drawText(canvas, text, centerX, centerY, 0, translateY, translateZ, rotate, mTextPaint);
123103
canvas.restore();
124-
} else {
125-
mPaint.setColor(getUnselectedColor());
126-
mPaint.setAlpha(alpha);
104+
}
105+
// 绘制两条分界线之间的文字
106+
else {
127107
canvas.save();
128-
canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);
129-
drawText(canvas, text, 0, translateY, translateZ, rotate);
108+
canvas.translate(refractX, 0);
109+
canvas.clipRect(mClipRectMiddle);
110+
drawText(canvas, text, centerX, centerY, 0, translateY, translateZ, rotate, mSelectedTextPaint);
130111
canvas.restore();
131112
}
132113
}
133114

134-
private void drawText(Canvas canvas, CharSequence text, float translateX, float translateY, float translateZ, float rotateX) {
115+
private void drawText(Canvas canvas,
116+
CharSequence text,
117+
float centerX,
118+
float centerY,
119+
float translateX,
120+
float translateY,
121+
float translateZ,
122+
float rotateX,
123+
Paint paint) {
135124
mCamera.save();
136125
mCamera.translate(translateX, 0, translateZ);
137126
mCamera.rotateX(rotateX);
@@ -145,7 +134,10 @@ private void drawText(Canvas canvas, CharSequence text, float translateX, float
145134
mMatrix.preTranslate(-x, -y);
146135
mMatrix.postTranslate(x, y);
147136

137+
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
138+
int baseline = (int) ((fontMetrics.top + fontMetrics.bottom) / 2);
139+
148140
canvas.concat(mMatrix);
149-
canvas.drawText(text, 0, text.length(), x, y - baseline, mPaint);
141+
canvas.drawText(text, 0, text.length(), x, y - baseline, paint);
150142
}
151143
}

0 commit comments

Comments
 (0)