用Scroller绘制简易滚动选择视图
/** * 数字滚动显示视图 * Created by yudongbo on 2017/9/25. */ public class NumberScrollView extends View { private Paint centerPaint; private Paint otherPaint; private Scroller mScroller; private int topY; private int centerY; private int bottomY; private String topValue = "0"; private String centerValue = "1"; private String bottomValue = "2"; private String newValue = "3"; private int scrollUnit; private int scrollFlag; public NumberScrollView(Context context) { this(context, null); } public NumberScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public NumberScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { centerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); otherPaint = new Paint(Paint.ANTI_ALIAS_FLAG); centerPaint.setTextSize(DensityUtils.sp2px(getContext(), 24)); centerPaint.setColor(0xffffffff); otherPaint.setTextSize(DensityUtils.sp2px(getContext(), 24)); otherPaint.setColor(0x4dffffff); mScroller = new Scroller(getContext(), new LinearInterpolator()); } /** * 设置当前选择状态 * * @param isSelect true:选中, false:未选中 */ public void setSelect(boolean isSelect) { if (isSelect) { otherPaint.setColor(0xffffffff); } else { otherPaint.setColor(0x4dffffff); } } /** * 计算滚动距离 */ @Override public void computeScroll() { super.computeScroll(); if (mScroller.computeScrollOffset()) { invalidate(); } else { if (scrollFlag == 1) { scrollFlag = 0; setValue(centerValue, bottomValue, newValue); } else if (scrollFlag == 2) { scrollFlag = 0; setValue(newValue, topValue, centerValue); } } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Paint.FontMetricsInt otherMetri = otherPaint.getFontMetricsInt(); int fontHeight = otherMetri.bottom - otherMetri.top; int space = (getHeight() - 3 * fontHeight) / 2; scrollUnit = fontHeight + space; topY = fontHeight - 10; centerY = getMeasuredHeight() / 2 + fontHeight / 2 - 10; bottomY = getMeasuredHeight() - 10; } /** * 设置当前值 * @param topValue 上值 * @param centerValue 中间值 * @param bottomValue 下值 */ public void setValue(String topValue, String centerValue, String bottomValue) { Log.d(getClass().getSimpleName(), "topValue:" + topValue + ", centerValue:" + centerValue + ", bottomValue:" + bottomValue); this.topValue = topValue; this.centerValue = centerValue; this.bottomValue = bottomValue; postInvalidate(); } /** * 设置颜色 * @param centerColor * @param otherColor */ public void setColor(int centerColor, int otherColor) { centerPaint.setColor(centerColor); otherPaint.setColor(otherColor); } /** * 设置字体大小 * @param textSize */ public void setTextSize(int textSize) { centerPaint.setTextSize(textSize); otherPaint.setTextSize(textSize); } /** * 获取中间值 * @return */ public String getCenterValue() { return centerValue; } /** * 获取上面值 * @return */ public String getTopValue() { return topValue; } /** * 获取下面值 * @return */ public String getBottomValue() { return bottomValue; } /** * 从下面插入值 * @param newValue */ public void scrollNext(String newValue) { if(!mScroller.isFinished()) { return; } scrollFlag = 1; this.newValue = newValue; mScroller.startScroll(0, 0, 0, -scrollUnit, 80); invalidate(); } /** * 从上面插入值 * @param newValue */ public void scrollPreview(String newValue) { if(!mScroller.isFinished()) { return; } scrollFlag = 2; this.newValue = newValue; mScroller.startScroll(0, 0, 0, scrollUnit, 80); invalidate(); } @Override protected void onDraw(Canvas canvas) { int scrollY = mScroller.getCurrY(); int x = (int) (getMeasuredWidth() / 2 - centerPaint.measureText(centerValue) / 2); if (scrollFlag == 0) { canvas.drawText(topValue, x, topY, otherPaint); canvas.drawText(centerValue, x, centerY, centerPaint); canvas.drawText(bottomValue, x, bottomY, otherPaint); } else { canvas.drawText(topValue, x, topY + scrollY, otherPaint); canvas.drawText(centerValue, x, centerY + scrollY, centerPaint); canvas.drawText(bottomValue, x, bottomY + scrollY, otherPaint); } if (scrollFlag == 1) { canvas.drawText(newValue, x, bottomY + scrollUnit + scrollY, otherPaint); } else if (scrollFlag == 2) { canvas.drawText(newValue, x, topY - scrollUnit + scrollY, otherPaint); } } }