刻度尺view

该代码实现了一个自定义的RulerViewJava,包括背景填充、刻度线、游标(三角形)和文字标签。使用Paint对象绘制刻度尺,通过TextView显示刻度值。游标的颜色、位置可调整,并支持设置文字标签。

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

package com.allynav.pilesystem.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView; 

public class RulerViewJava extends View {
    private Paint backgroundPaint = new Paint();
    private Paint tickPaint = new Paint();
    private Paint cursorPaint = new Paint();
    private Paint textPaint = new Paint();
    private TextView cursorLabel;  //三角形底部文字


    private TextView cursorLabelLeft;  //刻度文字
    private TextView cursorLabelLeft1;  //刻度文字
    private TextView cursorLabelCenter;  //刻度文字
    private TextView cursorLabelRight;  //刻度文字
    private TextView cursorLabelRight1;  //刻度文字

    private int l1 = 8;    //-0.2位置
    private int r1 = 12;   //0.2位置

    private int cursorPosition = getWidth() / 2;
    private float offsetX;
    private float offsetY;

    public RulerViewJava(Context context, AttributeSet attrs) {
        super(context, attrs);

        backgroundPaint.setColor(Color.WHITE);
        backgroundPaint.setStyle(Paint.Style.FILL);

        tickPaint.setStyle(Paint.Style.FILL);
        tickPaint.setStrokeWidth(dpToPx(4f));

        cursorPaint.setColor(Color.RED);
        cursorPaint.setStyle(Paint.Style.FILL);

        cursorLabel = new TextView(context);
        cursorLabel.setTextSize(10);
        cursorLabel.setTextColor(Color.BLACK);

        cursorLabelLeft = new TextView(context);
        cursorLabelLeft.setTextSize(13);
        cursorLabelLeft.setText("-1.0");

        cursorLabelLeft1 = new TextView(context);
        cursorLabelLeft1.setTextSize(13);
        cursorLabelLeft1.setText("-0.2");

        cursorLabelCenter = new TextView(context);
        cursorLabelCenter.setTextSize(13);
        cursorLabelCenter.setText("0.0");

        cursorLabelRight = new TextView(context);
        cursorLabelRight.setTextSize(13);
        cursorLabelRight.setText("0.2");

        cursorLabelRight1 = new TextView(context);
        cursorLabelRight1.setTextSize(13);
        cursorLabelRight1.setText("1.0");

        textPaint.setColor(Color.BLUE);
        textPaint.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        RectF backgroundRect = new RectF(0f, 0f, getWidth(), getHeight() / 4);
        canvas.drawRect(backgroundRect, backgroundPaint);

        float startX = getPaddingLeft();
        float endX = getWidth() - getPaddingRight();
        float startY = getHeight() / 5f;
        float endY = startY;

        float tickSpacing = dpToPx(20f);
        float tickLength = dpToPx(10f);
        int tickCount = 21;

        int middleTickIndex = tickCount / 2;

        for (int i = 0; i < tickCount; i++) {

            float x = startX + i * tickSpacing;

            tickPaint.setColor(getTickColor(i, middleTickIndex));

            canvas.drawLine(x, startY, x, endY - tickLength, tickPaint);
        }

        float triangleSide = dpToPx(10f);

        float centerX = getWidth() / 2f + offsetX;
        float centerY = getHeight() / 7f + offsetY;

        float baseMidX = centerX;
        float baseMidY = centerY + triangleSide / 2;

        float leftX = baseMidX - triangleSide / 2;
        float leftY = baseMidY;

        float rightX = baseMidX + triangleSide / 2;
        float rightY = leftY;

        float topX = centerX;
        float topY = baseMidY - triangleSide;

        Path path = new Path();
        path.moveTo(leftX, leftY);
        path.lineTo(rightX, rightY);
        path.lineTo(topX, topY);
        path.close();

        canvas.drawPath(path, cursorPaint);

        canvas.drawText(cursorLabel.getText().toString(), rightX - 20, rightY + 10, textPaint);

        canvas.drawText(cursorLabelLeft.getText().toString(), startX, startY - 15, textPaint);

        canvas.drawText(cursorLabelLeft1.getText().toString(), startX + l1 * tickSpacing - 10, startY - 15, textPaint);   //-0.2

        canvas.drawText(cursorLabelCenter.getText().toString(), startX + 10 * tickSpacing - 10, startY - 15, textPaint);

        canvas.drawText(cursorLabelRight.getText().toString(), startX + r1 * tickSpacing - 10, startY - 15, textPaint);    //0.2

        canvas.drawText(cursorLabelRight1.getText().toString(), startX + 20 * tickSpacing, startY - 15, textPaint);

        setOffsetYs(20f);     //设置三角形离刻度尺高度
//        setCursorLabel("0.5");    //画出三角形底部的文字
    }

    public void setCursorPosition(float position) {
        cursorPosition = (int) position;
        invalidate();
    }

    private float dpToPx(float dp) {
        float density = getResources().getDisplayMetrics().density;
        return dp * density;
    }

    public void setOffsetXs(float offset) {
        this.offsetX = offset;
        cursorLabel.setX(offset);
        invalidate();
    }

    public void setOffsetYs(float offset) {
        this.offsetY = offset;
        invalidate();
    }

    /**
     * 设置文本
     *
     * @param label
     */
    public void setCursorLabel(String label) {
        cursorLabel.setText(label + "˚");
        invalidate();  // 重绘View
    }

    /**
     * 设置刻度尺位置
     * @param left1
     * @param right
     * @param l1
     * @param r1
     */
    public void setScaleSize(String left1, String right, int l1, int r1) {
        cursorLabelLeft1.setText(left1);
        cursorLabelRight.setText(right);
        this.l1 = l1;
        this.r1 = r1;
        if (l1 == 0){
            this.l1 = 8;
        }
        if (r1 == 0) {
            this.r1 = 12;
        }
        invalidate();  // 重绘View
    }


    private int getTickColor(int tickIndex, int middleTickIndex) {
        if (tickIndex == 10) {
            return Color.GREEN;
        } else if (tickIndex == l1 || tickIndex == r1) {
            return Color.RED;
        } else {
            return Color.GRAY;
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迪霸LZTXDY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值