Skip to content

Commit 49e60a1

Browse files
author
imkarl
committed
修复部分情况下选项显示宽度变小的bug
1 parent 8ceafdf commit 49e60a1

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.1.3'
8+
classpath 'com.android.tools.build:gradle:2.2.0'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

library/src/main/java/com/weidongjian/meitu/wheelviewdemo/view/LoopView.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public enum ACTION {
7171
int itemsVisible;
7272

7373
int measuredHeight;
74-
int measuredWidth;
7574
int paddingLeft = 0;
7675
int paddingRight = 0;
7776

@@ -156,7 +155,6 @@ private void remeasure() {
156155
halfCircumference = (int) (maxTextHeight * lineSpacingMultiplier * (itemsVisible - 1));
157156
measuredHeight = (int) ((halfCircumference * 2) / Math.PI);
158157
radius = (int) (halfCircumference / Math.PI);
159-
measuredWidth = maxTextWidth + paddingLeft + paddingRight;
160158
firstLineY = (int) ((measuredHeight - lineSpacingMultiplier * maxTextHeight) / 2.0F);
161159
secondLineY = (int) ((measuredHeight + lineSpacingMultiplier * maxTextHeight) / 2.0F);
162160
if (initPosition == -1) {
@@ -337,8 +335,8 @@ protected void onDraw(Canvas canvas) {
337335
}
338336
k1++;
339337
}
340-
canvas.drawLine(0.0F, firstLineY, measuredWidth, firstLineY, paintIndicator);
341-
canvas.drawLine(0.0F, secondLineY, measuredWidth, secondLineY, paintIndicator);
338+
canvas.drawLine(0.0F, firstLineY, getWidth(), firstLineY, paintIndicator);
339+
canvas.drawLine(0.0F, secondLineY, getWidth(), secondLineY, paintIndicator);
342340

343341
final int itemWidth = ((View)getParent()).getWidth();
344342

@@ -369,31 +367,31 @@ protected void onDraw(Canvas canvas) {
369367
if (translateY <= firstLineY && maxTextHeight + translateY >= firstLineY) {
370368
// 条目经过第一条线
371369
canvas.save();
372-
canvas.clipRect(0, 0, measuredWidth, firstLineY - translateY);
370+
canvas.clipRect(0, 0, getWidth(), firstLineY - translateY);
373371
canvas.drawText(text, getTextX(text, paintOuterText, tempRect), maxTextHeight, paintOuterText);
374372
canvas.restore();
375373
canvas.save();
376-
canvas.clipRect(0, firstLineY - translateY, measuredWidth, (int) (itemHeight));
374+
canvas.clipRect(0, firstLineY - translateY, getWidth(), (int) (itemHeight));
377375
canvas.drawText(text, getTextX(text, paintCenterText, tempRect), maxTextHeight, paintCenterText);
378376
canvas.restore();
379377
} else if (translateY <= secondLineY && maxTextHeight + translateY >= secondLineY) {
380378
// 条目经过第二条线
381379
canvas.save();
382-
canvas.clipRect(0, 0, measuredWidth, secondLineY - translateY);
380+
canvas.clipRect(0, 0, getWidth(), secondLineY - translateY);
383381
canvas.drawText(text, getTextX(text, paintCenterText, tempRect), maxTextHeight, paintCenterText);
384382
canvas.restore();
385383
canvas.save();
386-
canvas.clipRect(0, secondLineY - translateY, measuredWidth, (int) (itemHeight));
384+
canvas.clipRect(0, secondLineY - translateY, getWidth(), (int) (itemHeight));
387385
canvas.drawText(text, getTextX(text, paintOuterText, tempRect), maxTextHeight, paintOuterText);
388386
canvas.restore();
389387
} else if (translateY >= firstLineY && maxTextHeight + translateY <= secondLineY) {
390388
// 中间条目
391-
canvas.clipRect(0, 0, measuredWidth, (int) (itemHeight));
389+
canvas.clipRect(0, 0, getWidth(), (int) (itemHeight));
392390
canvas.drawText(text, getTextX(text, paintCenterText, tempRect), maxTextHeight, paintCenterText);
393391
selectedItem = items.indexOf(text);
394392
} else {
395393
// 其他条目
396-
canvas.clipRect(0, 0, measuredWidth, (int) (itemHeight));
394+
canvas.clipRect(0, 0, getWidth(), (int) (itemHeight));
397395
canvas.drawText(text, getTextX(text, paintOuterText, tempRect), maxTextHeight, paintOuterText);
398396
}
399397
canvas.restore();
@@ -407,15 +405,13 @@ private int getTextX(String a, Paint paint, Rect rect) {
407405
paint.getTextBounds(a, 0, a.length(), rect);
408406
// 获取到的是实际文字宽度
409407
int textWidth = rect.width();
410-
// 转换成绘制文字宽度
411-
textWidth *= scaleX;
412-
return (measuredWidth - textWidth) / 2;
408+
return (getWidth() - textWidth) / 2;
413409
}
414410

415411
@Override
416412
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
417413
remeasure();
418-
setMeasuredDimension(measuredWidth, measuredHeight);
414+
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), measuredHeight);
419415
}
420416

421417
@Override

library/src/main/res/layout/j_picker_items.xml

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
android:gravity="center_horizontal">
1515
<com.weidongjian.meitu.wheelviewdemo.view.LoopView
1616
android:id="@+id/j_options1"
17-
android:layout_width="wrap_content"
18-
android:layout_height="wrap_content" />
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent" />
1919
</LinearLayout>
2020

2121
<LinearLayout
@@ -27,8 +27,8 @@
2727
android:gravity="center_horizontal">
2828
<com.weidongjian.meitu.wheelviewdemo.view.LoopView
2929
android:id="@+id/j_options2"
30-
android:layout_width="wrap_content"
31-
android:layout_height="wrap_content" />
30+
android:layout_width="match_parent"
31+
android:layout_height="match_parent" />
3232
</LinearLayout>
3333

3434
<LinearLayout
@@ -40,8 +40,8 @@
4040
android:gravity="center_horizontal">
4141
<com.weidongjian.meitu.wheelviewdemo.view.LoopView
4242
android:id="@+id/j_options3"
43-
android:layout_width="wrap_content"
44-
android:layout_height="wrap_content" />
43+
android:layout_width="match_parent"
44+
android:layout_height="match_parent" />
4545
</LinearLayout>
4646

4747
</LinearLayout>

0 commit comments

Comments
 (0)