博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简易滚动选择视图
阅读量:5347 次
发布时间:2019-06-15

本文共 5452 字,大约阅读时间需要 18 分钟。

用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); } } }

转载于:https://www.cnblogs.com/yudongbo/p/7602187.html

你可能感兴趣的文章
Python之旅Day14 JQuery部分
查看>>
core--线程池
查看>>
redux-effect
查看>>
Android轻量级的开源缓存框架ASimpleCache
查看>>
他山之石:加载图片的一个小问题
查看>>
shell - 常识
查看>>
mssql sqlserver 使用sql脚本 清空所有数据库表数据的方法分享
查看>>
分层图最短路【bzoj2763】: [JLOI2011]飞行路线
查看>>
linux下编译复数类型引发的错误:expected unqualified-id before '(' token
查看>>
codeforces 1041A Heist
查看>>
Spring Cloud Stream消费失败后的处理策略(三):使用DLQ队列(RabbitMQ)
查看>>
bzoj1048 [HAOI2007]分割矩阵
查看>>
Java中的编码
查看>>
PKUWC2018 5/6
查看>>
As-If-Serial 理解
查看>>
洛谷P1005 矩阵取数游戏
查看>>
在Silverlight中使用HierarchicalDataTemplate为TreeView实现递归树状结构
查看>>
无线通信基础(一):无线网络演进
查看>>
如何在工作中快速成长?阿里资深架构师给工程师的10个简单技巧
查看>>
WebSocket 时时双向数据,前后端(聊天室)
查看>>