组件--游标尺 数值选择器

本文介绍了一种自定义的数值选择器组件实现方法,该组件采用尺子样式设计,支持用户通过滑动来调整数值。文章提供了详细的代码示例,并解释了其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近看到很多金融应用用一种数值选择器,一把尺子的设计,刻度可以左右滑动改变,如下图:
这里写图片描述

不多说,上代码

public class Ruler extends View {

    private static final int RULER_LINE_MAX = 50;
    private static final int RULER_LINE_MID = 30;
    private static final int RULER_LINE_MIN = 20;
    private static final int RULER_LINE_SLOT = 15;
    private final int slop;
    int width;
    int height;
    Paint mPaint;
    Paint mColorPaint;
    float lastX;

    float moveX;
    float bottom;

    int current;

    public Ruler(Context context) {
        this(context, null);
    }

    public Ruler(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Ruler(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        slop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
        mPaint = new Paint();
        mColorPaint = new Paint();
        mColorPaint.setColor(Color.RED);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                return true;
            case MotionEvent.ACTION_MOVE:
                float dx = lastX - event.getX();
                moveX += dx;
                Log.e("----", dx + "");
                lastX = event.getX();
                if (moveX < 0) {
                    moveX = 0;
                }
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
                if (moveX % RULER_LINE_SLOT == 0) {
                    invalidate();
                    return true;
                }
                moveX = (Math.round(moveX / RULER_LINE_SLOT)) * RULER_LINE_SLOT;
                invalidate();
                return true;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (height == 0) {
            width = getMeasuredWidth();
            height = getMeasuredHeight();
            bottom = height / 2;
        }
        int lineLen = 0;
        boolean drawText;
        for (int i = (int) (moveX / RULER_LINE_SLOT) + 1, slot = RULER_LINE_SLOT - (int) (moveX % RULER_LINE_SLOT); slot < width; i++, slot += RULER_LINE_SLOT) {
            if (i % 10 == 0) {
                lineLen = RULER_LINE_MAX;
                drawText = true;
            } else if (i % 5 == 0) {
                lineLen = RULER_LINE_MID;
                drawText = false;
            } else {
                lineLen = RULER_LINE_MIN;
                drawText = false;
            }
            canvas.drawLine(slot, bottom, slot, bottom - lineLen, mPaint);
            if (drawText) {
                canvas.drawText(String.valueOf(i), slot, bottom - lineLen, mPaint);
            }
            if (Math.abs(slot - width / 2) < RULER_LINE_SLOT / 2) {
                current = i;
            }
        }
        canvas.drawLine(0, bottom, width, bottom, mPaint);
        canvas.drawLine(width / 2, bottom, width / 2, bottom - RULER_LINE_MAX * 3 / 2, mColorPaint);
        canvas.drawText(String.valueOf(current), width / 2, bottom - RULER_LINE_MAX * 3 / 2, mColorPaint);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值