Skip to content

Commit 595040d

Browse files
committed
1、修复画笔初始时机,避免崩溃
2、增加无曲面的滚动效果
1 parent 850459f commit 595040d

File tree

5 files changed

+103
-58
lines changed

5 files changed

+103
-58
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ dependencies {
3030
api fileTree(include: ['*.jar'], dir: 'libs')
3131
api project(':loopview')
3232
api 'com.android.support:appcompat-v7:28.0.0'
33-
// api 'com.weigan:loopView:0.2.2'
33+
// api 'com.github.weidongjian:androidWheelView:0.9.1'
3434
}
3535

app/src/main/java/com/weidongjian/meitu/wheelviewdemo/MainActivity.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,45 @@ public class MainActivity extends AppCompatActivity {
1717

1818
private Toast toast;
1919

20-
private LoopView loopView;
21-
2220
@Override
2321
protected void onCreate(Bundle savedInstanceState) {
2422
super.onCreate(savedInstanceState);
2523
setContentView(R.layout.activity_main);
2624

27-
loopView = (LoopView) findViewById(R.id.loopView);
25+
LoopView loopView = (LoopView) findViewById(R.id.loopView);
26+
initLoopView(loopView);
27+
28+
LoopView loopView2 = (LoopView) findViewById(R.id.loopView2);
29+
initLoopView(loopView2);
30+
31+
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
32+
@Override
33+
public void onClick(View v) {
34+
loopView.setCurrentPosition(4);
35+
}
36+
});
2837

29-
//set type face
30-
// loopView.setTypeface(Typeface.DEFAULT_BOLD);
38+
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
39+
@Override
40+
public void onClick(View v) {
41+
Intent intent = new Intent(MainActivity.this, ScrollViewActivity.class);
42+
startActivity(intent);
43+
}
44+
});
45+
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
46+
@Override
47+
public void onClick(View v) {
48+
Intent intent = new Intent(MainActivity.this, DialogActivity.class);
49+
startActivity(intent);
50+
}
51+
});
52+
}
3153

54+
private void initLoopView(LoopView loopView) {
3255
ArrayList<String> list = new ArrayList<>();
3356
for (int i = 0; i < 60; i++) {
3457
list.add("item gp" + i);
3558
}
36-
//设置是否循环播放
37-
// loopView.setNotLoop();
3859
//滚动监听
3960
loopView.setListener(new OnItemSelectedListener() {
4061
@Override
@@ -49,41 +70,18 @@ public void onItemSelected(int index) {
4970
loopView.setOnItemScrollListener(new OnItemScrollListener() {
5071
@Override
5172
public void onItemScrollStateChanged(LoopView loopView, int currentPassItem, int oldScrollState, int scrollState, int totalScrollY) {
52-
Log.i("gy",String.format("onItemScrollStateChanged currentPassItem %d oldScrollState %d scrollState %d totalScrollY %d",currentPassItem,oldScrollState,scrollState,totalScrollY));
73+
Log.i("gy", String.format("onItemScrollStateChanged currentPassItem %d oldScrollState %d scrollState %d totalScrollY %d", currentPassItem, oldScrollState, scrollState, totalScrollY));
5374
}
5475

5576
@Override
5677
public void onItemScrolling(LoopView loopView, int currentPassItem, int scrollState, int totalScrollY) {
57-
Log.i("gy",String.format("onItemScrolling currentPassItem %d scrollState %d totalScrollY %d",currentPassItem,scrollState,totalScrollY));
78+
Log.i("gy", String.format("onItemScrolling currentPassItem %d scrollState %d totalScrollY %d", currentPassItem, scrollState, totalScrollY));
5879
}
5980
});
6081
//设置原始数据
6182
loopView.setItems(list);
62-
6383
//设置初始位置
6484
loopView.setInitPosition(4);
65-
66-
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
67-
@Override
68-
public void onClick(View v) {
69-
loopView.setCurrentPosition(4);
70-
}
71-
});
72-
73-
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
74-
@Override
75-
public void onClick(View v) {
76-
Intent intent = new Intent(MainActivity.this, ScrollViewActivity.class);
77-
startActivity(intent);
78-
}
79-
});
80-
findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
81-
@Override
82-
public void onClick(View v) {
83-
Intent intent = new Intent(MainActivity.this, DialogActivity.class);
84-
startActivity(intent);
85-
}
86-
});
8785
}
8886

8987
@Override

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<LinearLayout
2-
android:id="@+id/rootview"
1+
<LinearLayout android:id="@+id/rootview"
32
xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:app="http://schemas.android.com/apk/res-auto"
54
xmlns:tools="http://schemas.android.com/tools"
@@ -8,22 +7,47 @@
87
android:orientation="vertical"
98
tools:context=".MainActivity">
109

10+
<TextView
11+
android:layout_marginTop="10dp"
12+
android:textSize="20dp"
13+
android:text="曲面效果"
14+
android:gravity="center"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content" />
17+
18+
<com.weigan.loopview.LoopView
19+
android:layout_marginTop="10dp"
20+
android:id="@+id/loopView"
21+
android:layout_width="match_parent"
22+
android:layout_height="150dp"
23+
app:awv_textsize="18"
24+
app:awv_isLoop="false" />
1125

1226
<Button
13-
android:text="Button"
27+
android:text="选中第4个位置"
1428
android:layout_width="wrap_content"
1529
android:layout_height="wrap_content"
16-
android:id="@+id/button"
17-
/>
30+
android:id="@+id/button" />
31+
32+
33+
<TextView
34+
android:layout_marginTop="10dp"
35+
android:textSize="20dp"
36+
android:text="无曲面效果"
37+
android:gravity="center"
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content" />
1840

1941
<com.weigan.loopview.LoopView
20-
android:layout_marginTop="50dp"
21-
android:id="@+id/loopView"
42+
android:layout_marginTop="10dp"
43+
android:id="@+id/loopView2"
2244
android:layout_width="match_parent"
23-
android:layout_height="150dp"
45+
android:layout_height="180dp"
46+
app:awv_isCurve="false"
47+
app:awv_itemsVisibleCount="7"
2448
app:awv_textsize="18"
25-
app:awv_isLoop="false"
26-
/>
49+
app:awv_isLoop="false" />
50+
2751

2852
<Button
2953
android:text="Inside scrollView"

loopview/src/main/java/com/weigan/loopview/LoopView.java

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public class LoopView extends View {
3333

3434
private static final float DEFAULT_LINE_SPACE = 1f;
3535

36-
private static final int DEFAULT_VISIBIE_ITEMS = 9;
36+
private static final int DEFAULT_VISIBLE_ITEMS = 9;
37+
38+
private static boolean ENABLE_CURVE = true;
3739

3840
public static final int SCROLL_STATE_IDLE = 0; // 停止滚动
3941
public static final int SCROLL_STATE_SETTING = 1; // 用户设置
@@ -65,7 +67,7 @@ public enum ACTION {
6567
List<IndexString> items;
6668

6769
int textSize;
68-
int itemTextHeight;
70+
int itemTextHeight; //单个item的高度
6971

7072
//文本的高度
7173
int textHeight;
@@ -88,14 +90,14 @@ public enum ACTION {
8890

8991
int itemsVisibleCount;
9092

91-
HashMap<Integer,IndexString> drawingStrings;
93+
HashMap<Integer, IndexString> drawingStrings;
9294
// HashMap<String,Integer> drawingStr
9395

94-
int measuredHeight;
96+
int measuredHeight; //布局定义的高度
9597
int measuredWidth;
9698

97-
int halfCircumference;
98-
int radius;
99+
int halfCircumference;//半圆的周长
100+
int radius; //半径
99101

100102
private int mOffset = 0;
101103
private float previousY;
@@ -107,8 +109,11 @@ public enum ACTION {
107109

108110
private Typeface typeface = Typeface.MONOSPACE;
109111

112+
private boolean isEnableCurve = ENABLE_CURVE;
113+
110114
/**
111115
* set text line space, must more than 1
116+
*
112117
* @param lineSpacingMultiplier
113118
*/
114119
public void setLineSpacingMultiplier(float lineSpacingMultiplier) {
@@ -189,17 +194,21 @@ private void initLoopView(Context context, AttributeSet attributeset) {
189194
outerTextColor = typedArray.getInteger(R.styleable.LoopView_awv_outerTextColor, 0xffafafaf);
190195
dividerColor = typedArray.getInteger(R.styleable.LoopView_awv_dividerTextColor, 0xffc5c5c5);
191196
itemsVisibleCount =
192-
typedArray.getInteger(R.styleable.LoopView_awv_itemsVisibleCount, DEFAULT_VISIBIE_ITEMS);
197+
typedArray.getInteger(R.styleable.LoopView_awv_itemsVisibleCount, DEFAULT_VISIBLE_ITEMS);
193198
if (itemsVisibleCount % 2 == 0) {
194-
itemsVisibleCount = DEFAULT_VISIBIE_ITEMS;
199+
itemsVisibleCount = DEFAULT_VISIBLE_ITEMS;
195200
}
196201
isLoop = typedArray.getBoolean(R.styleable.LoopView_awv_isLoop, true);
202+
isEnableCurve = typedArray.getBoolean(R.styleable.LoopView_awv_isCurve, ENABLE_CURVE);
197203
typedArray.recycle();
198204
}
199205

200-
drawingStrings=new HashMap<>();
206+
drawingStrings = new HashMap<>();
201207
totalScrollY = 0;
202208
initPosition = -1;
209+
210+
//初始化画笔
211+
initPaintsIfPossible();
203212
}
204213

205214

@@ -266,7 +275,12 @@ private void remeasure() {
266275
textHeight = tempRect.height();
267276
halfCircumference = (int) (measuredHeight * Math.PI / 2);
268277

269-
itemTextHeight = (int) (halfCircumference / (lineSpacingMultiplier * (itemsVisibleCount - 1)));
278+
if (isEnableCurve) {
279+
itemTextHeight = (int) (halfCircumference / (lineSpacingMultiplier * (itemsVisibleCount - 1)));
280+
} else {
281+
itemTextHeight = measuredHeight / itemsVisibleCount;
282+
}
283+
270284

271285
radius = measuredHeight / 2;
272286
firstLineY = (int) ((measuredHeight - lineSpacingMultiplier * itemTextHeight) / 2.0F);
@@ -474,7 +488,8 @@ protected void onDraw(Canvas canvas) {
474488
}
475489
}
476490

477-
int j2 = (int) (totalScrollY % (lineSpacingMultiplier * itemTextHeight));
491+
//偏移量
492+
int j2 = totalScrollY % itemTextHeight;
478493
// put value to drawingString
479494
int k1 = 0;
480495
while (k1 < itemsVisibleCount) {
@@ -507,12 +522,20 @@ protected void onDraw(Canvas canvas) {
507522
canvas.save();
508523
float itemHeight = itemTextHeight * lineSpacingMultiplier;
509524
double radian = ((itemHeight * i - j2) * Math.PI) / halfCircumference;
510-
if (radian >= Math.PI || radian <= 0) {
525+
if ((radian >= Math.PI || radian <= 0) && isEnableCurve) {
511526
canvas.restore();
512527
} else {
513-
int translateY = (int) (radius - Math.cos(radian) * radius - (Math.sin(radian) * itemTextHeight) / 2D);
528+
int translateY;
529+
if (isEnableCurve) {
530+
translateY = (int) (radius - Math.cos(radian) * radius - (Math.sin(radian) * itemTextHeight) / 2D);
531+
} else {
532+
translateY = (int) (itemHeight * i - j2);
533+
Log.d("weigan", "translateY " + translateY + " pos " + i + " j2 " + j2);
534+
}
514535
canvas.translate(0.0F, translateY);
515-
canvas.scale(1.0F, (float) Math.sin(radian));
536+
if (isEnableCurve) {
537+
canvas.scale(1.0F, (float) Math.sin(radian));
538+
}
516539
if (translateY <= firstLineY && itemTextHeight + translateY >= firstLineY) {
517540
// first divider
518541
canvas.save();
@@ -594,7 +617,6 @@ private int getTextX(String a, Paint paint, Rect rect) {
594617
@Override
595618
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
596619
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
597-
initPaintsIfPossible();
598620
remeasure();
599621
}
600622

loopview/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<attr name="awv_dividerTextColor" format="integer" />
99
<attr name="awv_itemsVisibleCount" format="integer" />
1010
<attr name="awv_isLoop" format="boolean" />
11+
<attr name="awv_isCurve" format="boolean" />
1112
<attr name="awv_initialPosition" format="integer" />
1213
<attr name="awv_scaleX" format="float" />
1314
</declare-styleable>

0 commit comments

Comments
 (0)