diff --git a/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java b/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java index 930a6c5b..bb83cda9 100644 --- a/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java +++ b/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java @@ -4,6 +4,7 @@ import android.content.Intent; import android.graphics.Color; import android.os.Bundle; +import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Gravity; @@ -210,6 +211,9 @@ public void onTimeSelectChanged(Date date) { }) .setType(new boolean[]{true, true, true, true, true, true}) .isDialog(true) //默认设置false ,内部实现将DecorView 作为它的父控件。 + .setDividerWidth(5)//设置选中条目分割线的线宽,单位px,若不设置默认为1 + .setDividerColor(Color.RED) + .setSelectItemBgColor(Color.GREEN)//设置选中条目的背景色,若不设置默认为0xFFe7e7e7 .addOnCancelClickListener(new View.OnClickListener() { @Override public void onClick(View view) { 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..4916efde 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java @@ -195,6 +195,26 @@ public TimePickerBuilder setDividerColor(@ColorInt int dividerColor) { return this; } + /** + * 设置分割线的宽度 + * + * @param dividerWidth + */ + public TimePickerBuilder setDividerWidth(int dividerWidth) { + mPickerOptions.dividerWidth = dividerWidth; + return this; + } + + /** + * 设置选中条目的颜色 + * + * @param selectItemBgColor + */ + public TimePickerBuilder setSelectItemBgColor(int selectItemBgColor) { + mPickerOptions.selectItemBgColor = selectItemBgColor; + return this; + } + /** * 设置分割线的类型 * 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..7a99dec6 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java @@ -100,6 +100,9 @@ public PickerOptions(int buildType) { public int textColorOut = 0xFFa8a8a8; //分割线以外的文字颜色 public int textColorCenter = 0xFF2a2a2a; //分割线之间的文字颜色 public int dividerColor = 0xFFd5d5d5; //分割线的颜色 + public int dividerWidth = 1; //分割线的宽度 + public int selectItemBgColor = 0xFFe7e7e7; //选中条目的背景色 + public int backgroundId = -1; //显示时的外部背景色颜色,默认是灰色 public int outSideColor = -1; //显示时的外部背景色颜色,默认是灰色 public float lineSpacingMultiplier = 1.6f; // 条目间距倍数 默认1.6 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..33835649 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java @@ -139,6 +139,8 @@ public void onTimeSelectChanged() { wheelTime.setCyclic(mPickerOptions.cyclic); wheelTime.setDividerColor(mPickerOptions.dividerColor); wheelTime.setDividerType(mPickerOptions.dividerType); + wheelTime.setDividerWidth(mPickerOptions.dividerWidth); + wheelTime.setSelectItemBgColor(mPickerOptions.selectItemBgColor); wheelTime.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier); wheelTime.setTextColorOut(mPickerOptions.textColorOut); wheelTime.setTextColorCenter(mPickerOptions.textColorCenter); 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..cb2c5d3e 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java @@ -47,6 +47,15 @@ public class WheelTime { private int textSize; + //文字的颜色和分割线的颜色、宽度、选中条目背景色 + private int textColorOut; + private int textColorCenter; + private int dividerColor; + private int dividerWidth; + private int selectItemBgColor; + + private float lineSpacingMultiplier; + private WheelView.DividerType dividerType; private boolean isLunarCalendar = false; private ISelectTimeCallback mSelectChangeCallback; @@ -56,6 +65,8 @@ public WheelTime(View view, boolean[] type, int gravity, int textSize) { this.type = type; this.gravity = gravity; this.textSize = textSize; + setView(view); + } } public void setLunarMode(boolean isLunarCalendar) { @@ -101,14 +112,12 @@ private void setLunar(int year, final int month, int day, boolean isLeap, int h, wv_month = (WheelView) view.findViewById(R.id.month); wv_month.setAdapter(new ArrayWheelAdapter(ChinaDate.getMonths(year))); wv_month.setLabel(""); - int leapMonth = ChinaDate.leapMonth(year); if (leapMonth != 0 && (month > leapMonth - 1 || isLeap)) { //选中月是闰月或大于闰月 wv_month.setCurrentItem(month + 1); } else { wv_month.setCurrentItem(month); } - wv_month.setGravity(gravity); // 日 @@ -153,7 +162,6 @@ public void onItemSelected(int index) { } else { wv_month.setCurrentItem(wv_month.getCurrentItem()); } - int currentIndex = wv_day.getCurrentItem(); int maxItem = 29; if (ChinaDate.leapMonth(year_num) != 0 && wv_month.getCurrentItem() > ChinaDate.leapMonth(year_num) - 1) { @@ -169,6 +177,7 @@ public void onItemSelected(int index) { maxItem = ChinaDate.monthDays(year_num, wv_month.getCurrentItem() + 1); } + if (wv_day.getCurrentItem() > maxItem - 1) { if (currentIndex > maxItem - 1) { wv_day.setCurrentItem(maxItem - 1); } @@ -200,6 +209,7 @@ public void onItemSelected(int index) { maxItem = ChinaDate.monthDays(year_num, month_num + 1); } + if (wv_day.getCurrentItem() > maxItem - 1) { if (currentIndex > maxItem - 1) { wv_day.setCurrentItem(maxItem - 1); } @@ -288,6 +298,7 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) { wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay)); } else { // 闰年 + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if (leapYear) { if (endDay > 29) { endDay = 29; @@ -310,6 +321,14 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) { wv_day.setAdapter(new NumericWheelAdapter(startDay, 30)); } else { + // 闰年 + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { + + wv_day.setAdapter(new NumericWheelAdapter(startDay, 29)); + } else { + + wv_day.setAdapter(new NumericWheelAdapter(startDay, 28)); + } // 闰年 29,平年 28 wv_day.setAdapter(new NumericWheelAdapter(startDay, leapYear ? 29 : 28)); } @@ -328,6 +347,7 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) { wv_day.setAdapter(new NumericWheelAdapter(1, endDay)); } else { // 闰年 + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if (leapYear) { if (endDay > 29) { endDay = 29; @@ -344,6 +364,20 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) { } else { // 判断大小月及是否闰年,用来确定"日"的数据 if (list_big.contains(String.valueOf(month + 1))) { + + wv_day.setAdapter(new NumericWheelAdapter(1, 31)); + } else if (list_little.contains(String.valueOf(month + 1))) { + + wv_day.setAdapter(new NumericWheelAdapter(1, 30)); + } else { + // 闰年 + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { + + wv_day.setAdapter(new NumericWheelAdapter(1, 29)); + } else { + + wv_day.setAdapter(new NumericWheelAdapter(1, 28)); + } wv_day.setAdapter(new NumericWheelAdapter(1, 31)); } else if (list_little.contains(String.valueOf(month + 1))) { wv_day.setAdapter(new NumericWheelAdapter(1, 30)); @@ -582,6 +616,71 @@ private void setContentTextSize() { } + private void setTextColorOut() { + wv_day.setTextColorOut(textColorOut); + wv_month.setTextColorOut(textColorOut); + wv_year.setTextColorOut(textColorOut); + wv_hours.setTextColorOut(textColorOut); + wv_minutes.setTextColorOut(textColorOut); + wv_seconds.setTextColorOut(textColorOut); + } + + private void setTextColorCenter() { + wv_day.setTextColorCenter(textColorCenter); + wv_month.setTextColorCenter(textColorCenter); + wv_year.setTextColorCenter(textColorCenter); + wv_hours.setTextColorCenter(textColorCenter); + wv_minutes.setTextColorCenter(textColorCenter); + wv_seconds.setTextColorCenter(textColorCenter); + } + + private void setDividerColor() { + wv_day.setDividerColor(dividerColor); + wv_month.setDividerColor(dividerColor); + wv_year.setDividerColor(dividerColor); + wv_hours.setDividerColor(dividerColor); + wv_minutes.setDividerColor(dividerColor); + wv_seconds.setDividerColor(dividerColor); + } + + private void setDividerWidth() { + wv_day.setDividerWidth(dividerWidth); + wv_month.setDividerWidth(dividerWidth); + wv_year.setDividerWidth(dividerWidth); + wv_hours.setDividerWidth(dividerWidth); + wv_minutes.setDividerWidth(dividerWidth); + wv_seconds.setDividerWidth(dividerWidth); + } + + private void setSelectItemBgColor() { + wv_day.setSelectItemBgColor(selectItemBgColor); + wv_month.setSelectItemBgColor(selectItemBgColor); + wv_year.setSelectItemBgColor(selectItemBgColor); + wv_hours.setSelectItemBgColor(selectItemBgColor); + wv_minutes.setSelectItemBgColor(selectItemBgColor); + wv_seconds.setSelectItemBgColor(selectItemBgColor); + } + + private void setDividerType() { + + wv_day.setDividerType(dividerType); + wv_month.setDividerType(dividerType); + wv_year.setDividerType(dividerType); + wv_hours.setDividerType(dividerType); + wv_minutes.setDividerType(dividerType); + wv_seconds.setDividerType(dividerType); + + } + + private void setLineSpacingMultiplier() { + wv_day.setLineSpacingMultiplier(lineSpacingMultiplier); + wv_month.setLineSpacingMultiplier(lineSpacingMultiplier); + wv_year.setLineSpacingMultiplier(lineSpacingMultiplier); + wv_hours.setLineSpacingMultiplier(lineSpacingMultiplier); + wv_minutes.setLineSpacingMultiplier(lineSpacingMultiplier); + wv_seconds.setLineSpacingMultiplier(lineSpacingMultiplier); + } + public void setLabels(String label_year, String label_month, String label_day, String label_hours, String label_mins, String label_seconds) { if (isLunarCalendar) { return; @@ -622,6 +721,9 @@ public void setLabels(String label_year, String label_month, String label_day, S public void setTextXOffset(int x_offset_year, int x_offset_month, int x_offset_day, int x_offset_hours, int x_offset_minutes, int x_offset_seconds) { + wv_day.setTextXOffset(x_offset_year); + wv_month.setTextXOffset(x_offset_month); + wv_year.setTextXOffset(x_offset_day); wv_year.setTextXOffset(x_offset_year); wv_month.setTextXOffset(x_offset_month); wv_day.setTextXOffset(x_offset_day); @@ -720,6 +822,10 @@ public View getView() { return view; } + public void setView(View view) { + this.view = view; + } + public int getStartYear() { return startYear; } @@ -800,6 +906,8 @@ public void setRangDate(Calendar startDate, Calendar endDate) { * @param lineSpacingMultiplier */ public void setLineSpacingMultiplier(float lineSpacingMultiplier) { + this.lineSpacingMultiplier = lineSpacingMultiplier; + setLineSpacingMultiplier(); wv_day.setLineSpacingMultiplier(lineSpacingMultiplier); wv_month.setLineSpacingMultiplier(lineSpacingMultiplier); wv_year.setLineSpacingMultiplier(lineSpacingMultiplier); @@ -814,6 +922,28 @@ public void setLineSpacingMultiplier(float lineSpacingMultiplier) { * @param dividerColor */ public void setDividerColor(int dividerColor) { + this.dividerColor = dividerColor; + setDividerColor(); + } + + /** + * 设置分割线的宽度 + * + * @param dividerWidth + */ + public void setDividerWidth(int dividerWidth) { + this.dividerWidth = dividerWidth; + setDividerWidth(); + } + + /** + * 设置选中条目的背景色 + * + * @param selectItemBgColor + */ + public void setSelectItemBgColor(int selectItemBgColor) { + this.selectItemBgColor = selectItemBgColor; + setSelectItemBgColor(); wv_day.setDividerColor(dividerColor); wv_month.setDividerColor(dividerColor); wv_year.setDividerColor(dividerColor); @@ -828,6 +958,8 @@ public void setDividerColor(int dividerColor) { * @param dividerType */ public void setDividerType(WheelView.DividerType dividerType) { + this.dividerType = dividerType; + setDividerType(); wv_day.setDividerType(dividerType); wv_month.setDividerType(dividerType); wv_year.setDividerType(dividerType); @@ -842,6 +974,8 @@ public void setDividerType(WheelView.DividerType dividerType) { * @param textColorCenter */ public void setTextColorCenter(int textColorCenter) { + this.textColorCenter = textColorCenter; + setTextColorCenter(); wv_day.setTextColorCenter(textColorCenter); wv_month.setTextColorCenter(textColorCenter); wv_year.setTextColorCenter(textColorCenter); @@ -856,6 +990,8 @@ public void setTextColorCenter(int textColorCenter) { * @param textColorOut */ public void setTextColorOut(int textColorOut) { + this.textColorOut = textColorOut; + setTextColorOut(); wv_day.setTextColorOut(textColorOut); wv_month.setTextColorOut(textColorOut); wv_year.setTextColorOut(textColorOut); @@ -879,6 +1015,7 @@ public void isCenterLabel(boolean isCenterLabel) { public void setSelectChangeCallback(ISelectTimeCallback mSelectChangeCallback) { this.mSelectChangeCallback = mSelectChangeCallback; } +} public void setItemsVisible(int itemsVisibleCount) { wv_day.setItemsVisibleCount(itemsVisibleCount); diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index ea7327c7..6841f735 100644 --- a/wheelview/src/main/java/com/contrarywind/view/WheelView.java +++ b/wheelview/src/main/java/com/contrarywind/view/WheelView.java @@ -61,6 +61,7 @@ public enum DividerType { // 分隔线类型 private Paint paintOuterText; private Paint paintCenterText; private Paint paintIndicator; + private Paint paintSelectItem; private WheelAdapter adapter; @@ -77,6 +78,7 @@ public enum DividerType { // 分隔线类型 private int textColorCenter; private int dividerColor; private int dividerWidth; + private int selectItemBgColor; // 条目间距倍数 private float lineSpacingMultiplier = 1.6F; @@ -132,7 +134,7 @@ public WheelView(Context context, AttributeSet attrs) { super(context, attrs); textSize = getResources().getDimensionPixelSize(R.dimen.pickerview_textsize);//默认大小 - + dividerWidth = 1;//默认线宽 DisplayMetrics dm = getResources().getDisplayMetrics(); float density = dm.density; // 屏幕密度比(0.75/1.0/1.5/2.0/3.0) @@ -152,6 +154,9 @@ public WheelView(Context context, AttributeSet attrs) { textColorOut = a.getColor(R.styleable.pickerview_wheelview_textColorOut, 0xFFa8a8a8); textColorCenter = a.getColor(R.styleable.pickerview_wheelview_textColorCenter, 0xFF2a2a2a); dividerColor = a.getColor(R.styleable.pickerview_wheelview_dividerColor, 0xFFd5d5d5); + dividerWidth = a.getInt(R.styleable.pickerview_wheelview_dividerWidth, dividerWidth); + selectItemBgColor = a.getColor(R.styleable.pickerview_wheelview_selectItemBgColor, + 0xFFe7e7e7); 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); @@ -202,6 +207,11 @@ private void initPaints() { paintIndicator = new Paint(); paintIndicator.setColor(dividerColor); paintIndicator.setAntiAlias(true); + paintIndicator.setStrokeWidth(dividerWidth); + + paintSelectItem = new Paint(); + paintSelectItem.setColor(selectItemBgColor); + paintSelectItem.setAntiAlias(true); setLayerType(LAYER_TYPE_SOFTWARE, null); } @@ -438,7 +448,7 @@ protected void onDraw(Canvas canvas) { canvas.drawLine(0.0F, firstLineY, measuredWidth, firstLineY, paintIndicator); canvas.drawLine(0.0F, secondLineY, measuredWidth, secondLineY, paintIndicator); } - + canvas.drawRect(0.0f, firstLineY, measuredWidth, secondLineY, paintSelectItem); //只显示选中项Label文字的模式,并且Label文字不为空,则进行绘制 if (!TextUtils.isEmpty(label) && isCenterLabel) { //绘制文字,靠右并留出空隙 @@ -802,6 +812,16 @@ public void setDividerColor(int dividerColor) { paintIndicator.setColor(dividerColor); } + public void setDividerWidth(int dividerWidth) { + this.dividerWidth = dividerWidth; + paintIndicator.setStrokeWidth(dividerWidth); + } + + public void setSelectItemBgColor(int selectItemBgColor) { + this.selectItemBgColor = selectItemBgColor; + paintSelectItem.setColor(selectItemBgColor); + } + public void setDividerType(DividerType dividerType) { this.dividerType = dividerType; } diff --git a/wheelview/src/main/res/values/attrs.xml b/wheelview/src/main/res/values/attrs.xml index e7cae4d3..b53b97e3 100644 --- a/wheelview/src/main/res/values/attrs.xml +++ b/wheelview/src/main/res/values/attrs.xml @@ -10,6 +10,8 @@ + +