项目需求,需要根据UI自定义日期选择器(Android自带的DatePicker极丑)。
所以自定义了WheelView,并在此基础上自定义日期选择器。
参考1.WheelView TimePicker CityPicker 滚轮(联动)选择器
自定义WheelView代码如下:
/**
* WheelView滚轮
*
*/
public class WheelView extends View {
/**
* 控件宽度
*/
private float controlWidth;
/**
* 控件高度
*/
private float controlHeight;
/**
* 是否滑动中
*/
private boolean isScrolling = false;
/**
* 选择的内容
*/
private ArrayList<ItemObject> itemList = new ArrayList<>();
/**
* 设置数据
*/
private ArrayList<String> dataList = new ArrayList<>();
/**
* 按下的坐标
*/
private int downY;
/**
* 按下的时间
*/
private long downTime = 0;
/**
* 短促移动
*/
private long goonTime = 200;
/**
* 短促移动距离
*/
private int goonDistance = 100;
/**
* 画线画笔
*/
private Paint linePaint;
/**
* 线的默认颜色
*/
private int lineColor = 0xff000000;
/**
* 线的默认宽度
*/
private float lineHeight = 2f;
/**
* 默认字体
*/
private float normalFont = 14.0f;
/**
* 选中的时候字体
*/
private float selectedFont = 22.0f;
/**
* 单元格高度
*/
private int unitHeight = 50;
/**
* 显示多少个内容
*/
private int itemNumber = 7;
/**
* 默认字体颜色
*/
private int normalColor = 0xff000000;
/**
* 选中时候的字体颜色
*/
private int selectedColor = 0xffff0000;
/**
* 蒙板高度
*/
private float maskHeight = 48.0f;
/**
* 选择监听
*/
private OnSelectListener onSelectListener;
/**
* 是否可用
*/
private boolean isEnable = true;
/**
* 刷新界面
*/
private static final int REFRESH_VIEW = 0x001;
/**
* 移动距离
*/
private static final int MOVE_NUMBER = 5;
/**
* 是否允许选空
*/
private boolean noEmpty = true;
/**
* 正在修改数据,避免ConcurrentModificationException异常
*/
private boolean isClearing = false;
public WheelView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
initData();
}
public WheelView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
initData();
}
public WheelView(Context context) {
super(context);
initData();
}
/**
* 初始化,获取设置的属性
*
* @param context
* @param attrs
*/
private void init(Context context, AttributeSet attrs) {
TypedArray attribute = context.obtainStyledAttributes(attrs, R.styleable.WheelView);
unitHeight = (int) attribute.getDimension(R.styleable.WheelView_unitHeight, unitHeight);
itemNumber = attribute.getInt(R.styleable.WheelView_itemNumber, itemNumber);
normalFont = attribute.getDimension(R.styleable.WheelView_normalTextSize, normalFont);
selectedFont = attribute.getDimension(R.styleable.WheelView_selectedTextSize, selectedFont);
normalColor = attribute.getColor(R.styleable.WheelView_normalTextColor, normalColor);
selectedColor = attribute.getColor(R.styleable.WheelView_selectedTextColor, selectedColor);
lineColor = attribute.getColor(R.styleable.WheelView_lineColor, lineColor);
lineHeight = attribute.getDimension(R.styleable.WheelView_lineHeight, lineHeight);
maskHeight = attribute.getDimension(R.styleable.WheelView_maskHeight, maskHeight);
noEmpty = attribute.getBoolean(R.styleable.WheelView_noEmpty, true);
isEnable = attribute.getBoolean(R.styleable.WheelView_isEnable, true);
attribute.recycle();
controlHeight = itemNumber * unitHeight;
}
/**
* 初始化数据
*/
private void initData() {
isClearing = true;
itemList.clear();
for (int i = 0; i < dataList.size(); i++) {
ItemObject itemObject = new ItemObject();
itemObject.id = i;
itemObject.itemText = dataList.get(i);
itemObject.x = 0;
itemObject.y = i * unitHeight;
itemList.add(itemObject);
}
isClearing = false;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
controlWidth = getMeasuredWidth();
if (controlWidth != 0) {
setMeasuredDimension(getMeasuredWidth(), itemNumber * unitHeight);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawLine(canvas);
drawList(canvas);
drawMask(canvas);
}
/**
* 绘制线条
*
* @param canvas
*/
private void drawLine(Canvas canvas) {
if (linePaint == null) {
linePaint = new Paint();
linePaint.setColor(lineColor);
linePaint.setAntiAlias(true);
linePaint.setStrokeWidth(lineHeight);
}
canvas.drawLine(0, controlHeight / 2 - unitHeight / 2 + lineHeight,
controlWidth, controlHeight / 2 - unitHeight / 2 + lineHeight, linePaint);
canvas.drawLine(0, controlHeight / 2 + unitHeight / 2 - lineHeight,
controlWidth, controlHeight / 2 + unitHeight / 2 - lineHeight, linePaint);
}
private synchronized void drawList(Canvas canvas) {
if (isClearing)
return;
try {
for (ItemObject itemObject : itemList) {
itemObject.drawSelf(canvas, getMeasuredWidth());
}
} catch (Exception e) {
}
}
/**
* 绘制遮盖板
*
* @param canvas
*/
private void drawMask(Canvas canvas) {
LinearGradient lg = new LinearGradient(0, 0, 0, maskHeight, 0x00f2f2f2,
0x00f2f2f2, Shader.TileMode.MIRROR);
Paint paint = new Paint();
paint.setShader(lg);
canvas.drawRect(0, 0, controlWidth, maskHeight, paint);
LinearGradient lg2 = new LinearGradient(0, controlHeight - maskHeight,
0, controlHeight, 0x00f2f2f2, 0x00f2f2f2, Shader.TileMode.MIRROR);
Paint paint2 = new Paint();
paint2.setShader(lg2);
canvas.drawRect(0, controlHeight - maskHeight, controlWidth,
con