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

feat: add change selected area bg color #866

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public OptionsPickerBuilder setCancelColor(int textColorCancel) {
return this;
}

public OptionsPickerBuilder setSelectAreaColor(int selectAreaColor){
mPickerOptions.selectAreaColor = selectAreaColor;
return this;
}


/**
* {@link #setOutSideColor} instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public PickerOptions(int buildType) {
public int textColorCenter = 0xFF2a2a2a; //分割线之间的文字颜色
public int dividerColor = 0xFFd5d5d5; //分割线的颜色
public int outSideColor = -1; //显示时的外部背景色颜色,默认是灰色
public int selectAreaColor = 0xFFFFFFFF;//选中区域的背景色,默认是白色

public float lineSpacingMultiplier = 1.6f; // 条目间距倍数 默认1.6
public boolean isDialog;//是否是对话框模式
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private void initView(Context context) {
wheelOptions.setTextColorOut(mPickerOptions.textColorOut);
wheelOptions.setTextColorCenter(mPickerOptions.textColorCenter);
wheelOptions.isCenterLabel(mPickerOptions.isCenterLabel);
wheelOptions.setSelectAreaColor(mPickerOptions.selectAreaColor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,16 @@ public void setDividerColor(int dividerColor) {
wv_option3.setDividerColor(dividerColor);
}

/**
* 设置选择区域背景颜色
* @param selectAreaColor
*/
public void setSelectAreaColor(int selectAreaColor){
wv_option1.setSelectAreaColor(selectAreaColor);
wv_option2.setSelectAreaColor(selectAreaColor);
wv_option3.setSelectAreaColor(selectAreaColor);
}

/**
* 设置分割线的类型
*
Expand Down
21 changes: 20 additions & 1 deletion wheelview/src/main/java/com/contrarywind/view/WheelView.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public enum DividerType { // 分隔线类型
private Paint paintOuterText;
private Paint paintCenterText;
private Paint paintIndicator;
private Paint paintSelectArea;

private WheelAdapter adapter;

Expand All @@ -77,6 +78,7 @@ public enum DividerType { // 分隔线类型
private int textColorCenter;
private int dividerColor;
private int dividerWidth;
private int selectAreaColor;

// 条目间距倍数
private float lineSpacingMultiplier = 1.6F;
Expand Down Expand Up @@ -152,6 +154,7 @@ 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);
selectAreaColor = a.getColor(R.styleable.pickerview_wheelview_selectAreaColor, 0xFFFFFFFF);
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);
Expand Down Expand Up @@ -203,6 +206,10 @@ private void initPaints() {
paintIndicator.setColor(dividerColor);
paintIndicator.setAntiAlias(true);

paintSelectArea = new Paint();
paintSelectArea.setColor(selectAreaColor);
paintSelectArea.setAntiAlias(true);

setLayerType(LAYER_TYPE_SOFTWARE, null);
}

Expand Down Expand Up @@ -416,6 +423,7 @@ protected void onDraw(Canvas canvas) {
endX = measuredWidth - startX;
canvas.drawLine(startX, firstLineY, endX, firstLineY, paintIndicator);
canvas.drawLine(startX, secondLineY, endX, secondLineY, paintIndicator);
canvas.drawRect(startX,firstLineY,endX,secondLineY, paintSelectArea);
} else if (dividerType == DividerType.CIRCLE) {
//分割线为圆圈形状
paintIndicator.setStyle(Paint.Style.STROKE);
Expand All @@ -433,10 +441,16 @@ protected void onDraw(Canvas canvas) {
endX = measuredWidth - startX;
//半径始终以宽高中最大的来算
float radius = Math.max((endX - startX), itemHeight) / 1.8f;
canvas.drawCircle(measuredWidth / 2f, measuredHeight / 2f, radius, paintIndicator);

Paint paint = new Paint();
paint.setColor(selectAreaColor);
paint.setStrokeWidth(radius);
canvas.drawCircle(measuredWidth / 2f, measuredHeight / 2f, radius, paint);
canvas.drawCircle(measuredWidth / 2f, measuredHeight / 2f, radius, paintIndicator);
} else {
canvas.drawLine(0.0F, firstLineY, measuredWidth, firstLineY, paintIndicator);
canvas.drawLine(0.0F, secondLineY, measuredWidth, secondLineY, paintIndicator);
canvas.drawRect(0.0F,firstLineY,measuredWidth,secondLineY, paintSelectArea);
}

//只显示选中项Label文字的模式,并且Label文字不为空,则进行绘制
Expand Down Expand Up @@ -802,6 +816,11 @@ public void setDividerColor(int dividerColor) {
paintIndicator.setColor(dividerColor);
}

public void setSelectAreaColor(int selectAreaColor){
this.selectAreaColor = selectAreaColor;
paintSelectArea.setColor(selectAreaColor);
}

public void setDividerType(DividerType dividerType) {
this.dividerType = dividerType;
}
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_selectAreaColor" format="color"/>
<attr name="wheelview_dividerWidth" format="dimension"/>
<attr name="wheelview_lineSpacingMultiplier" format="float"/>
</declare-styleable>
Expand Down