简易滚动选择视图

用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值