From 9eeb64b38deacc8a84f87bf69e6022a5a1e6f967 Mon Sep 17 00:00:00 2001 From: angcyo Date: Wed, 11 Mar 2020 18:29:02 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=89=8B=E5=8A=BF?= =?UTF-8?q?=E6=BB=91=E5=8A=A8=E5=9C=A8=E9=A6=96=E5=B0=BE=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E8=BE=B9=E7=95=8C=E5=88=A4=E6=96=AD=E8=8C=83?= =?UTF-8?q?=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wheelview/src/main/java/com/contrarywind/view/WheelView.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index ea7327c7..fba8eb94 100644 --- a/wheelview/src/main/java/com/contrarywind/view/WheelView.java +++ b/wheelview/src/main/java/com/contrarywind/view/WheelView.java @@ -675,6 +675,7 @@ public boolean onTouchEvent(MotionEvent event) { float top = -initPosition * itemHeight; float bottom = (adapter.getItemsCount() - 1 - initPosition) * itemHeight; float ratio = 0.25f; + float threshold = (itemHeight - maxTextHeight) / 2 + CENTER_CONTENT_OFFSET; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: @@ -690,8 +691,8 @@ public boolean onTouchEvent(MotionEvent event) { // normal mode。 if (!isLoop) { - if ((totalScrollY - itemHeight * ratio < top && dy < 0) - || (totalScrollY + itemHeight * ratio > bottom && dy > 0)) { + if ((totalScrollY - itemHeight * ratio < top - threshold && dy < 0) + || (totalScrollY + itemHeight * ratio > bottom + threshold && dy > 0)) { //快滑动到边界了,设置已滑动到边界的标志 totalScrollY -= dy; isIgnore = true; From 50e7f031299ab74f3ce534f3dd982aaf833209e7 Mon Sep 17 00:00:00 2001 From: angcyo Date: Thu, 12 Mar 2020 15:42:01 +0800 Subject: [PATCH 02/12] =?UTF-8?q?+=20=E6=94=AF=E6=8C=81=E5=9C=A8xml?= =?UTF-8?q?=E4=B8=AD=E9=A2=84=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../contrarywind/adapter/PreviewAdapter.java | 36 +++++++++++++++++++ .../java/com/contrarywind/view/WheelView.java | 9 ++++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 wheelview/src/main/java/com/contrarywind/adapter/PreviewAdapter.java diff --git a/wheelview/src/main/java/com/contrarywind/adapter/PreviewAdapter.java b/wheelview/src/main/java/com/contrarywind/adapter/PreviewAdapter.java new file mode 100644 index 00000000..0d9522cb --- /dev/null +++ b/wheelview/src/main/java/com/contrarywind/adapter/PreviewAdapter.java @@ -0,0 +1,36 @@ +package com.contrarywind.adapter; + +import java.util.ArrayList; + +/** + * Email:angcyo@126.com + * + * @author angcyo + * @date 2020/03/12 + * Copyright (c) 2019 ShenZhen O&M Cloud Co., Ltd. All rights reserved. + */ +public class PreviewAdapter implements WheelAdapter { + + private ArrayList itemList = new ArrayList<>(); + + public PreviewAdapter() { + for (int i = 0; i < 5; i++) { + itemList.add("Item " + i); + } + } + + @Override + public int getItemsCount() { + return itemList.size(); + } + + @Override + public String getItem(int index) { + return itemList.get(index); + } + + @Override + public int indexOf(String o) { + return itemList.indexOf(o); + } +} diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index fba8eb94..6a16438e 100644 --- a/wheelview/src/main/java/com/contrarywind/view/WheelView.java +++ b/wheelview/src/main/java/com/contrarywind/view/WheelView.java @@ -16,6 +16,7 @@ import android.view.MotionEvent; import android.view.View; +import com.contrarywind.adapter.PreviewAdapter; import com.contrarywind.adapter.WheelAdapter; import com.contrarywind.interfaces.IPickerViewData; import com.contrarywind.listener.LoopViewGestureListener; @@ -160,6 +161,10 @@ public WheelView(Context context, AttributeSet attrs) { judgeLineSpace(); initLoopView(context); + + if (isInEditMode()) { + setAdapter(new PreviewAdapter()); + } } /** @@ -533,7 +538,9 @@ protected void onDraw(Canvas canvas) { // 其他条目 canvas.save(); canvas.clipRect(0, 0, measuredWidth, (int) (itemHeight)); - canvas.scale(1.0F, (float) Math.sin(radian) * SCALE_CONTENT); + if (!isInEditMode()) { + canvas.scale(1.0F, (float) Math.sin(radian) * SCALE_CONTENT); + } setOutPaintStyle(offsetCoefficient, angle); // 控制文字水平偏移距离 canvas.drawText(contentText, drawOutContentStart + textXOffset * offsetCoefficient, maxTextHeight, paintOuterText); From 001cde9d49b553429c614ac4314a52b270fa3da0 Mon Sep 17 00:00:00 2001 From: angcyo Date: Thu, 12 Mar 2020 16:13:06 +0800 Subject: [PATCH 03/12] =?UTF-8?q?+=20=E6=8F=90=E4=BE=9Bxml=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E7=94=A8=E4=BA=8E=E9=A2=84=E8=A7=88,=20label=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=BB=98=E5=88=B6=E5=9C=A8item=E7=9A=84=E5=90=8E?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/contrarywind/view/WheelView.java | 40 +++++++++--- wheelview/src/main/res/values/attrs.xml | 61 +++++++++++++++---- 2 files changed, 82 insertions(+), 19 deletions(-) diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index 6a16438e..307c12b0 100644 --- a/wheelview/src/main/java/com/contrarywind/view/WheelView.java +++ b/wheelview/src/main/java/com/contrarywind/view/WheelView.java @@ -54,6 +54,9 @@ public enum DividerType { // 分隔线类型 private boolean isOptions = false; private boolean isCenterLabel = true; + //在item后面 绘制 label + private boolean isDrawLabelOnTextBehind = false; + private float labelTextXOffset; // Timer mTimer; private ScheduledExecutorService mExecutor = Executors.newSingleThreadScheduledExecutor(); @@ -136,6 +139,7 @@ public WheelView(Context context, AttributeSet attrs) { DisplayMetrics dm = getResources().getDisplayMetrics(); float density = dm.density; // 屏幕密度比(0.75/1.0/1.5/2.0/3.0) + labelTextXOffset = density * 4; if (density < 1) {//根据密度不同进行适配 CENTER_CONTENT_OFFSET = 2.4F; @@ -156,6 +160,17 @@ public WheelView(Context context, AttributeSet attrs) { dividerWidth = a.getDimensionPixelSize(R.styleable.pickerview_wheelview_dividerWidth, 2); textSize = a.getDimensionPixelOffset(R.styleable.pickerview_wheelview_textSize, textSize); lineSpacingMultiplier = a.getFloat(R.styleable.pickerview_wheelview_lineSpacingMultiplier, lineSpacingMultiplier); + isLoop = a.getBoolean(R.styleable.pickerview_wheelview_loop, true); + isOptions = a.getBoolean(R.styleable.pickerview_wheelview_isOptions, isOptions); + isCenterLabel = a.getBoolean(R.styleable.pickerview_wheelview_isCenterLabel, isCenterLabel); + initPosition = a.getInt(R.styleable.pickerview_wheelview_initPosition, -1); + if (a.hasValue(R.styleable.pickerview_wheelview_label)) { + label = a.getString(R.styleable.pickerview_wheelview_label); + } + isDrawLabelOnTextBehind = a.getBoolean(R.styleable.pickerview_wheelview_isDrawLabelOnTextBehind, isDrawLabelOnTextBehind); + if (a.hasValue(R.styleable.pickerview_wheelview_labelTextXOffset)) { + labelTextXOffset = a.getDimensionPixelOffset(R.styleable.pickerview_wheelview_labelTextXOffset, (int) labelTextXOffset); + } a.recycle();//回收内存 } @@ -183,10 +198,8 @@ private void initLoopView(Context context) { handler = new MessageHandler(this); gestureDetector = new GestureDetector(context, new LoopViewGestureListener(this)); gestureDetector.setIsLongpressEnabled(false); - isLoop = true; totalScrollY = 0; - initPosition = -1; initPaints(); } @@ -445,10 +458,16 @@ protected void onDraw(Canvas canvas) { } //只显示选中项Label文字的模式,并且Label文字不为空,则进行绘制 - if (!TextUtils.isEmpty(label) && isCenterLabel) { - //绘制文字,靠右并留出空隙 - int drawRightContentStart = measuredWidth - getTextWidth(paintCenterText, label); - canvas.drawText(label, drawRightContentStart - CENTER_CONTENT_OFFSET, centerY, paintCenterText); + if (!TextUtils.isEmpty(label)) { + if (isDrawLabelOnTextBehind) { + //取第0个计算坐标 + int textWidth = measuredCenterContentStart(getContentText(adapter.getItem(0))); + canvas.drawText(label, drawCenterContentStart + labelTextXOffset + textWidth + CENTER_CONTENT_OFFSET, centerY, paintCenterText); + } else if (isCenterLabel) { + //绘制文字,靠右并留出空隙 + int drawRightContentStart = measuredWidth - getTextWidth(paintCenterText, label); + canvas.drawText(label, drawRightContentStart - CENTER_CONTENT_OFFSET, centerY, paintCenterText); + } } // 设置数组中每个元素的值 @@ -486,7 +505,11 @@ protected void onDraw(Canvas canvas) { //如果是label每项都显示的模式,并且item内容不为空、label 也不为空 if (!isCenterLabel && !TextUtils.isEmpty(label) && !TextUtils.isEmpty(getContentText(showText))) { - contentText = getContentText(showText) + label; + if (isDrawLabelOnTextBehind) { + contentText = getContentText(showText); + } else { + contentText = getContentText(showText) + label; + } } else { contentText = getContentText(showText); } @@ -627,7 +650,7 @@ private String getFixNum(int timeNum) { return timeNum >= 0 && timeNum < 10 ? TIME_NUM[timeNum] : String.valueOf(timeNum); } - private void measuredCenterContentStart(String content) { + private int measuredCenterContentStart(String content) { Rect rect = new Rect(); paintCenterText.getTextBounds(content, 0, content.length(), rect); switch (mGravity) { @@ -645,6 +668,7 @@ private void measuredCenterContentStart(String content) { drawCenterContentStart = measuredWidth - rect.width() - (int) CENTER_CONTENT_OFFSET; break; } + return rect.width(); } private void measuredOutContentStart(String content) { diff --git a/wheelview/src/main/res/values/attrs.xml b/wheelview/src/main/res/values/attrs.xml index e7cae4d3..8aa25572 100644 --- a/wheelview/src/main/res/values/attrs.xml +++ b/wheelview/src/main/res/values/attrs.xml @@ -1,16 +1,55 @@ + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From c17a8839d8d865052e41827401b8d53ffba09815 Mon Sep 17 00:00:00 2001 From: angcyo Date: Thu, 12 Mar 2020 17:34:55 +0800 Subject: [PATCH 04/12] =?UTF-8?q?+=20set=20get=20=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/contrarywind/view/WheelView.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index 307c12b0..0a3af9c4 100644 --- a/wheelview/src/main/java/com/contrarywind/view/WheelView.java +++ b/wheelview/src/main/java/com/contrarywind/view/WheelView.java @@ -869,4 +869,48 @@ public int getInitPosition() { public Handler getHandler() { return handler; } + + public boolean isOptions() { + return isOptions; + } + + public void setOptions(boolean options) { + isOptions = options; + } + + public boolean isCenterLabel() { + return isCenterLabel; + } + + public void setCenterLabel(boolean centerLabel) { + isCenterLabel = centerLabel; + } + + public boolean isDrawLabelOnTextBehind() { + return isDrawLabelOnTextBehind; + } + + public void setDrawLabelOnTextBehind(boolean drawLabelOnTextBehind) { + isDrawLabelOnTextBehind = drawLabelOnTextBehind; + } + + public float getLabelTextXOffset() { + return labelTextXOffset; + } + + public void setLabelTextXOffset(float labelTextXOffset) { + this.labelTextXOffset = labelTextXOffset; + } + + public String getLabel() { + return label; + } + + public int getTextSize() { + return textSize; + } + + public void setTextSize(int textSize) { + this.textSize = textSize; + } } \ No newline at end of file From e27d5d3e498b3811a1e76a50f9c753ce51dcef59 Mon Sep 17 00:00:00 2001 From: angcyo Date: Sat, 12 Feb 2022 13:56:10 +0800 Subject: [PATCH 05/12] * rang --- .../java/com/bigkoo/pickerview/view/WheelTime.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java index 9bc45b65..2b431231 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java @@ -786,10 +786,13 @@ public void setRangDate(Calendar startDate, Calendar endDate) { } else if (startDate != null && endDate != null) { this.startYear = startDate.get(Calendar.YEAR); this.endYear = endDate.get(Calendar.YEAR); - this.startMonth = startDate.get(Calendar.MONTH) + 1; - this.endMonth = endDate.get(Calendar.MONTH) + 1; - this.startDay = startDate.get(Calendar.DAY_OF_MONTH); - this.endDay = endDate.get(Calendar.DAY_OF_MONTH); + if (this.startYear == this.endYear) { + //只有在开始年份和结束年份一样的时候, 在设置月/日限制, 才有意义. 2022-2-12 + this.startMonth = startDate.get(Calendar.MONTH) + 1; + this.endMonth = endDate.get(Calendar.MONTH) + 1; + this.startDay = startDate.get(Calendar.DAY_OF_MONTH); + this.endDay = endDate.get(Calendar.DAY_OF_MONTH); + } } } From 5dbee73504dcd66a85f500f9f7fffecb5448f226 Mon Sep 17 00:00:00 2001 From: angcyo Date: Sat, 12 Feb 2022 14:54:25 +0800 Subject: [PATCH 06/12] * fix rang --- .../com/bigkoo/pickerview/view/WheelTime.java | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java index 2b431231..7fe5877a 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java @@ -406,15 +406,21 @@ public void onItemSelected(int index) { } } else if (year_num == startYear) {//等于开始的年 //重新设置月份 - wv_month.setAdapter(new NumericWheelAdapter(startMonth, 12)); + + int _startMonth = startMonth; + if (_startDate != null) { + _startMonth = _startDate.get(Calendar.MONTH) + 1; + } + + wv_month.setAdapter(new NumericWheelAdapter(_startMonth, 12)); if (currentMonthItem > wv_month.getAdapter().getItemsCount() - 1) { currentMonthItem = wv_month.getAdapter().getItemsCount() - 1; wv_month.setCurrentItem(currentMonthItem); } - int month = currentMonthItem + startMonth; - if (month == startMonth) { + int month = currentMonthItem + _startMonth; + if (month == _startMonth) { //重新设置日 setReDay(year_num, month, startDay, 31, list_big, list_little); } else { @@ -424,14 +430,20 @@ public void onItemSelected(int index) { } else if (year_num == endYear) { //重新设置月份 - wv_month.setAdapter(new NumericWheelAdapter(1, endMonth)); + + int _endMonth = endMonth; + if (_endDate != null) { + _endMonth = _endDate.get(Calendar.MONTH) + 1; + } + + wv_month.setAdapter(new NumericWheelAdapter(1, _endMonth)); if (currentMonthItem > wv_month.getAdapter().getItemsCount() - 1) { currentMonthItem = wv_month.getAdapter().getItemsCount() - 1; wv_month.setCurrentItem(currentMonthItem); } int monthNum = currentMonthItem + 1; - if (monthNum == endMonth) { + if (monthNum == _endMonth) { //重新设置日 setReDay(year_num, monthNum, 1, endDay, list_big, list_little); } else { @@ -477,7 +489,15 @@ public void onItemSelected(int index) { month_num = month_num + startMonth - 1; if (month_num == startMonth) { //重新设置日 - setReDay(currentYear, month_num, startDay, 31, list_big, list_little); + + int _startDay = startDay; + if (_startDate != null) { + if (_startDate.get(Calendar.MONTH) + 1 == month_num) { + _startDay = _startDate.get(Calendar.DAY_OF_MONTH); + } + } + + setReDay(currentYear, month_num, _startDay, 31, list_big, list_little); } else { //重新设置日 setReDay(currentYear, month_num, 1, 31, list_big, list_little); @@ -486,7 +506,14 @@ public void onItemSelected(int index) { } else if (currentYear == endYear) { if (month_num == endMonth) { //重新设置日 - setReDay(currentYear, wv_month.getCurrentItem() + 1, 1, endDay, list_big, list_little); + int _endDay = endDay; + if (_endDate != null) { + if (_endDate.get(Calendar.MONTH) + 1 == month_num) { + _endDay = _endDate.get(Calendar.DAY_OF_MONTH); + } + } + + setReDay(currentYear, wv_month.getCurrentItem() + 1, 1, _endDay, list_big, list_little); } else { setReDay(currentYear, wv_month.getCurrentItem() + 1, 1, 31, list_big, list_little); } @@ -736,9 +763,13 @@ public void setEndYear(int endYear) { this.endYear = endYear; } + protected Calendar _startDate, _endDate; public void setRangDate(Calendar startDate, Calendar endDate) { + _startDate = startDate; + _endDate = endDate; + if (startDate == null && endDate != null) { int year = endDate.get(Calendar.YEAR); int month = endDate.get(Calendar.MONTH) + 1; From b0c9a793a330e0fed83fe6857f88af43a64d458d Mon Sep 17 00:00:00 2001 From: angcyo Date: Sat, 12 Feb 2022 15:33:53 +0800 Subject: [PATCH 07/12] Revert "* fix rang" This reverts commit 5dbee73504dcd66a85f500f9f7fffecb5448f226. --- .../com/bigkoo/pickerview/view/WheelTime.java | 45 +++---------------- 1 file changed, 7 insertions(+), 38 deletions(-) diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java index 7fe5877a..2b431231 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java @@ -406,21 +406,15 @@ public void onItemSelected(int index) { } } else if (year_num == startYear) {//等于开始的年 //重新设置月份 - - int _startMonth = startMonth; - if (_startDate != null) { - _startMonth = _startDate.get(Calendar.MONTH) + 1; - } - - wv_month.setAdapter(new NumericWheelAdapter(_startMonth, 12)); + wv_month.setAdapter(new NumericWheelAdapter(startMonth, 12)); if (currentMonthItem > wv_month.getAdapter().getItemsCount() - 1) { currentMonthItem = wv_month.getAdapter().getItemsCount() - 1; wv_month.setCurrentItem(currentMonthItem); } - int month = currentMonthItem + _startMonth; - if (month == _startMonth) { + int month = currentMonthItem + startMonth; + if (month == startMonth) { //重新设置日 setReDay(year_num, month, startDay, 31, list_big, list_little); } else { @@ -430,20 +424,14 @@ public void onItemSelected(int index) { } else if (year_num == endYear) { //重新设置月份 - - int _endMonth = endMonth; - if (_endDate != null) { - _endMonth = _endDate.get(Calendar.MONTH) + 1; - } - - wv_month.setAdapter(new NumericWheelAdapter(1, _endMonth)); + wv_month.setAdapter(new NumericWheelAdapter(1, endMonth)); if (currentMonthItem > wv_month.getAdapter().getItemsCount() - 1) { currentMonthItem = wv_month.getAdapter().getItemsCount() - 1; wv_month.setCurrentItem(currentMonthItem); } int monthNum = currentMonthItem + 1; - if (monthNum == _endMonth) { + if (monthNum == endMonth) { //重新设置日 setReDay(year_num, monthNum, 1, endDay, list_big, list_little); } else { @@ -489,15 +477,7 @@ public void onItemSelected(int index) { month_num = month_num + startMonth - 1; if (month_num == startMonth) { //重新设置日 - - int _startDay = startDay; - if (_startDate != null) { - if (_startDate.get(Calendar.MONTH) + 1 == month_num) { - _startDay = _startDate.get(Calendar.DAY_OF_MONTH); - } - } - - setReDay(currentYear, month_num, _startDay, 31, list_big, list_little); + setReDay(currentYear, month_num, startDay, 31, list_big, list_little); } else { //重新设置日 setReDay(currentYear, month_num, 1, 31, list_big, list_little); @@ -506,14 +486,7 @@ public void onItemSelected(int index) { } else if (currentYear == endYear) { if (month_num == endMonth) { //重新设置日 - int _endDay = endDay; - if (_endDate != null) { - if (_endDate.get(Calendar.MONTH) + 1 == month_num) { - _endDay = _endDate.get(Calendar.DAY_OF_MONTH); - } - } - - setReDay(currentYear, wv_month.getCurrentItem() + 1, 1, _endDay, list_big, list_little); + setReDay(currentYear, wv_month.getCurrentItem() + 1, 1, endDay, list_big, list_little); } else { setReDay(currentYear, wv_month.getCurrentItem() + 1, 1, 31, list_big, list_little); } @@ -763,13 +736,9 @@ public void setEndYear(int endYear) { this.endYear = endYear; } - protected Calendar _startDate, _endDate; public void setRangDate(Calendar startDate, Calendar endDate) { - _startDate = startDate; - _endDate = endDate; - if (startDate == null && endDate != null) { int year = endDate.get(Calendar.YEAR); int month = endDate.get(Calendar.MONTH) + 1; From 9cb9c229b3c179810f6f952df94705df13af4b7a Mon Sep 17 00:00:00 2001 From: angcyo Date: Thu, 2 Jun 2022 15:14:58 +0800 Subject: [PATCH 08/12] + scroll state --- .../src/main/java/com/contrarywind/view/WheelView.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index 0a3af9c4..50af669a 100644 --- a/wheelview/src/main/java/com/contrarywind/view/WheelView.java +++ b/wheelview/src/main/java/com/contrarywind/view/WheelView.java @@ -286,11 +286,18 @@ public void smoothScroll(ACTION action) {//平滑滚动的实现 } //停止的时候,位置有偏移,不是全部都能正确停止到中间位置的,这里把文字位置挪回中间去 mFuture = mExecutor.scheduleWithFixedDelay(new SmoothScrollTimerTask(this, mOffset), 0, 10, TimeUnit.MILLISECONDS); + isScrollSetting = true; } + /** + * 是否正在滚动中 + */ + public boolean isScrollSetting = false; + public final void scrollBy(float velocityY) {//滚动惯性的实现 cancelFuture(); mFuture = mExecutor.scheduleWithFixedDelay(new InertiaTimerTask(this, velocityY), 0, VELOCITY_FLING, TimeUnit.MILLISECONDS); + isScrollSetting = true; } public void cancelFuture() { @@ -368,6 +375,7 @@ public final int getCurrentItem() { } public final void onItemSelected() { + isScrollSetting = false; if (onItemSelectedListener != null) { postDelayed(new Runnable() { @Override From b4b0b3b0a3517334acf3cfc40871749e61586e79 Mon Sep 17 00:00:00 2001 From: angcyo Date: Tue, 28 Jun 2022 10:47:05 +0800 Subject: [PATCH 09/12] * fix text size --- .../src/main/java/com/contrarywind/view/WheelView.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index 50af669a..1795aa3f 100644 --- a/wheelview/src/main/java/com/contrarywind/view/WheelView.java +++ b/wheelview/src/main/java/com/contrarywind/view/WheelView.java @@ -918,7 +918,11 @@ public int getTextSize() { return textSize; } - public void setTextSize(int textSize) { - this.textSize = textSize; + /** + * @param textSize 文本大小, 像素单位 + */ + public void setTextSizePixel(int textSize) { + paintOuterText.setTextSize(textSize); + paintCenterText.setTextSize(textSize); } } \ No newline at end of file From fb07c7ebcb9083be6fe69fccc3eddc1aa8c81b74 Mon Sep 17 00:00:00 2001 From: angcyo Date: Sat, 3 Sep 2022 10:07:40 +0800 Subject: [PATCH 10/12] * fix text size unit --- .../TestCircleWheelViewActivity.java | 2 +- .../pickerview/configure/PickerOptions.java | 2 +- .../pickerview/view/TimePickerView.java | 3 +- .../bigkoo/pickerview/view/WheelOptions.java | 8 ++--- .../com/bigkoo/pickerview/view/WheelTime.java | 8 ++--- .../java/com/contrarywind/view/WheelView.java | 31 +++++++++---------- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/bigkoo/pickerviewdemo/TestCircleWheelViewActivity.java b/app/src/main/java/com/bigkoo/pickerviewdemo/TestCircleWheelViewActivity.java index 1906d93f..697d57cd 100644 --- a/app/src/main/java/com/bigkoo/pickerviewdemo/TestCircleWheelViewActivity.java +++ b/app/src/main/java/com/bigkoo/pickerviewdemo/TestCircleWheelViewActivity.java @@ -25,7 +25,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { setContentView(R.layout.activity_test_circle_wheelview); WheelView wheelView = findViewById(R.id.wheelview); - wheelView.setTextSize(20); + wheelView.setTextSizeDp(20); wheelView.setLineSpacingMultiplier(2f); // wheelView.setDividerWidth(6); wheelView.setDividerType(WheelView.DividerType.CIRCLE); diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java b/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java index 44c85be9..f7be0576 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java @@ -95,7 +95,7 @@ public PickerOptions(int buildType) { public int textSizeSubmitCancel = 17;//确定取消按钮大小 public int textSizeTitle = 18;//标题文字大小 - public int textSizeContent = 18;//内容文字大小 + public int textSizeContent = 18;//内容文字大小 dp public int textColorOut = 0xFFa8a8a8; //分割线以外的文字颜色 public int textColorCenter = 0xFF2a2a2a; //分割线之间的文字颜色 diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java index 2313a626..c36f1b7a 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java @@ -83,7 +83,8 @@ private void initView(Context context) { } private void initWheelTime(LinearLayout timePickerView) { - wheelTime = new WheelTime(timePickerView, mPickerOptions.type, mPickerOptions.textGravity, mPickerOptions.textSizeContent); + wheelTime = new WheelTime(timePickerView, mPickerOptions.type, mPickerOptions.textGravity, + mPickerOptions.textSizeContent * timePickerView.getContext().getResources().getDisplayMetrics().density); if (mPickerOptions.timeSelectChangeListener != null) { wheelTime.setSelectChangeCallback(new ISelectTimeCallback() { @Override diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelOptions.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelOptions.java index 75017a15..3890ca47 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelOptions.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelOptions.java @@ -216,10 +216,10 @@ public void onItemSelected(int index) { } } - public void setTextContentSize(int textSize) { - wv_option1.setTextSize(textSize); - wv_option2.setTextSize(textSize); - wv_option3.setTextSize(textSize); + public void setTextContentSize(float textSize) { + wv_option1.setTextSizeDp(textSize); + wv_option2.setTextSizeDp(textSize); + wv_option3.setTextSizeDp(textSize); } private void setLineSpacingMultiplier() { diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java index 2b431231..5d8f0f57 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java @@ -1,5 +1,6 @@ package com.bigkoo.pickerview.view; +import android.support.annotation.Px; import android.view.View; import com.bigkoo.pickerview.R; @@ -45,12 +46,13 @@ public class WheelTime { private int endDay = DEFAULT_END_DAY; //表示31天的 private int currentYear; - private int textSize; + @Px + private float textSize; private boolean isLunarCalendar = false; private ISelectTimeCallback mSelectChangeCallback; - public WheelTime(View view, boolean[] type, int gravity, int textSize) { + public WheelTime(View view, boolean[] type, int gravity, @Px float textSize) { super(); this.view = view; this.type = type; @@ -571,7 +573,6 @@ private void setReDay(int year_num, int monthNum, int startD, int endD, List 0.0F) { - textSize = (int) (context.getResources().getDisplayMetrics().density * size); + textSize = size; paintOuterText.setTextSize(textSize); paintCenterText.setTextSize(textSize); } } + public final void setTextSizeDp(float size) { + if (size > 0.0F) { + textSize = context.getResources().getDisplayMetrics().density * size; + setTextSize(textSize); + } + } + public final void setCurrentItem(int currentItem) { //不添加这句,当这个wheelView不可见时,默认都是0,会导致获取到的时间错误 this.selectedItem = currentItem; @@ -611,7 +618,7 @@ private void reMeasureTextSize(String contentText) { Rect rect = new Rect(); paintCenterText.getTextBounds(contentText, 0, contentText.length(), rect); int width = rect.width(); - int size = textSize; + float size = textSize; while (width > measuredWidth) { size--; //设置2条横线中间的文字大小 @@ -914,15 +921,7 @@ public String getLabel() { return label; } - public int getTextSize() { + public float getTextSize() { return textSize; } - - /** - * @param textSize 文本大小, 像素单位 - */ - public void setTextSizePixel(int textSize) { - paintOuterText.setTextSize(textSize); - paintCenterText.setTextSize(textSize); - } } \ No newline at end of file From 1f74fef03a29f7561d291e77a5c1d255d30c3058 Mon Sep 17 00:00:00 2001 From: angcyo Date: Wed, 28 Sep 2022 11:09:09 +0800 Subject: [PATCH 11/12] * update androidx --- app/build.gradle | 8 ++++---- app/src/main/AndroidManifest.xml | 16 ++++++++++------ .../pickerviewdemo/FragmentTestActivity.java | 9 +++++---- .../bigkoo/pickerviewdemo/JsonDataActivity.java | 2 +- .../com/bigkoo/pickerviewdemo/MainActivity.java | 2 +- .../TestCircleWheelViewActivity.java | 4 ++-- .../com/bigkoo/pickerviewdemo/TestFragment.java | 6 ++++-- build.gradle | 4 ++-- gradle.properties | 4 +++- gradle/wrapper/gradle-wrapper.properties | 2 +- pickerview/build.gradle | 8 ++++---- .../pickerview/builder/OptionsPickerBuilder.java | 2 +- .../pickerview/builder/TimePickerBuilder.java | 2 +- .../com/bigkoo/pickerview/view/WheelTime.java | 2 +- wheelview/build.gradle | 8 ++++---- .../java/com/contrarywind/view/WheelView.java | 2 +- 16 files changed, 45 insertions(+), 36 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b22ece5f..f453bf0d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 26 + compileSdkVersion 32 defaultConfig { applicationId "com.bigkoo.pickerviewdemo" minSdkVersion 14 - targetSdkVersion 26 + targetSdkVersion 32 versionCode 1 versionName "1.0" } @@ -27,6 +27,6 @@ dependencies { implementation project(':pickerview') // implementation 'com.contrarywind:Android-PickerView:4.1.9' // implementation 'com.contrarywind:wheelview:4.1.0' - implementation 'com.android.support:appcompat-v7:26.1.0' - implementation 'com.google.code.gson:gson:2.7' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'com.google.code.gson:gson:2.9.1' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2d63adce..ce4f2c5b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ android:theme="@style/AppTheme"> @@ -16,12 +17,15 @@ - - - - - - + + + \ No newline at end of file diff --git a/app/src/main/java/com/bigkoo/pickerviewdemo/FragmentTestActivity.java b/app/src/main/java/com/bigkoo/pickerviewdemo/FragmentTestActivity.java index 44e8174c..6cf31d73 100644 --- a/app/src/main/java/com/bigkoo/pickerviewdemo/FragmentTestActivity.java +++ b/app/src/main/java/com/bigkoo/pickerviewdemo/FragmentTestActivity.java @@ -2,10 +2,11 @@ import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentTransaction; -import android.support.v7.app.AppCompatActivity; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; public class FragmentTestActivity extends AppCompatActivity { private FragmentManager mFragmentManager; diff --git a/app/src/main/java/com/bigkoo/pickerviewdemo/JsonDataActivity.java b/app/src/main/java/com/bigkoo/pickerviewdemo/JsonDataActivity.java index 0278233b..bde3c7e0 100644 --- a/app/src/main/java/com/bigkoo/pickerviewdemo/JsonDataActivity.java +++ b/app/src/main/java/com/bigkoo/pickerviewdemo/JsonDataActivity.java @@ -5,7 +5,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.Message; -import android.support.v7.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatActivity; import android.view.View; import android.widget.Toast; diff --git a/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java b/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java index 930a6c5b..d5f12652 100644 --- a/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java +++ b/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java @@ -4,7 +4,7 @@ import android.content.Intent; import android.graphics.Color; import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatActivity; import android.util.Log; import android.view.Gravity; import android.view.View; diff --git a/app/src/main/java/com/bigkoo/pickerviewdemo/TestCircleWheelViewActivity.java b/app/src/main/java/com/bigkoo/pickerviewdemo/TestCircleWheelViewActivity.java index 697d57cd..9bf4748f 100644 --- a/app/src/main/java/com/bigkoo/pickerviewdemo/TestCircleWheelViewActivity.java +++ b/app/src/main/java/com/bigkoo/pickerviewdemo/TestCircleWheelViewActivity.java @@ -1,8 +1,8 @@ package com.bigkoo.pickerviewdemo; import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v7.app.AppCompatActivity; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; import android.widget.Toast; import com.bigkoo.pickerview.adapter.ArrayWheelAdapter; diff --git a/app/src/main/java/com/bigkoo/pickerviewdemo/TestFragment.java b/app/src/main/java/com/bigkoo/pickerviewdemo/TestFragment.java index 56b7d2a5..7b5a4448 100644 --- a/app/src/main/java/com/bigkoo/pickerviewdemo/TestFragment.java +++ b/app/src/main/java/com/bigkoo/pickerviewdemo/TestFragment.java @@ -3,8 +3,10 @@ import android.graphics.Color; import android.os.Bundle; -import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; + +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; + import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/build.gradle b/build.gradle index 595c3eab..808cc6dd 100644 --- a/build.gradle +++ b/build.gradle @@ -9,9 +9,9 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.1' + classpath 'com.android.tools.build:gradle:7.2.2' // classpath "com.novoda:bintray-release:0.9.1" - classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' + //classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } diff --git a/gradle.properties b/gradle.properties index 1d3591c8..2a8e7cc6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,4 +15,6 @@ # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true \ No newline at end of file +# org.gradle.parallel=true +android.enableJetifier=false +android.useAndroidX=true \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 70da1afe..40d9eebc 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip diff --git a/pickerview/build.gradle b/pickerview/build.gradle index 9f398512..f8adf5e0 100644 --- a/pickerview/build.gradle +++ b/pickerview/build.gradle @@ -1,15 +1,15 @@ apply plugin: 'com.android.library' -apply plugin: 'com.github.dcendents.android-maven' +//apply plugin: 'com.github.dcendents.android-maven' //apply plugin: 'com.novoda.bintray-release'//添加JCenter插件 android { - compileSdkVersion 26 + compileSdkVersion 32 defaultConfig { minSdkVersion 14 - targetSdkVersion 26 + targetSdkVersion 32 versionCode 34 versionName "4.1.9" } @@ -46,5 +46,5 @@ dependencies { // compile fileTree(include: ['*.jar'], dir: 'libs') api project(path: ':wheelview') // api 'com.contrarywind:wheelview:4.1.0' - implementation 'com.android.support:support-annotations:28.0.0' + implementation 'androidx.annotation:annotation:1.5.0' } \ No newline at end of file diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/builder/OptionsPickerBuilder.java b/pickerview/src/main/java/com/bigkoo/pickerview/builder/OptionsPickerBuilder.java index 84eaa165..46a9e5ed 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/builder/OptionsPickerBuilder.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/builder/OptionsPickerBuilder.java @@ -2,7 +2,7 @@ import android.content.Context; import android.graphics.Typeface; -import android.support.annotation.ColorInt; +import androidx.annotation.ColorInt; import android.view.View; import android.view.ViewGroup; diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java b/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java index 9fcd75ca..d11b93ca 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java @@ -1,7 +1,7 @@ package com.bigkoo.pickerview.builder; import android.content.Context; -import android.support.annotation.ColorInt; +import androidx.annotation.ColorInt; import android.view.View; import android.view.ViewGroup; diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java index 5d8f0f57..84a5fc41 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java @@ -1,6 +1,6 @@ package com.bigkoo.pickerview.view; -import android.support.annotation.Px; +import androidx.annotation.Px; import android.view.View; import com.bigkoo.pickerview.R; diff --git a/wheelview/build.gradle b/wheelview/build.gradle index b90293d8..8364bd00 100644 --- a/wheelview/build.gradle +++ b/wheelview/build.gradle @@ -1,15 +1,15 @@ apply plugin: 'com.android.library' -apply plugin: 'com.github.dcendents.android-maven' +//apply plugin: 'com.github.dcendents.android-maven' //apply plugin: 'com.novoda.bintray-release'//添加插件 android { - compileSdkVersion 26 + compileSdkVersion 32 defaultConfig { minSdkVersion 14 - targetSdkVersion 26 + targetSdkVersion 32 versionCode 31 versionName "4.1.0" } @@ -44,5 +44,5 @@ android { dependencies { // compile fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.android.support:support-annotations:28.0.0' + implementation 'androidx.annotation:annotation:1.5.0' } \ No newline at end of file diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index c4321892..77e1c22f 100644 --- a/wheelview/src/main/java/com/contrarywind/view/WheelView.java +++ b/wheelview/src/main/java/com/contrarywind/view/WheelView.java @@ -7,7 +7,7 @@ import android.graphics.Rect; import android.graphics.Typeface; import android.os.Handler; -import android.support.annotation.Px; +import androidx.annotation.Px; import android.text.TextUtils; import android.util.AttributeSet; import android.util.DisplayMetrics; From 458839f118f6d9eadb6d1892939369e5d0640fc8 Mon Sep 17 00:00:00 2001 From: angcyo Date: Fri, 30 Dec 2022 11:35:44 +0800 Subject: [PATCH 12/12] + IWheelDraw --- .../com/contrarywind/adapter/IWheelDraw.java | 20 ++++++++++ .../java/com/contrarywind/view/WheelView.java | 37 ++++++++++++++++--- 2 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 wheelview/src/main/java/com/contrarywind/adapter/IWheelDraw.java diff --git a/wheelview/src/main/java/com/contrarywind/adapter/IWheelDraw.java b/wheelview/src/main/java/com/contrarywind/adapter/IWheelDraw.java new file mode 100644 index 00000000..7984350e --- /dev/null +++ b/wheelview/src/main/java/com/contrarywind/adapter/IWheelDraw.java @@ -0,0 +1,20 @@ +package com.contrarywind.adapter; + +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Rect; + +import com.contrarywind.view.WheelView; + +/** + * @author angcyo + * @since 2022/12/30 + */ +public interface IWheelDraw { + + /** + * 绘制文本时, 额外需要绘制的东西 + */ + void onDrawOnText(WheelView wheelView, Canvas canvas, String text, float textDrawX, float textDrawY, Paint textDrawPaint, int index, Rect textBounds); + +} diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index 77e1c22f..887d4d32 100644 --- a/wheelview/src/main/java/com/contrarywind/view/WheelView.java +++ b/wheelview/src/main/java/com/contrarywind/view/WheelView.java @@ -7,7 +7,6 @@ import android.graphics.Rect; import android.graphics.Typeface; import android.os.Handler; -import androidx.annotation.Px; import android.text.TextUtils; import android.util.AttributeSet; import android.util.DisplayMetrics; @@ -17,6 +16,9 @@ import android.view.MotionEvent; import android.view.View; +import androidx.annotation.Px; + +import com.contrarywind.adapter.IWheelDraw; import com.contrarywind.adapter.PreviewAdapter; import com.contrarywind.adapter.WheelAdapter; import com.contrarywind.interfaces.IPickerViewData; @@ -431,7 +433,6 @@ protected void onDraw(Canvas canvas) { //跟滚动流畅度有关,总滑动距离与每个item高度取余,即并不是一格格的滚动,每个item不一定滚到对应Rect里的,这个item对应格子的偏移值 float itemHeightOffset = (totalScrollY % itemHeight); - //绘制中间两条横线 if (dividerType == DividerType.WRAP) {//横线长度仅包裹内容 float startX; @@ -545,11 +546,13 @@ protected void onDraw(Canvas canvas) { canvas.scale(1.0F, (float) Math.sin(radian) * SCALE_CONTENT); setOutPaintStyle(offsetCoefficient, angle); canvas.drawText(contentText, drawOutContentStart, maxTextHeight, paintOuterText); + drawOnText(canvas, contentText, drawOutContentStart, maxTextHeight, paintOuterText, index); canvas.restore(); canvas.save(); canvas.clipRect(0, firstLineY - translateY, measuredWidth, (int) (itemHeight)); canvas.scale(1.0F, (float) Math.sin(radian) * 1.0F); canvas.drawText(contentText, drawCenterContentStart, maxTextHeight - CENTER_CONTENT_OFFSET, paintCenterText); + drawOnText(canvas, contentText, drawCenterContentStart, maxTextHeight - CENTER_CONTENT_OFFSET, paintCenterText, index); canvas.restore(); } else if (translateY <= secondLineY && maxTextHeight + translateY >= secondLineY) { // 条目经过第二条线 @@ -557,12 +560,14 @@ protected void onDraw(Canvas canvas) { canvas.clipRect(0, 0, measuredWidth, secondLineY - translateY); canvas.scale(1.0F, (float) Math.sin(radian) * 1.0F); canvas.drawText(contentText, drawCenterContentStart, maxTextHeight - CENTER_CONTENT_OFFSET, paintCenterText); + drawOnText(canvas, contentText, drawCenterContentStart, maxTextHeight - CENTER_CONTENT_OFFSET, paintCenterText, index); canvas.restore(); canvas.save(); canvas.clipRect(0, secondLineY - translateY, measuredWidth, (int) (itemHeight)); canvas.scale(1.0F, (float) Math.sin(radian) * SCALE_CONTENT); setOutPaintStyle(offsetCoefficient, angle); canvas.drawText(contentText, drawOutContentStart, maxTextHeight, paintOuterText); + drawOnText(canvas, contentText, drawOutContentStart, maxTextHeight, paintOuterText, index); canvas.restore(); } else if (translateY >= firstLineY && maxTextHeight + translateY <= secondLineY) { // 中间条目 @@ -570,6 +575,7 @@ protected void onDraw(Canvas canvas) { //让文字居中 float Y = maxTextHeight - CENTER_CONTENT_OFFSET;//因为圆弧角换算的向下取值,导致角度稍微有点偏差,加上画笔的基线会偏上,因此需要偏移量修正一下 canvas.drawText(contentText, drawCenterContentStart, Y, paintCenterText); + drawOnText(canvas, contentText, drawCenterContentStart, Y, paintCenterText, index); //设置选中项 selectedItem = preCurrentIndex - (itemsVisible / 2 - counter); } else { @@ -581,7 +587,9 @@ protected void onDraw(Canvas canvas) { } setOutPaintStyle(offsetCoefficient, angle); // 控制文字水平偏移距离 - canvas.drawText(contentText, drawOutContentStart + textXOffset * offsetCoefficient, maxTextHeight, paintOuterText); + float x = drawOutContentStart + textXOffset * offsetCoefficient; + canvas.drawText(contentText, x, maxTextHeight, paintOuterText); + drawOnText(canvas, contentText, x, maxTextHeight, paintOuterText, index); canvas.restore(); } canvas.restore(); @@ -591,6 +599,21 @@ protected void onDraw(Canvas canvas) { } } + /** + * 2022-12-30 在绘制文本的同时, 绘制其他元素 + */ + private void drawOnText(Canvas canvas, String text, float textDrawX, float textDrawY, Paint textDrawPaint, int index) { + if (adapter instanceof IWheelDraw) { + Rect textBounds; + if (textDrawPaint == paintCenterText) { + textBounds = centerContentBounds; + } else { + textBounds = outContentBounds; + } + ((IWheelDraw) adapter).onDrawOnText(this, canvas, text, textDrawX, textDrawY, textDrawPaint, index, textBounds); + } + } + //设置文字倾斜角度,透明度 private void setOutPaintStyle(float offsetCoefficient, float angle) { // 控制文字倾斜角度 @@ -665,8 +688,10 @@ private String getFixNum(int timeNum) { return timeNum >= 0 && timeNum < 10 ? TIME_NUM[timeNum] : String.valueOf(timeNum); } + public Rect centerContentBounds = new Rect(); + private int measuredCenterContentStart(String content) { - Rect rect = new Rect(); + Rect rect = centerContentBounds; paintCenterText.getTextBounds(content, 0, content.length(), rect); switch (mGravity) { case Gravity.CENTER://显示内容居中 @@ -686,8 +711,10 @@ private int measuredCenterContentStart(String content) { return rect.width(); } + public Rect outContentBounds = new Rect(); + private void measuredOutContentStart(String content) { - Rect rect = new Rect(); + Rect rect = outContentBounds; paintOuterText.getTextBounds(content, 0, content.length(), rect); switch (mGravity) { case Gravity.CENTER: