Android 自定义View 刻度表

圆形刻度表  


public class TestDialView extends View {
    private Context context;
    private int jindu = 60;
    //判断是否在改变
    private boolean isDown;


    //写字的笔
    private Paint paintText;
    //外圈笔
    private Paint paintOut;
    //内圈笔
    private Paint paintIn;
    //进度笔
    private Paint paintJindu;
    //圆心
    private Point center;
    //半径
    private float radius;
    //外圈半径
    private float outRadius;
    //内圈半径
    private float inRadius;
    //进度条半径
    private float jinduRadius;
    // 计算刻度间隔
    private float space = 270 / 100F;


    public TestDialView(Context context) {
        super(context);
        this.context = context;
    }


    public TestDialView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }


    public TestDialView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(heightMeasureSpec);
        int height = MeasureSpec.getSize(widthMeasureSpec);


        this.radius = width / 2 < height / 2 ? width / 2 : height / 2;
        this.center = new Point(width / 2, height / 2);
        this.outRadius = radius / 30 * 29;
        this.jinduRadius = outRadius - 20;
        this.inRadius = jinduRadius - 20;
        //初始化
        initView();
    }


    private void initView() {
        Paint basePaint = new Paint();
        basePaint.setAntiAlias(true);
        basePaint.setStyle(Paint.Style.STROKE);


        paintOut = new Paint(basePaint);
        paintOut.setColor(context.getResources().getColor(R.color.colorPrimary));
        paintOut.setStrokeWidth(2);


        paintIn = new Paint(basePaint);
        paintIn.setColor(context.getResources().getColor(R.color.colorPrimary));
        paintIn.setStrokeWidth(2);


        paintText = new Paint(basePaint);
        paintText.setColor(context.getResources().getColor(R.color.textColor99));


        paintJindu = new Paint(basePaint);
        paintJindu.setColor(context.getResources().getColor(R.color.yellow));
        paintJindu.setStrokeCap(Paint.Cap.ROUND);
        paintJindu.setStrokeWidth(radius / 30 * 2);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //先找到最开始左右的2个点
        Point poingLeft = getPoint(center, outRadius, 315.0);
        Point poingright = getPoint(center, outRadius, 45.0);


        canvas.drawLine(poingLeft.x, poingLeft.y, poingLeft.x - 30, poingLeft.y, paintOut);
        canvas.drawLine(poingright.x, poingright.y, poingright.x + 30, poingright.y, paintOut);
        canvas.drawArc(creatRectF(outRadius), 135, 270, false, paintOut);


        paintJindu.setColor(context.getResources().getColor(R.color.textColor99));
        canvas.drawArc(creatRectF(jinduRadius), 135, 270, false, paintJindu);
        paintJindu.setColor(context.getResources().getColor(R.color.yellow));
        canvas.drawArc(creatRectF(jinduRadius), 135, 270 * jindu / 100, false, paintJindu);


        canvas.drawArc(creatRectF(inRadius), 135, 270, false, paintIn);
        int lenght;
        for (int i = 0; i < 101; i++) {
            if (i % 10 == 0) {
                lenght = 12;
            } else if (i % 5 == 0) {
                lenght = 9;
            } else {
                lenght = 5;
            }
            Point start = getPoint(center, inRadius, 315.0 - i * space);
            Point end = getPoint(center, inRadius - lenght, 315.0 - i * space);
            canvas.drawLine(start.x, start.y, end.x, end.y, paintIn);
        }


        //绘制 60%  进度
        paintText.setTextSize(radius / 10 * 6);
        float l = paintText.measureText(jindu + "");
        canvas.drawText(String.valueOf(jindu), (getWidth() - l) / 2, getHeight() / 2, paintText);
        paintText.setTextSize(radius / 10 * 2);
        canvas.drawText("%", (getWidth() - l) / 2 + l, getHeight() / 2, paintText);


        // 绘制内容
        paintText.setTextSize(radius / 15 * 2);
        Paint.FontMetrics fontMetrics = paintText.getFontMetrics();
        float tl = paintText.measureText("使用了xxGB");
        canvas.drawText("使用了xxGB", (getWidth() - tl) / 2, getHeight() / 2 + (fontMetrics.bottom - fontMetrics.top) * 2, paintText);


        //设置开始和末尾值
        float minl = paintText.measureText("0GB");
        float maxl = paintText.measureText("5GB");
        canvas.drawText("0GB", poingLeft.x - minl / 2, poingLeft.y + minl / 3
                * 2, paintText);
        canvas.drawText("5GB", poingright.x - maxl / 2, poingright.y + maxl
                / 3 * 2, paintText);


    }




    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isDown) return true;
        isDown = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                Random random = new Random();
                int n = random.nextInt(100) + 1;
                while (jindu > 0) {
                    jindu--;
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    postInvalidate();
                }


                while (jindu < n) {
                    jindu++;
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    postInvalidate();
                }
                isDown = false;
            }
        }).start();
        return true;
    }


    /**
     * 计算圆上的任意一点位置
     *
     * @param angle 角度
     * @return
     */
    public Point getPoint(Point center, float r, Double angle) {
        Point point = new Point();
        point.x = (float) (center.x + r * Math.sin(angle * Math.PI / 180));
        point.y = (float) (center.y + r * Math.cos(angle * Math.PI / 180));
        return point;
    }


    private RectF creatRectF(Float r) {
        RectF oval = new RectF();
        oval.left = center.x - r;
        oval.top = center.y - r;
        oval.right = center.x + r;
        oval.bottom = center.y + r;
        return oval;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安卓自定义view刻度尺的实现步骤如下: 1. 创建一个自定义view类,继承自View。 2. 在onDraw方法中绘制刻度尺的背景和刻度线。 3. 在onMeasure方法中设置View的尺寸,以适应不同的屏幕大小。 4. 在onTouchEvent方法中处理用户的手势操作,例如手指滑动刻度尺、点击刻度尺等。 5. 在回调接口中提供获取刻度值的方法,以便外部获取用户选择的刻度值。 6. 使用自定义属性来设置刻度尺的样式和属性,例如刻度尺的颜色、刻度线的宽度和间距等。 以下是一个简单的安卓自定义view刻度尺示例代码: ``` public class RulerView extends View { private int mWidth; // View的宽度 private int mHeight; // View的高度 private int mScaleWidth; // 刻度线宽度 private int mScaleHeight; // 刻度线高度 private int mScaleMargin; // 刻度线间隔 private int mScaleMax; // 刻度尺最大值 private int mScaleMin; // 刻度尺最小值 private int mScaleValue; // 当前刻度值 private Paint mPaint; // 画笔 public RulerView(Context context) { super(context); init(); } public RulerView(Context context, AttributeSet attrs) { super(context, attrs); initAttrs(context, attrs); init(); } public RulerView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initAttrs(context, attrs); init(); } private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); } private void initAttrs(Context context, AttributeSet attrs) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RulerView); mScaleWidth = ta.getDimensionPixelSize(R.styleable.RulerView_scaleWidth, dp2px(1)); mScaleHeight = ta.getDimensionPixelSize(R.styleable.RulerView_scaleHeight, dp2px(10)); mScaleMargin = ta.getDimensionPixelSize(R.styleable.RulerView_scaleMargin, dp2px(10)); mScaleMin = ta.getInt(R.styleable.RulerView_scaleMin, 0); mScaleMax = ta.getInt(R.styleable.RulerView_scaleMax, 100); mScaleValue = ta.getInt(R.styleable.RulerView_scaleValue, 0); ta.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); drawBackground(canvas); drawScale(canvas); } private void drawBackground(Canvas canvas) { // 绘制刻度尺背景 } private void drawScale(Canvas canvas) { // 绘制刻度线 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = MeasureSpec.getSize(widthMeasureSpec); mHeight = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(mWidth, mHeight); } @Override public boolean onTouchEvent(MotionEvent event) { // 处理用户手势操作 return true; } public int getScaleValue() { return mScaleValue; } private int dp2px(int dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); } } ``` 在布局文件中使用该自定义view: ``` <com.example.rulerview.RulerView android:id="@+id/ruler_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:scaleWidth="2dp" app:scaleHeight="20dp" app:scaleMargin="20dp" app:scaleMin="0" app:scaleMax="200" app:scaleValue="100" /> ``` 在attrs.xml中定义定义属性: ``` <declare-styleable name="RulerView"> <attr name="scaleWidth" format="dimension" /> <attr name="scaleHeight" format="dimension" /> <attr name="scaleMargin" format="dimension" /> <attr name="scaleMin" format="integer" /> <attr name="scaleMax" format="integer" /> <attr name="scaleValue" format="integer" /> </declare-styleable> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值