From 9048f77b1be0b223b0288c68c54a362bc5cfbe26 Mon Sep 17 00:00:00 2001 From: feng1991G <981479805@qq.com> Date: Fri, 26 Apr 2019 17:45:11 +0800 Subject: [PATCH] =?UTF-8?q?*=E6=96=B0=E5=A2=9E=E9=80=89=E4=B8=AD=E9=A1=B9?= =?UTF-8?q?=E7=9A=84=E8=83=8C=E6=99=AF=E8=89=B2=E5=8F=8A=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=BA=BF=E7=9A=84=E5=AE=BD=E5=BA=A6=20=EF=BC=9A=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=96=B9=E6=B3=95=EF=BC=8CmainActivity=E4=B8=AD=20.se?= =?UTF-8?q?tDividerWidth(5)//=E8=AE=BE=E7=BD=AE=E9=80=89=E4=B8=AD=E6=9D=A1?= =?UTF-8?q?=E7=9B=AE=E5=88=86=E5=89=B2=E7=BA=BF=E7=9A=84=E7=BA=BF=E5=AE=BD?= =?UTF-8?q?=EF=BC=8C=E5=8D=95=E4=BD=8Dpx=EF=BC=8C=E8=8B=A5=E4=B8=8D?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=BB=98=E8=AE=A4=E4=B8=BA1=20=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20=20=20=20=20=20=20=20=20=20.setSelectItemBgColor?= =?UTF-8?q?(Color.GREEN)//=E8=AE=BE=E7=BD=AE=E9=80=89=E4=B8=AD=E6=9D=A1?= =?UTF-8?q?=E7=9B=AE=E7=9A=84=E8=83=8C=E6=99=AF=E8=89=B2=EF=BC=8C=E8=8B=A5?= =?UTF-8?q?=E4=B8=8D=E8=AE=BE=E7=BD=AE=E9=BB=98=E8=AE=A4=E4=B8=BA0xFFe7e7e?= =?UTF-8?q?7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bigkoo/pickerviewdemo/MainActivity.java | 4 + .../pickerview/builder/TimePickerBuilder.java | 20 + .../pickerview/configure/PickerOptions.java | 2 + .../pickerview/view/TimePickerView.java | 2 + .../com/bigkoo/pickerview/view/WheelTime.java | 1904 +++++++++-------- .../java/com/contrarywind/view/WheelView.java | 25 +- wheelview/src/main/res/values/attrs.xml | 2 + 7 files changed, 1025 insertions(+), 934 deletions(-) diff --git a/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java b/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java index a75304bb..68f676dc 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; @@ -206,6 +207,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 .build(); Dialog mDialog = pvTime.getDialog(); 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 a31bda81..023cdedd 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java @@ -168,6 +168,26 @@ public TimePickerBuilder setDividerColor(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 74965b9b..d085a62a 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java @@ -101,6 +101,8 @@ 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 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 15c9d098..739c6b88 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java @@ -138,6 +138,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 5941f934..62356a49 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java @@ -1,932 +1,972 @@ -package com.bigkoo.pickerview.view; - -import android.view.View; - -import com.bigkoo.pickerview.R; -import com.bigkoo.pickerview.adapter.ArrayWheelAdapter; -import com.bigkoo.pickerview.adapter.NumericWheelAdapter; -import com.bigkoo.pickerview.listener.ISelectTimeCallback; -import com.bigkoo.pickerview.utils.ChinaDate; -import com.bigkoo.pickerview.utils.LunarCalendar; -import com.contrarywind.listener.OnItemSelectedListener; -import com.contrarywind.view.WheelView; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Calendar; -import java.util.List; - - -public class WheelTime { - public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - private View view; - private WheelView wv_year; - private WheelView wv_month; - private WheelView wv_day; - private WheelView wv_hours; - private WheelView wv_minutes; - private WheelView wv_seconds; - private int gravity; - - private boolean[] type; - private static final int DEFAULT_START_YEAR = 1900; - private static final int DEFAULT_END_YEAR = 2100; - private static final int DEFAULT_START_MONTH = 1; - private static final int DEFAULT_END_MONTH = 12; - private static final int DEFAULT_START_DAY = 1; - private static final int DEFAULT_END_DAY = 31; - - private int startYear = DEFAULT_START_YEAR; - private int endYear = DEFAULT_END_YEAR; - private int startMonth = DEFAULT_START_MONTH; - private int endMonth = DEFAULT_END_MONTH; - private int startDay = DEFAULT_START_DAY; - private int endDay = DEFAULT_END_DAY; //表示31天的 - private int currentYear; - - private int textSize; - - //文字的颜色和分割线的颜色 - private int textColorOut; - private int textColorCenter; - private int dividerColor; - - private float lineSpacingMultiplier; - private WheelView.DividerType dividerType; - private boolean isLunarCalendar = false; - private ISelectTimeCallback mSelectChangeCallback; - - public WheelTime(View view, boolean[] type, int gravity, int textSize) { - super(); - this.view = view; - this.type = type; - this.gravity = gravity; - this.textSize = textSize; - setView(view); - } - - - public void setLunarMode(boolean isLunarCalendar) { - this.isLunarCalendar = isLunarCalendar; - } - - public boolean isLunarMode() { - return isLunarCalendar; - } - - public void setPicker(int year, int month, int day) { - this.setPicker(year, month, day, 0, 0, 0); - } - - public void setPicker(int year, final int month, int day, int h, int m, int s) { - if (isLunarCalendar) { - int[] lunar = LunarCalendar.solarToLunar(year, month + 1, day); - setLunar(lunar[0], lunar[1] - 1, lunar[2], lunar[3] == 1, h, m, s); - } else { - setSolar(year, month, day, h, m, s); - } - } - - /** - * 设置农历 - * - * @param year - * @param month - * @param day - * @param h - * @param m - * @param s - */ - private void setLunar(int year, final int month, int day, boolean isLeap, int h, int m, int s) { - // 年 - wv_year = (WheelView) view.findViewById(R.id.year); - wv_year.setAdapter(new ArrayWheelAdapter(ChinaDate.getYears(startYear, endYear)));// 设置"年"的显示数据 - wv_year.setLabel("");// 添加文字 - wv_year.setCurrentItem(year - startYear);// 初始化时显示的数据 - wv_year.setGravity(gravity); - - // 月 - 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); - - // 日 - wv_day = (WheelView) view.findViewById(R.id.day); - // 判断大小月及是否闰年,用来确定"日"的数据 - if (ChinaDate.leapMonth(year) == 0) { - wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year, month)))); - } else { - wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.leapDays(year)))); - } - wv_day.setLabel(""); - wv_day.setCurrentItem(day - 1); - wv_day.setGravity(gravity); - - wv_hours = (WheelView) view.findViewById(R.id.hour); - wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); - //wv_hours.setLabel(context.getString(R.string.pickerview_hours));// 添加文字 - wv_hours.setCurrentItem(h); - wv_hours.setGravity(gravity); - - wv_minutes = (WheelView) view.findViewById(R.id.min); - wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); - //wv_minutes.setLabel(context.getString(R.string.pickerview_minutes));// 添加文字 - wv_minutes.setCurrentItem(m); - wv_minutes.setGravity(gravity); - - wv_seconds = (WheelView) view.findViewById(R.id.second); - wv_seconds.setAdapter(new NumericWheelAdapter(0, 59)); - //wv_seconds.setLabel(context.getString(R.string.pickerview_minutes));// 添加文字 - wv_seconds.setCurrentItem(m); - wv_seconds.setGravity(gravity); - - // 添加"年"监听 - wv_year.setOnItemSelectedListener(new OnItemSelectedListener() { - @Override - public void onItemSelected(int index) { - int year_num = index + startYear; - // 判断是不是闰年,来确定月和日的选择 - wv_month.setAdapter(new ArrayWheelAdapter(ChinaDate.getMonths(year_num))); - if (ChinaDate.leapMonth(year_num) != 0 && wv_month.getCurrentItem() > ChinaDate.leapMonth(year_num) - 1) { - wv_month.setCurrentItem(wv_month.getCurrentItem() + 1); - } else { - wv_month.setCurrentItem(wv_month.getCurrentItem()); - } - - int maxItem = 29; - if (ChinaDate.leapMonth(year_num) != 0 && wv_month.getCurrentItem() > ChinaDate.leapMonth(year_num) - 1) { - if (wv_month.getCurrentItem() == ChinaDate.leapMonth(year_num) + 1) { - wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.leapDays(year_num)))); - maxItem = ChinaDate.leapDays(year_num); - } else { - wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year_num, wv_month.getCurrentItem())))); - maxItem = ChinaDate.monthDays(year_num, wv_month.getCurrentItem()); - } - } else { - wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year_num, wv_month.getCurrentItem() + 1)))); - maxItem = ChinaDate.monthDays(year_num, wv_month.getCurrentItem() + 1); - } - - if (wv_day.getCurrentItem() > maxItem - 1) { - wv_day.setCurrentItem(maxItem - 1); - } - - if (mSelectChangeCallback != null) { - mSelectChangeCallback.onTimeSelectChanged(); - } - } - }); - - // 添加"月"监听 - wv_month.setOnItemSelectedListener(new OnItemSelectedListener() { - @Override - public void onItemSelected(int index) { - int month_num = index; - int year_num = wv_year.getCurrentItem() + startYear; - int maxItem = 29; - if (ChinaDate.leapMonth(year_num) != 0 && month_num > ChinaDate.leapMonth(year_num) - 1) { - if (wv_month.getCurrentItem() == ChinaDate.leapMonth(year_num) + 1) { - wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.leapDays(year_num)))); - maxItem = ChinaDate.leapDays(year_num); - } else { - wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year_num, month_num)))); - maxItem = ChinaDate.monthDays(year_num, month_num); - } - } else { - wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year_num, month_num + 1)))); - maxItem = ChinaDate.monthDays(year_num, month_num + 1); - } - - if (wv_day.getCurrentItem() > maxItem - 1) { - wv_day.setCurrentItem(maxItem - 1); - } - - if (mSelectChangeCallback != null) { - mSelectChangeCallback.onTimeSelectChanged(); - } - } - }); - - setChangedListener(wv_day); - setChangedListener(wv_hours); - setChangedListener(wv_minutes); - setChangedListener(wv_seconds); - - if (type.length != 6) { - throw new RuntimeException("type[] length is not 6"); - } - wv_year.setVisibility(type[0] ? View.VISIBLE : View.GONE); - wv_month.setVisibility(type[1] ? View.VISIBLE : View.GONE); - wv_day.setVisibility(type[2] ? View.VISIBLE : View.GONE); - wv_hours.setVisibility(type[3] ? View.VISIBLE : View.GONE); - wv_minutes.setVisibility(type[4] ? View.VISIBLE : View.GONE); - wv_seconds.setVisibility(type[5] ? View.VISIBLE : View.GONE); - setContentTextSize(); - } - - /** - * 设置公历 - * - * @param year - * @param month - * @param day - * @param h - * @param m - * @param s - */ - private void setSolar(int year, final int month, int day, int h, int m, int s) { - // 添加大小月月份并将其转换为list,方便之后的判断 - String[] months_big = {"1", "3", "5", "7", "8", "10", "12"}; - String[] months_little = {"4", "6", "9", "11"}; - - final List list_big = Arrays.asList(months_big); - final List list_little = Arrays.asList(months_little); - - currentYear = year; - // 年 - wv_year = (WheelView) view.findViewById(R.id.year); - wv_year.setAdapter(new NumericWheelAdapter(startYear, endYear));// 设置"年"的显示数据 - - - wv_year.setCurrentItem(year - startYear);// 初始化时显示的数据 - wv_year.setGravity(gravity); - // 月 - wv_month = (WheelView) view.findViewById(R.id.month); - if (startYear == endYear) {//开始年等于终止年 - wv_month.setAdapter(new NumericWheelAdapter(startMonth, endMonth)); - wv_month.setCurrentItem(month + 1 - startMonth); - } else if (year == startYear) { - //起始日期的月份控制 - wv_month.setAdapter(new NumericWheelAdapter(startMonth, 12)); - wv_month.setCurrentItem(month + 1 - startMonth); - } else if (year == endYear) { - //终止日期的月份控制 - wv_month.setAdapter(new NumericWheelAdapter(1, endMonth)); - wv_month.setCurrentItem(month); - } else { - wv_month.setAdapter(new NumericWheelAdapter(1, 12)); - wv_month.setCurrentItem(month); - } - wv_month.setGravity(gravity); - // 日 - wv_day = (WheelView) view.findViewById(R.id.day); - - if (startYear == endYear && startMonth == endMonth) { - if (list_big.contains(String.valueOf(month + 1))) { - if (endDay > 31) { - endDay = 31; - } - wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay)); - } else if (list_little.contains(String.valueOf(month + 1))) { - if (endDay > 30) { - endDay = 30; - } - wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay)); - } else { - // 闰年 - if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { - if (endDay > 29) { - endDay = 29; - } - wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay)); - } else { - if (endDay > 28) { - endDay = 28; - } - wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay)); - } - } - wv_day.setCurrentItem(day - startDay); - } else if (year == startYear && month + 1 == startMonth) { - // 起始日期的天数控制 - if (list_big.contains(String.valueOf(month + 1))) { - - wv_day.setAdapter(new NumericWheelAdapter(startDay, 31)); - } else if (list_little.contains(String.valueOf(month + 1))) { - - 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)); - } - } - wv_day.setCurrentItem(day - startDay); - } else if (year == endYear && month + 1 == endMonth) { - // 终止日期的天数控制 - if (list_big.contains(String.valueOf(month + 1))) { - if (endDay > 31) { - endDay = 31; - } - wv_day.setAdapter(new NumericWheelAdapter(1, endDay)); - } else if (list_little.contains(String.valueOf(month + 1))) { - if (endDay > 30) { - endDay = 30; - } - wv_day.setAdapter(new NumericWheelAdapter(1, endDay)); - } else { - // 闰年 - if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { - if (endDay > 29) { - endDay = 29; - } - wv_day.setAdapter(new NumericWheelAdapter(1, endDay)); - } else { - if (endDay > 28) { - endDay = 28; - } - wv_day.setAdapter(new NumericWheelAdapter(1, endDay)); - } - } - wv_day.setCurrentItem(day - 1); - } 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.setCurrentItem(day - 1); - } - - wv_day.setGravity(gravity); - //时 - wv_hours = (WheelView) view.findViewById(R.id.hour); - wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); - - wv_hours.setCurrentItem(h); - wv_hours.setGravity(gravity); - //分 - wv_minutes = (WheelView) view.findViewById(R.id.min); - wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); - - wv_minutes.setCurrentItem(m); - wv_minutes.setGravity(gravity); - //秒 - wv_seconds = (WheelView) view.findViewById(R.id.second); - wv_seconds.setAdapter(new NumericWheelAdapter(0, 59)); - - wv_seconds.setCurrentItem(s); - wv_seconds.setGravity(gravity); - - // 添加"年"监听 - wv_year.setOnItemSelectedListener(new OnItemSelectedListener() { - @Override - public void onItemSelected(int index) { - int year_num = index + startYear; - currentYear = year_num; - int currentMonthItem = wv_month.getCurrentItem();//记录上一次的item位置 - // 判断大小月及是否闰年,用来确定"日"的数据 - if (startYear == endYear) { - //重新设置月份 - wv_month.setAdapter(new NumericWheelAdapter(startMonth, endMonth)); - - if (currentMonthItem > wv_month.getAdapter().getItemsCount() - 1) { - currentMonthItem = wv_month.getAdapter().getItemsCount() - 1; - wv_month.setCurrentItem(currentMonthItem); - } - - int monthNum = currentMonthItem + startMonth; - - if (startMonth == endMonth) { - //重新设置日 - setReDay(year_num, monthNum, startDay, endDay, list_big, list_little); - } else if (monthNum == startMonth) { - //重新设置日 - setReDay(year_num, monthNum, startDay, 31, list_big, list_little); - } else if (monthNum == endMonth) { - setReDay(year_num, monthNum, 1, endDay, list_big, list_little); - } else {//重新设置日 - setReDay(year_num, monthNum, 1, 31, list_big, list_little); - } - } else if (year_num == startYear) {//等于开始的年 - //重新设置月份 - 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) { - //重新设置日 - setReDay(year_num, month, startDay, 31, list_big, list_little); - } else { - //重新设置日 - setReDay(year_num, month, 1, 31, list_big, list_little); - } - - } else if (year_num == endYear) { - //重新设置月份 - 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) { - //重新设置日 - setReDay(year_num, monthNum, 1, endDay, list_big, list_little); - } else { - //重新设置日 - setReDay(year_num, monthNum, 1, 31, list_big, list_little); - } - - } else { - //重新设置月份 - wv_month.setAdapter(new NumericWheelAdapter(1, 12)); - //重新设置日 - setReDay(year_num, wv_month.getCurrentItem() + 1, 1, 31, list_big, list_little); - } - - if (mSelectChangeCallback != null) { - mSelectChangeCallback.onTimeSelectChanged(); - } - } - }); - - - // 添加"月"监听 - wv_month.setOnItemSelectedListener(new OnItemSelectedListener() { - @Override - public void onItemSelected(int index) { - int month_num = index + 1; - - if (startYear == endYear) { - month_num = month_num + startMonth - 1; - if (startMonth == endMonth) { - //重新设置日 - setReDay(currentYear, month_num, startDay, endDay, list_big, list_little); - } else if (startMonth == month_num) { - - //重新设置日 - setReDay(currentYear, month_num, startDay, 31, list_big, list_little); - } else if (endMonth == month_num) { - setReDay(currentYear, month_num, 1, endDay, list_big, list_little); - } else { - setReDay(currentYear, month_num, 1, 31, list_big, list_little); - } - } else if (currentYear == startYear) { - month_num = month_num + startMonth - 1; - if (month_num == startMonth) { - //重新设置日 - setReDay(currentYear, month_num, startDay, 31, list_big, list_little); - } else { - //重新设置日 - setReDay(currentYear, month_num, 1, 31, list_big, list_little); - } - - } else if (currentYear == endYear) { - if (month_num == endMonth) { - //重新设置日 - 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); - } - - } else { - //重新设置日 - setReDay(currentYear, month_num, 1, 31, list_big, list_little); - } - - if (mSelectChangeCallback != null) { - mSelectChangeCallback.onTimeSelectChanged(); - } - } - }); - - setChangedListener(wv_day); - setChangedListener(wv_hours); - setChangedListener(wv_minutes); - setChangedListener(wv_seconds); - - if (type.length != 6) { - throw new IllegalArgumentException("type[] length is not 6"); - } - wv_year.setVisibility(type[0] ? View.VISIBLE : View.GONE); - wv_month.setVisibility(type[1] ? View.VISIBLE : View.GONE); - wv_day.setVisibility(type[2] ? View.VISIBLE : View.GONE); - wv_hours.setVisibility(type[3] ? View.VISIBLE : View.GONE); - wv_minutes.setVisibility(type[4] ? View.VISIBLE : View.GONE); - wv_seconds.setVisibility(type[5] ? View.VISIBLE : View.GONE); - setContentTextSize(); - } - - private void setChangedListener(WheelView wheelView) { - if (mSelectChangeCallback != null) { - wheelView.setOnItemSelectedListener(new OnItemSelectedListener() { - @Override - public void onItemSelected(int index) { - mSelectChangeCallback.onTimeSelectChanged(); - } - }); - } - - } - - - private void setReDay(int year_num, int monthNum, int startD, int endD, List list_big, List list_little) { - int currentItem = wv_day.getCurrentItem(); - -// int maxItem; - if (list_big.contains(String.valueOf(monthNum))) { - if (endD > 31) { - endD = 31; - } - wv_day.setAdapter(new NumericWheelAdapter(startD, endD)); -// maxItem = endD; - } else if (list_little.contains(String.valueOf(monthNum))) { - if (endD > 30) { - endD = 30; - } - wv_day.setAdapter(new NumericWheelAdapter(startD, endD)); -// maxItem = endD; - } else { - if ((year_num % 4 == 0 && year_num % 100 != 0) - || year_num % 400 == 0) { - if (endD > 29) { - endD = 29; - } - wv_day.setAdapter(new NumericWheelAdapter(startD, endD)); -// maxItem = endD; - } else { - if (endD > 28) { - endD = 28; - } - wv_day.setAdapter(new NumericWheelAdapter(startD, endD)); -// maxItem = endD; - } - } - - if (currentItem > wv_day.getAdapter().getItemsCount() - 1) { - currentItem = wv_day.getAdapter().getItemsCount() - 1; - wv_day.setCurrentItem(currentItem); - } - } - - - private void setContentTextSize() { - wv_day.setTextSize(textSize); - wv_month.setTextSize(textSize); - wv_year.setTextSize(textSize); - wv_hours.setTextSize(textSize); - wv_minutes.setTextSize(textSize); - wv_seconds.setTextSize(textSize); - } - - 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 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; - } - - if (label_year != null) { - wv_year.setLabel(label_year); - } else { - wv_year.setLabel(view.getContext().getString(R.string.pickerview_year)); - } - if (label_month != null) { - wv_month.setLabel(label_month); - } else { - wv_month.setLabel(view.getContext().getString(R.string.pickerview_month)); - } - if (label_day != null) { - wv_day.setLabel(label_day); - } else { - wv_day.setLabel(view.getContext().getString(R.string.pickerview_day)); - } - if (label_hours != null) { - wv_hours.setLabel(label_hours); - } else { - wv_hours.setLabel(view.getContext().getString(R.string.pickerview_hours)); - } - if (label_mins != null) { - wv_minutes.setLabel(label_mins); - } else { - wv_minutes.setLabel(view.getContext().getString(R.string.pickerview_minutes)); - } - if (label_seconds != null) { - wv_seconds.setLabel(label_seconds); - } else { - wv_seconds.setLabel(view.getContext().getString(R.string.pickerview_seconds)); - } - - } - - 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_hours.setTextXOffset(x_offset_hours); - wv_minutes.setTextXOffset(x_offset_minutes); - wv_seconds.setTextXOffset(x_offset_seconds); - } - - /** - * 设置是否循环滚动 - * - * @param cyclic - */ - public void setCyclic(boolean cyclic) { - wv_year.setCyclic(cyclic); - wv_month.setCyclic(cyclic); - wv_day.setCyclic(cyclic); - wv_hours.setCyclic(cyclic); - wv_minutes.setCyclic(cyclic); - wv_seconds.setCyclic(cyclic); - } - - public String getTime() { - if (isLunarCalendar) { - //如果是农历 返回对应的公历时间 - return getLunarTime(); - } - StringBuilder sb = new StringBuilder(); - if (currentYear == startYear) { - /* int i = wv_month.getCurrentItem() + startMonth; - System.out.println("i:" + i);*/ - if ((wv_month.getCurrentItem() + startMonth) == startMonth) { - sb.append((wv_year.getCurrentItem() + startYear)).append("-") - .append((wv_month.getCurrentItem() + startMonth)).append("-") - .append((wv_day.getCurrentItem() + startDay)).append(" ") - .append(wv_hours.getCurrentItem()).append(":") - .append(wv_minutes.getCurrentItem()).append(":") - .append(wv_seconds.getCurrentItem()); - } else { - sb.append((wv_year.getCurrentItem() + startYear)).append("-") - .append((wv_month.getCurrentItem() + startMonth)).append("-") - .append((wv_day.getCurrentItem() + 1)).append(" ") - .append(wv_hours.getCurrentItem()).append(":") - .append(wv_minutes.getCurrentItem()).append(":") - .append(wv_seconds.getCurrentItem()); - } - - } else { - sb.append((wv_year.getCurrentItem() + startYear)).append("-") - .append((wv_month.getCurrentItem() + 1)).append("-") - .append((wv_day.getCurrentItem() + 1)).append(" ") - .append(wv_hours.getCurrentItem()).append(":") - .append(wv_minutes.getCurrentItem()).append(":") - .append(wv_seconds.getCurrentItem()); - } - - return sb.toString(); - } - - - /** - * 农历返回对应的公历时间 - * - * @return - */ - private String getLunarTime() { - StringBuilder sb = new StringBuilder(); - int year = wv_year.getCurrentItem() + startYear; - int month = 1; - boolean isLeapMonth = false; - if (ChinaDate.leapMonth(year) == 0) { - month = wv_month.getCurrentItem() + 1; - } else { - if ((wv_month.getCurrentItem() + 1) - ChinaDate.leapMonth(year) <= 0) { - month = wv_month.getCurrentItem() + 1; - } else if ((wv_month.getCurrentItem() + 1) - ChinaDate.leapMonth(year) == 1) { - month = wv_month.getCurrentItem(); - isLeapMonth = true; - } else { - month = wv_month.getCurrentItem(); - } - } - int day = wv_day.getCurrentItem() + 1; - int[] solar = LunarCalendar.lunarToSolar(year, month, day, isLeapMonth); - - sb.append(solar[0]).append("-") - .append(solar[1]).append("-") - .append(solar[2]).append(" ") - .append(wv_hours.getCurrentItem()).append(":") - .append(wv_minutes.getCurrentItem()).append(":") - .append(wv_seconds.getCurrentItem()); - return sb.toString(); - } - - public View getView() { - return view; - } - - public void setView(View view) { - this.view = view; - } - - public int getStartYear() { - return startYear; - } - - public void setStartYear(int startYear) { - this.startYear = startYear; - } - - public int getEndYear() { - return endYear; - } - - public void setEndYear(int endYear) { - this.endYear = endYear; - } - - - public void setRangDate(Calendar startDate, Calendar endDate) { - - if (startDate == null && endDate != null) { - int year = endDate.get(Calendar.YEAR); - int month = endDate.get(Calendar.MONTH) + 1; - int day = endDate.get(Calendar.DAY_OF_MONTH); - if (year > startYear) { - this.endYear = year; - this.endMonth = month; - this.endDay = day; - } else if (year == startYear) { - if (month > startMonth) { - this.endYear = year; - this.endMonth = month; - this.endDay = day; - } else if (month == startMonth) { - if (day > startDay) { - this.endYear = year; - this.endMonth = month; - this.endDay = day; - } - } - } - - } else if (startDate != null && endDate == null) { - int year = startDate.get(Calendar.YEAR); - int month = startDate.get(Calendar.MONTH) + 1; - int day = startDate.get(Calendar.DAY_OF_MONTH); - if (year < endYear) { - this.startMonth = month; - this.startDay = day; - this.startYear = year; - } else if (year == endYear) { - if (month < endMonth) { - this.startMonth = month; - this.startDay = day; - this.startYear = year; - } else if (month == endMonth) { - if (day < endDay) { - this.startMonth = month; - this.startDay = day; - this.startYear = year; - } - } - } - - } 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); - } - - } - - /** - * 设置间距倍数,但是只能在1.0-4.0f之间 - * - * @param lineSpacingMultiplier - */ - public void setLineSpacingMultiplier(float lineSpacingMultiplier) { - this.lineSpacingMultiplier = lineSpacingMultiplier; - setLineSpacingMultiplier(); - } - - /** - * 设置分割线的颜色 - * - * @param dividerColor - */ - public void setDividerColor(int dividerColor) { - this.dividerColor = dividerColor; - setDividerColor(); - } - - /** - * 设置分割线的类型 - * - * @param dividerType - */ - public void setDividerType(WheelView.DividerType dividerType) { - this.dividerType = dividerType; - setDividerType(); - } - - /** - * 设置分割线之间的文字的颜色 - * - * @param textColorCenter - */ - public void setTextColorCenter(int textColorCenter) { - this.textColorCenter = textColorCenter; - setTextColorCenter(); - } - - /** - * 设置分割线以外文字的颜色 - * - * @param textColorOut - */ - public void setTextColorOut(int textColorOut) { - this.textColorOut = textColorOut; - setTextColorOut(); - } - - /** - * @param isCenterLabel 是否只显示中间选中项的 - */ - public void isCenterLabel(boolean isCenterLabel) { - wv_day.isCenterLabel(isCenterLabel); - wv_month.isCenterLabel(isCenterLabel); - wv_year.isCenterLabel(isCenterLabel); - wv_hours.isCenterLabel(isCenterLabel); - wv_minutes.isCenterLabel(isCenterLabel); - wv_seconds.isCenterLabel(isCenterLabel); - } - - public void setSelectChangeCallback(ISelectTimeCallback mSelectChangeCallback) { - this.mSelectChangeCallback = mSelectChangeCallback; - } -} +package com.bigkoo.pickerview.view; + +import android.view.View; + +import com.bigkoo.pickerview.R; +import com.bigkoo.pickerview.adapter.ArrayWheelAdapter; +import com.bigkoo.pickerview.adapter.NumericWheelAdapter; +import com.bigkoo.pickerview.listener.ISelectTimeCallback; +import com.bigkoo.pickerview.utils.ChinaDate; +import com.bigkoo.pickerview.utils.LunarCalendar; +import com.contrarywind.listener.OnItemSelectedListener; +import com.contrarywind.view.WheelView; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Calendar; +import java.util.List; + + +public class WheelTime { + public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private View view; + private WheelView wv_year; + private WheelView wv_month; + private WheelView wv_day; + private WheelView wv_hours; + private WheelView wv_minutes; + private WheelView wv_seconds; + private int gravity; + + private boolean[] type; + private static final int DEFAULT_START_YEAR = 1900; + private static final int DEFAULT_END_YEAR = 2100; + private static final int DEFAULT_START_MONTH = 1; + private static final int DEFAULT_END_MONTH = 12; + private static final int DEFAULT_START_DAY = 1; + private static final int DEFAULT_END_DAY = 31; + + private int startYear = DEFAULT_START_YEAR; + private int endYear = DEFAULT_END_YEAR; + private int startMonth = DEFAULT_START_MONTH; + private int endMonth = DEFAULT_END_MONTH; + private int startDay = DEFAULT_START_DAY; + private int endDay = DEFAULT_END_DAY; //表示31天的 + private int currentYear; + + 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; + + public WheelTime(View view, boolean[] type, int gravity, int textSize) { + super(); + this.view = view; + this.type = type; + this.gravity = gravity; + this.textSize = textSize; + setView(view); + } + + + public void setLunarMode(boolean isLunarCalendar) { + this.isLunarCalendar = isLunarCalendar; + } + + public boolean isLunarMode() { + return isLunarCalendar; + } + + public void setPicker(int year, int month, int day) { + this.setPicker(year, month, day, 0, 0, 0); + } + + public void setPicker(int year, final int month, int day, int h, int m, int s) { + if (isLunarCalendar) { + int[] lunar = LunarCalendar.solarToLunar(year, month + 1, day); + setLunar(lunar[0], lunar[1] - 1, lunar[2], lunar[3] == 1, h, m, s); + } else { + setSolar(year, month, day, h, m, s); + } + } + + /** + * 设置农历 + * + * @param year + * @param month + * @param day + * @param h + * @param m + * @param s + */ + private void setLunar(int year, final int month, int day, boolean isLeap, int h, int m, int s) { + // 年 + wv_year = (WheelView) view.findViewById(R.id.year); + wv_year.setAdapter(new ArrayWheelAdapter(ChinaDate.getYears(startYear, endYear)));// 设置"年"的显示数据 + wv_year.setLabel("");// 添加文字 + wv_year.setCurrentItem(year - startYear);// 初始化时显示的数据 + wv_year.setGravity(gravity); + + // 月 + 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); + + // 日 + wv_day = (WheelView) view.findViewById(R.id.day); + // 判断大小月及是否闰年,用来确定"日"的数据 + if (ChinaDate.leapMonth(year) == 0) { + wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year, month)))); + } else { + wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.leapDays(year)))); + } + wv_day.setLabel(""); + wv_day.setCurrentItem(day - 1); + wv_day.setGravity(gravity); + + wv_hours = (WheelView) view.findViewById(R.id.hour); + wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); + //wv_hours.setLabel(context.getString(R.string.pickerview_hours));// 添加文字 + wv_hours.setCurrentItem(h); + wv_hours.setGravity(gravity); + + wv_minutes = (WheelView) view.findViewById(R.id.min); + wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); + //wv_minutes.setLabel(context.getString(R.string.pickerview_minutes));// 添加文字 + wv_minutes.setCurrentItem(m); + wv_minutes.setGravity(gravity); + + wv_seconds = (WheelView) view.findViewById(R.id.second); + wv_seconds.setAdapter(new NumericWheelAdapter(0, 59)); + //wv_seconds.setLabel(context.getString(R.string.pickerview_minutes));// 添加文字 + wv_seconds.setCurrentItem(m); + wv_seconds.setGravity(gravity); + + // 添加"年"监听 + wv_year.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(int index) { + int year_num = index + startYear; + // 判断是不是闰年,来确定月和日的选择 + wv_month.setAdapter(new ArrayWheelAdapter(ChinaDate.getMonths(year_num))); + if (ChinaDate.leapMonth(year_num) != 0 && wv_month.getCurrentItem() > ChinaDate.leapMonth(year_num) - 1) { + wv_month.setCurrentItem(wv_month.getCurrentItem() + 1); + } else { + wv_month.setCurrentItem(wv_month.getCurrentItem()); + } + + int maxItem = 29; + if (ChinaDate.leapMonth(year_num) != 0 && wv_month.getCurrentItem() > ChinaDate.leapMonth(year_num) - 1) { + if (wv_month.getCurrentItem() == ChinaDate.leapMonth(year_num) + 1) { + wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.leapDays(year_num)))); + maxItem = ChinaDate.leapDays(year_num); + } else { + wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year_num, wv_month.getCurrentItem())))); + maxItem = ChinaDate.monthDays(year_num, wv_month.getCurrentItem()); + } + } else { + wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year_num, wv_month.getCurrentItem() + 1)))); + maxItem = ChinaDate.monthDays(year_num, wv_month.getCurrentItem() + 1); + } + + if (wv_day.getCurrentItem() > maxItem - 1) { + wv_day.setCurrentItem(maxItem - 1); + } + + if (mSelectChangeCallback != null) { + mSelectChangeCallback.onTimeSelectChanged(); + } + } + }); + + // 添加"月"监听 + wv_month.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(int index) { + int month_num = index; + int year_num = wv_year.getCurrentItem() + startYear; + int maxItem = 29; + if (ChinaDate.leapMonth(year_num) != 0 && month_num > ChinaDate.leapMonth(year_num) - 1) { + if (wv_month.getCurrentItem() == ChinaDate.leapMonth(year_num) + 1) { + wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.leapDays(year_num)))); + maxItem = ChinaDate.leapDays(year_num); + } else { + wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year_num, month_num)))); + maxItem = ChinaDate.monthDays(year_num, month_num); + } + } else { + wv_day.setAdapter(new ArrayWheelAdapter(ChinaDate.getLunarDays(ChinaDate.monthDays(year_num, month_num + 1)))); + maxItem = ChinaDate.monthDays(year_num, month_num + 1); + } + + if (wv_day.getCurrentItem() > maxItem - 1) { + wv_day.setCurrentItem(maxItem - 1); + } + + if (mSelectChangeCallback != null) { + mSelectChangeCallback.onTimeSelectChanged(); + } + } + }); + + setChangedListener(wv_day); + setChangedListener(wv_hours); + setChangedListener(wv_minutes); + setChangedListener(wv_seconds); + + if (type.length != 6) { + throw new RuntimeException("type[] length is not 6"); + } + wv_year.setVisibility(type[0] ? View.VISIBLE : View.GONE); + wv_month.setVisibility(type[1] ? View.VISIBLE : View.GONE); + wv_day.setVisibility(type[2] ? View.VISIBLE : View.GONE); + wv_hours.setVisibility(type[3] ? View.VISIBLE : View.GONE); + wv_minutes.setVisibility(type[4] ? View.VISIBLE : View.GONE); + wv_seconds.setVisibility(type[5] ? View.VISIBLE : View.GONE); + setContentTextSize(); + } + + /** + * 设置公历 + * + * @param year + * @param month + * @param day + * @param h + * @param m + * @param s + */ + private void setSolar(int year, final int month, int day, int h, int m, int s) { + // 添加大小月月份并将其转换为list,方便之后的判断 + String[] months_big = {"1", "3", "5", "7", "8", "10", "12"}; + String[] months_little = {"4", "6", "9", "11"}; + + final List list_big = Arrays.asList(months_big); + final List list_little = Arrays.asList(months_little); + + currentYear = year; + // 年 + wv_year = (WheelView) view.findViewById(R.id.year); + wv_year.setAdapter(new NumericWheelAdapter(startYear, endYear));// 设置"年"的显示数据 + + + wv_year.setCurrentItem(year - startYear);// 初始化时显示的数据 + wv_year.setGravity(gravity); + // 月 + wv_month = (WheelView) view.findViewById(R.id.month); + if (startYear == endYear) {//开始年等于终止年 + wv_month.setAdapter(new NumericWheelAdapter(startMonth, endMonth)); + wv_month.setCurrentItem(month + 1 - startMonth); + } else if (year == startYear) { + //起始日期的月份控制 + wv_month.setAdapter(new NumericWheelAdapter(startMonth, 12)); + wv_month.setCurrentItem(month + 1 - startMonth); + } else if (year == endYear) { + //终止日期的月份控制 + wv_month.setAdapter(new NumericWheelAdapter(1, endMonth)); + wv_month.setCurrentItem(month); + } else { + wv_month.setAdapter(new NumericWheelAdapter(1, 12)); + wv_month.setCurrentItem(month); + } + wv_month.setGravity(gravity); + // 日 + wv_day = (WheelView) view.findViewById(R.id.day); + + if (startYear == endYear && startMonth == endMonth) { + if (list_big.contains(String.valueOf(month + 1))) { + if (endDay > 31) { + endDay = 31; + } + wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay)); + } else if (list_little.contains(String.valueOf(month + 1))) { + if (endDay > 30) { + endDay = 30; + } + wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay)); + } else { + // 闰年 + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { + if (endDay > 29) { + endDay = 29; + } + wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay)); + } else { + if (endDay > 28) { + endDay = 28; + } + wv_day.setAdapter(new NumericWheelAdapter(startDay, endDay)); + } + } + wv_day.setCurrentItem(day - startDay); + } else if (year == startYear && month + 1 == startMonth) { + // 起始日期的天数控制 + if (list_big.contains(String.valueOf(month + 1))) { + + wv_day.setAdapter(new NumericWheelAdapter(startDay, 31)); + } else if (list_little.contains(String.valueOf(month + 1))) { + + 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)); + } + } + wv_day.setCurrentItem(day - startDay); + } else if (year == endYear && month + 1 == endMonth) { + // 终止日期的天数控制 + if (list_big.contains(String.valueOf(month + 1))) { + if (endDay > 31) { + endDay = 31; + } + wv_day.setAdapter(new NumericWheelAdapter(1, endDay)); + } else if (list_little.contains(String.valueOf(month + 1))) { + if (endDay > 30) { + endDay = 30; + } + wv_day.setAdapter(new NumericWheelAdapter(1, endDay)); + } else { + // 闰年 + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { + if (endDay > 29) { + endDay = 29; + } + wv_day.setAdapter(new NumericWheelAdapter(1, endDay)); + } else { + if (endDay > 28) { + endDay = 28; + } + wv_day.setAdapter(new NumericWheelAdapter(1, endDay)); + } + } + wv_day.setCurrentItem(day - 1); + } 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.setCurrentItem(day - 1); + } + + wv_day.setGravity(gravity); + //时 + wv_hours = (WheelView) view.findViewById(R.id.hour); + wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); + + wv_hours.setCurrentItem(h); + wv_hours.setGravity(gravity); + //分 + wv_minutes = (WheelView) view.findViewById(R.id.min); + wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); + + wv_minutes.setCurrentItem(m); + wv_minutes.setGravity(gravity); + //秒 + wv_seconds = (WheelView) view.findViewById(R.id.second); + wv_seconds.setAdapter(new NumericWheelAdapter(0, 59)); + + wv_seconds.setCurrentItem(s); + wv_seconds.setGravity(gravity); + + // 添加"年"监听 + wv_year.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(int index) { + int year_num = index + startYear; + currentYear = year_num; + int currentMonthItem = wv_month.getCurrentItem();//记录上一次的item位置 + // 判断大小月及是否闰年,用来确定"日"的数据 + if (startYear == endYear) { + //重新设置月份 + wv_month.setAdapter(new NumericWheelAdapter(startMonth, endMonth)); + + if (currentMonthItem > wv_month.getAdapter().getItemsCount() - 1) { + currentMonthItem = wv_month.getAdapter().getItemsCount() - 1; + wv_month.setCurrentItem(currentMonthItem); + } + + int monthNum = currentMonthItem + startMonth; + + if (startMonth == endMonth) { + //重新设置日 + setReDay(year_num, monthNum, startDay, endDay, list_big, list_little); + } else if (monthNum == startMonth) { + //重新设置日 + setReDay(year_num, monthNum, startDay, 31, list_big, list_little); + } else if (monthNum == endMonth) { + setReDay(year_num, monthNum, 1, endDay, list_big, list_little); + } else {//重新设置日 + setReDay(year_num, monthNum, 1, 31, list_big, list_little); + } + } else if (year_num == startYear) {//等于开始的年 + //重新设置月份 + 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) { + //重新设置日 + setReDay(year_num, month, startDay, 31, list_big, list_little); + } else { + //重新设置日 + setReDay(year_num, month, 1, 31, list_big, list_little); + } + + } else if (year_num == endYear) { + //重新设置月份 + 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) { + //重新设置日 + setReDay(year_num, monthNum, 1, endDay, list_big, list_little); + } else { + //重新设置日 + setReDay(year_num, monthNum, 1, 31, list_big, list_little); + } + + } else { + //重新设置月份 + wv_month.setAdapter(new NumericWheelAdapter(1, 12)); + //重新设置日 + setReDay(year_num, wv_month.getCurrentItem() + 1, 1, 31, list_big, list_little); + } + + if (mSelectChangeCallback != null) { + mSelectChangeCallback.onTimeSelectChanged(); + } + } + }); + + + // 添加"月"监听 + wv_month.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(int index) { + int month_num = index + 1; + + if (startYear == endYear) { + month_num = month_num + startMonth - 1; + if (startMonth == endMonth) { + //重新设置日 + setReDay(currentYear, month_num, startDay, endDay, list_big, list_little); + } else if (startMonth == month_num) { + + //重新设置日 + setReDay(currentYear, month_num, startDay, 31, list_big, list_little); + } else if (endMonth == month_num) { + setReDay(currentYear, month_num, 1, endDay, list_big, list_little); + } else { + setReDay(currentYear, month_num, 1, 31, list_big, list_little); + } + } else if (currentYear == startYear) { + month_num = month_num + startMonth - 1; + if (month_num == startMonth) { + //重新设置日 + setReDay(currentYear, month_num, startDay, 31, list_big, list_little); + } else { + //重新设置日 + setReDay(currentYear, month_num, 1, 31, list_big, list_little); + } + + } else if (currentYear == endYear) { + if (month_num == endMonth) { + //重新设置日 + 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); + } + + } else { + //重新设置日 + setReDay(currentYear, month_num, 1, 31, list_big, list_little); + } + + if (mSelectChangeCallback != null) { + mSelectChangeCallback.onTimeSelectChanged(); + } + } + }); + + setChangedListener(wv_day); + setChangedListener(wv_hours); + setChangedListener(wv_minutes); + setChangedListener(wv_seconds); + + if (type.length != 6) { + throw new IllegalArgumentException("type[] length is not 6"); + } + wv_year.setVisibility(type[0] ? View.VISIBLE : View.GONE); + wv_month.setVisibility(type[1] ? View.VISIBLE : View.GONE); + wv_day.setVisibility(type[2] ? View.VISIBLE : View.GONE); + wv_hours.setVisibility(type[3] ? View.VISIBLE : View.GONE); + wv_minutes.setVisibility(type[4] ? View.VISIBLE : View.GONE); + wv_seconds.setVisibility(type[5] ? View.VISIBLE : View.GONE); + setContentTextSize(); + } + + private void setChangedListener(WheelView wheelView) { + if (mSelectChangeCallback != null) { + wheelView.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(int index) { + mSelectChangeCallback.onTimeSelectChanged(); + } + }); + } + + } + + + private void setReDay(int year_num, int monthNum, int startD, int endD, List list_big, List list_little) { + int currentItem = wv_day.getCurrentItem(); + +// int maxItem; + if (list_big.contains(String.valueOf(monthNum))) { + if (endD > 31) { + endD = 31; + } + wv_day.setAdapter(new NumericWheelAdapter(startD, endD)); +// maxItem = endD; + } else if (list_little.contains(String.valueOf(monthNum))) { + if (endD > 30) { + endD = 30; + } + wv_day.setAdapter(new NumericWheelAdapter(startD, endD)); +// maxItem = endD; + } else { + if ((year_num % 4 == 0 && year_num % 100 != 0) + || year_num % 400 == 0) { + if (endD > 29) { + endD = 29; + } + wv_day.setAdapter(new NumericWheelAdapter(startD, endD)); +// maxItem = endD; + } else { + if (endD > 28) { + endD = 28; + } + wv_day.setAdapter(new NumericWheelAdapter(startD, endD)); +// maxItem = endD; + } + } + + if (currentItem > wv_day.getAdapter().getItemsCount() - 1) { + currentItem = wv_day.getAdapter().getItemsCount() - 1; + wv_day.setCurrentItem(currentItem); + } + } + + + private void setContentTextSize() { + wv_day.setTextSize(textSize); + wv_month.setTextSize(textSize); + wv_year.setTextSize(textSize); + wv_hours.setTextSize(textSize); + wv_minutes.setTextSize(textSize); + wv_seconds.setTextSize(textSize); + } + + 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; + } + + if (label_year != null) { + wv_year.setLabel(label_year); + } else { + wv_year.setLabel(view.getContext().getString(R.string.pickerview_year)); + } + if (label_month != null) { + wv_month.setLabel(label_month); + } else { + wv_month.setLabel(view.getContext().getString(R.string.pickerview_month)); + } + if (label_day != null) { + wv_day.setLabel(label_day); + } else { + wv_day.setLabel(view.getContext().getString(R.string.pickerview_day)); + } + if (label_hours != null) { + wv_hours.setLabel(label_hours); + } else { + wv_hours.setLabel(view.getContext().getString(R.string.pickerview_hours)); + } + if (label_mins != null) { + wv_minutes.setLabel(label_mins); + } else { + wv_minutes.setLabel(view.getContext().getString(R.string.pickerview_minutes)); + } + if (label_seconds != null) { + wv_seconds.setLabel(label_seconds); + } else { + wv_seconds.setLabel(view.getContext().getString(R.string.pickerview_seconds)); + } + + } + + 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_hours.setTextXOffset(x_offset_hours); + wv_minutes.setTextXOffset(x_offset_minutes); + wv_seconds.setTextXOffset(x_offset_seconds); + } + + /** + * 设置是否循环滚动 + * + * @param cyclic + */ + public void setCyclic(boolean cyclic) { + wv_year.setCyclic(cyclic); + wv_month.setCyclic(cyclic); + wv_day.setCyclic(cyclic); + wv_hours.setCyclic(cyclic); + wv_minutes.setCyclic(cyclic); + wv_seconds.setCyclic(cyclic); + } + + public String getTime() { + if (isLunarCalendar) { + //如果是农历 返回对应的公历时间 + return getLunarTime(); + } + StringBuilder sb = new StringBuilder(); + if (currentYear == startYear) { + /* int i = wv_month.getCurrentItem() + startMonth; + System.out.println("i:" + i);*/ + if ((wv_month.getCurrentItem() + startMonth) == startMonth) { + sb.append((wv_year.getCurrentItem() + startYear)).append("-") + .append((wv_month.getCurrentItem() + startMonth)).append("-") + .append((wv_day.getCurrentItem() + startDay)).append(" ") + .append(wv_hours.getCurrentItem()).append(":") + .append(wv_minutes.getCurrentItem()).append(":") + .append(wv_seconds.getCurrentItem()); + } else { + sb.append((wv_year.getCurrentItem() + startYear)).append("-") + .append((wv_month.getCurrentItem() + startMonth)).append("-") + .append((wv_day.getCurrentItem() + 1)).append(" ") + .append(wv_hours.getCurrentItem()).append(":") + .append(wv_minutes.getCurrentItem()).append(":") + .append(wv_seconds.getCurrentItem()); + } + + } else { + sb.append((wv_year.getCurrentItem() + startYear)).append("-") + .append((wv_month.getCurrentItem() + 1)).append("-") + .append((wv_day.getCurrentItem() + 1)).append(" ") + .append(wv_hours.getCurrentItem()).append(":") + .append(wv_minutes.getCurrentItem()).append(":") + .append(wv_seconds.getCurrentItem()); + } + + return sb.toString(); + } + + + /** + * 农历返回对应的公历时间 + * + * @return + */ + private String getLunarTime() { + StringBuilder sb = new StringBuilder(); + int year = wv_year.getCurrentItem() + startYear; + int month = 1; + boolean isLeapMonth = false; + if (ChinaDate.leapMonth(year) == 0) { + month = wv_month.getCurrentItem() + 1; + } else { + if ((wv_month.getCurrentItem() + 1) - ChinaDate.leapMonth(year) <= 0) { + month = wv_month.getCurrentItem() + 1; + } else if ((wv_month.getCurrentItem() + 1) - ChinaDate.leapMonth(year) == 1) { + month = wv_month.getCurrentItem(); + isLeapMonth = true; + } else { + month = wv_month.getCurrentItem(); + } + } + int day = wv_day.getCurrentItem() + 1; + int[] solar = LunarCalendar.lunarToSolar(year, month, day, isLeapMonth); + + sb.append(solar[0]).append("-") + .append(solar[1]).append("-") + .append(solar[2]).append(" ") + .append(wv_hours.getCurrentItem()).append(":") + .append(wv_minutes.getCurrentItem()).append(":") + .append(wv_seconds.getCurrentItem()); + return sb.toString(); + } + + public View getView() { + return view; + } + + public void setView(View view) { + this.view = view; + } + + public int getStartYear() { + return startYear; + } + + public void setStartYear(int startYear) { + this.startYear = startYear; + } + + public int getEndYear() { + return endYear; + } + + public void setEndYear(int endYear) { + this.endYear = endYear; + } + + + public void setRangDate(Calendar startDate, Calendar endDate) { + + if (startDate == null && endDate != null) { + int year = endDate.get(Calendar.YEAR); + int month = endDate.get(Calendar.MONTH) + 1; + int day = endDate.get(Calendar.DAY_OF_MONTH); + if (year > startYear) { + this.endYear = year; + this.endMonth = month; + this.endDay = day; + } else if (year == startYear) { + if (month > startMonth) { + this.endYear = year; + this.endMonth = month; + this.endDay = day; + } else if (month == startMonth) { + if (day > startDay) { + this.endYear = year; + this.endMonth = month; + this.endDay = day; + } + } + } + + } else if (startDate != null && endDate == null) { + int year = startDate.get(Calendar.YEAR); + int month = startDate.get(Calendar.MONTH) + 1; + int day = startDate.get(Calendar.DAY_OF_MONTH); + if (year < endYear) { + this.startMonth = month; + this.startDay = day; + this.startYear = year; + } else if (year == endYear) { + if (month < endMonth) { + this.startMonth = month; + this.startDay = day; + this.startYear = year; + } else if (month == endMonth) { + if (day < endDay) { + this.startMonth = month; + this.startDay = day; + this.startYear = year; + } + } + } + + } 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); + } + + } + + /** + * 设置间距倍数,但是只能在1.0-4.0f之间 + * + * @param lineSpacingMultiplier + */ + public void setLineSpacingMultiplier(float lineSpacingMultiplier) { + this.lineSpacingMultiplier = lineSpacingMultiplier; + setLineSpacingMultiplier(); + } + + /** + * 设置分割线的颜色 + * + * @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(); + } + + /** + * 设置分割线的类型 + * + * @param dividerType + */ + public void setDividerType(WheelView.DividerType dividerType) { + this.dividerType = dividerType; + setDividerType(); + } + + /** + * 设置分割线之间的文字的颜色 + * + * @param textColorCenter + */ + public void setTextColorCenter(int textColorCenter) { + this.textColorCenter = textColorCenter; + setTextColorCenter(); + } + + /** + * 设置分割线以外文字的颜色 + * + * @param textColorOut + */ + public void setTextColorOut(int textColorOut) { + this.textColorOut = textColorOut; + setTextColorOut(); + } + + /** + * @param isCenterLabel 是否只显示中间选中项的 + */ + public void isCenterLabel(boolean isCenterLabel) { + wv_day.isCenterLabel(isCenterLabel); + wv_month.isCenterLabel(isCenterLabel); + wv_year.isCenterLabel(isCenterLabel); + wv_hours.isCenterLabel(isCenterLabel); + wv_minutes.isCenterLabel(isCenterLabel); + wv_seconds.isCenterLabel(isCenterLabel); + } + + public void setSelectChangeCallback(ISelectTimeCallback mSelectChangeCallback) { + this.mSelectChangeCallback = mSelectChangeCallback; + } +} diff --git a/wheelview/src/main/java/com/contrarywind/view/WheelView.java b/wheelview/src/main/java/com/contrarywind/view/WheelView.java index 81adb07f..c9ac09a1 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; @@ -76,6 +77,8 @@ public enum DividerType { // 分隔线类型 private int textColorOut; private int textColorCenter; private int dividerColor; + private int dividerWidth; + private int selectItemBgColor; // 条目间距倍数 private float lineSpacingMultiplier = 1.6F; @@ -133,7 +136,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) @@ -155,6 +158,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); textSize = a.getDimensionPixelOffset(R.styleable.pickerview_wheelview_textSize, textSize); lineSpacingMultiplier = a.getFloat(R.styleable.pickerview_wheelview_lineSpacingMultiplier, lineSpacingMultiplier); a.recycle();//回收内存 @@ -204,6 +210,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); } @@ -435,7 +446,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) { //绘制文字,靠右并留出空隙 @@ -760,6 +771,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 142e2a34..bdbcf456 100644 --- a/wheelview/src/main/res/values/attrs.xml +++ b/wheelview/src/main/res/values/attrs.xml @@ -10,6 +10,8 @@ + + \ No newline at end of file