Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加设置选中item的颜色、label位置、item显示个数 #558

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,33 @@ public void setCyclic(boolean cyclic) {
wv_seconds.setCyclic(cyclic);
}

public void setItemsVisibleNum(int visibleNum){
wv_year.setItemsVisibleNum(visibleNum);
wv_month.setItemsVisibleNum(visibleNum);
wv_day.setItemsVisibleNum(visibleNum);
wv_hours.setItemsVisibleNum(visibleNum);
wv_minutes.setItemsVisibleNum(visibleNum);
wv_seconds.setItemsVisibleNum(visibleNum);
}

public void setCenterItemBgColor(int color){
wv_year.setCenterItemBgColor(color);
wv_month.setCenterItemBgColor(color);
wv_day.setCenterItemBgColor(color);
wv_hours.setCenterItemBgColor(color);
wv_minutes.setCenterItemBgColor(color);
wv_seconds.setCenterItemBgColor(color);
}

public void setLabelGravity(WheelView.LabelGravity gravity){
wv_year.setLabelGravity(gravity);
wv_month.setLabelGravity(gravity);
wv_day.setLabelGravity(gravity);
wv_hours.setLabelGravity(gravity);
wv_minutes.setLabelGravity(gravity);
wv_seconds.setLabelGravity(gravity);
}

public String getTime() {
if (isLunarCalendar) {
//如果是农历 返回对应的公历时间
Expand Down
88 changes: 83 additions & 5 deletions wheelview/src/main/java/com/contrarywind/view/WheelView.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public enum DividerType { // 分隔线类型
FILL, WRAP
}

public enum LabelGravity{ // label
RIGHT/*最右边*/, AFTER_CONTENT/*文字后面*/
}

private DividerType dividerType;//分隔线类型

private Context context;
Expand All @@ -61,6 +65,7 @@ public enum DividerType { // 分隔线类型
private Paint paintOuterText;
private Paint paintCenterText;
private Paint paintIndicator;
private Paint paintCenterBg;

private WheelAdapter adapter;

Expand All @@ -76,6 +81,7 @@ public enum DividerType { // 分隔线类型
private int textColorOut;
private int textColorCenter;
private int dividerColor;
private int centerItemBgColor;

// 条目间距倍数
private float lineSpacingMultiplier = 1.6F;
Expand Down Expand Up @@ -118,11 +124,16 @@ public enum DividerType { // 分隔线类型
private int widthMeasureSpec;

private int mGravity = Gravity.CENTER;
private LabelGravity mLabelGravity = LabelGravity.RIGHT;
private int drawCenterContentStart = 0;//中间选中文字开始绘制位置
private int drawOutContentStart = 0;//非中间文字开始绘制位置
private static final float SCALE_CONTENT = 0.8F;//非中间文字则用此控制高度,压扁形成3d错觉
private float CENTER_CONTENT_OFFSET;//偏移量

//item内容的最大宽度
private int maxItemContentWidth;
private String maxItemContent = "";

private final float DEFAULT_TEXT_TARGET_SKEWX = 0.5f;

public WheelView(Context context) {
Expand Down Expand Up @@ -154,6 +165,7 @@ public WheelView(Context context, AttributeSet attrs) {
mGravity = a.getInt(R.styleable.pickerview_wheelview_gravity, Gravity.CENTER);
textColorOut = a.getColor(R.styleable.pickerview_wheelview_textColorOut, 0xFFa8a8a8);
textColorCenter = a.getColor(R.styleable.pickerview_wheelview_textColorCenter, 0xFF2a2a2a);
centerItemBgColor = a.getColor(R.styleable.pickerview_wheelview_centerItemBgColor, 0x00000000);
dividerColor = a.getColor(R.styleable.pickerview_wheelview_dividerColor, 0xFFd5d5d5);
textSize = a.getDimensionPixelOffset(R.styleable.pickerview_wheelview_textSize, textSize);
lineSpacingMultiplier = a.getFloat(R.styleable.pickerview_wheelview_lineSpacingMultiplier, lineSpacingMultiplier);
Expand Down Expand Up @@ -205,6 +217,10 @@ private void initPaints() {
paintIndicator.setColor(dividerColor);
paintIndicator.setAntiAlias(true);

paintCenterBg = new Paint();
paintCenterBg.setColor(centerItemBgColor);
paintCenterBg.setAntiAlias(true);

setLayerType(LAYER_TYPE_SOFTWARE, null);
}

Expand Down Expand Up @@ -324,10 +340,26 @@ public final void setOnItemSelectedListener(OnItemSelectedListener OnItemSelecte

public final void setAdapter(WheelAdapter adapter) {
this.adapter = adapter;
measureMaxItemContentWidth();
remeasure();
invalidate();
}

private void measureMaxItemContentWidth(){
if(adapter != null){
int maxWidth = 0;
for (int i = 0; i < adapter.getItemsCount(); i++) {
String content = adapter.getItem(i).toString();
int width = getTextWidth(paintCenterText, content);
if(width > maxWidth){
maxWidth = width;
maxItemContent = content;
}
}
maxItemContentWidth = maxWidth;
}
}

public final WheelAdapter getAdapter() {
return adapter;
}
Expand Down Expand Up @@ -414,6 +446,9 @@ protected void onDraw(Canvas canvas) {

}

//绘制中间item背景颜色
canvas.drawRect(0.0f, firstLineY, measuredWidth, secondLineY, paintCenterBg);

//绘制中间两条横线
if (dividerType == DividerType.WRAP) {//横线长度仅包裹内容
float startX;
Expand All @@ -438,9 +473,15 @@ 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(mLabelGravity == LabelGravity.RIGHT){
//绘制文字,靠右并留出空隙
int drawRightContentStart = measuredWidth - getTextWidth(paintCenterText, label);
canvas.drawText(label, drawRightContentStart - CENTER_CONTENT_OFFSET, centerY, paintCenterText);
}else {
measuredCenterContentStart(maxItemContent);
int drawRightContentStart = drawCenterContentStart + maxItemContentWidth;
canvas.drawText(label, drawRightContentStart + CENTER_CONTENT_OFFSET, centerY, paintCenterText);
}
}

counter = 0;
Expand Down Expand Up @@ -591,7 +632,11 @@ private void measuredCenterContentStart(String content) {
if (isOptions || label == null || label.equals("") || !isCenterLabel) {
drawCenterContentStart = (int) ((measuredWidth - rect.width()) * 0.5);
} else {//只显示中间label时,时间选择器内容偏左一点,留出空间绘制单位标签
drawCenterContentStart = (int) ((measuredWidth - rect.width()) * 0.25);
if(mLabelGravity == LabelGravity.RIGHT){
drawCenterContentStart = (int) ((measuredWidth - rect.width()) * 0.25);
}else {
drawCenterContentStart = (int) ((measuredWidth - rect.width()) * 0.5);
}
}
break;
case Gravity.LEFT:
Expand All @@ -611,7 +656,11 @@ private void measuredOutContentStart(String content) {
if (isOptions || label == null || label.equals("") || !isCenterLabel) {
drawOutContentStart = (int) ((measuredWidth - rect.width()) * 0.5);
} else {//只显示中间label时,时间选择器内容偏左一点,留出空间绘制单位标签
drawOutContentStart = (int) ((measuredWidth - rect.width()) * 0.25);
if(mLabelGravity == LabelGravity.RIGHT){
drawOutContentStart = (int) ((measuredWidth - rect.width()) * 0.25);
}else {
drawOutContentStart = (int) ((measuredWidth - rect.width()) * 0.5);
}
}
break;
case Gravity.LEFT:
Expand Down Expand Up @@ -807,6 +856,35 @@ public float getItemHeight() {
return itemHeight;
}

public void setItemHeight(float itemHeight) {
this.itemHeight = itemHeight;
}

public int getItemsVisibleNum() {
return itemsVisible;
}

public void setItemsVisibleNum(int itemsVisible) {
this.itemsVisible = itemsVisible;
}

public int getCenterItemBgColor() {
return centerItemBgColor;
}

public void setCenterItemBgColor(int centerItemBgColor) {
this.centerItemBgColor = centerItemBgColor;
paintCenterBg.setColor(centerItemBgColor);
}

public LabelGravity getLabelGravity() {
return mLabelGravity;
}

public void setLabelGravity(LabelGravity mLabelGravity) {
this.mLabelGravity = mLabelGravity;
}

public int getInitPosition() {
return initPosition;
}
Expand Down
1 change: 1 addition & 0 deletions wheelview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<attr name="wheelview_textColorOut" format="color"/>
<attr name="wheelview_textColorCenter" format="color"/>
<attr name="wheelview_dividerColor" format="color"/>
<attr name="wheelview_centerItemBgColor" format="color"/>
<attr name="wheelview_lineSpacingMultiplier" format="float"/>
</declare-styleable>
</resources>