在Android上绘制自定义Seekbar [英] Draw custom Seekbar on Android

查看:943
本文介绍了在Android上绘制自定义Seekbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道GitHub上有很多关于此的库,并且关于此参数的堆栈溢出有几个问题.但是这些都不符合我的需求.

I know there are a lot of libraries about that on GitHub and several questions on stack overflow about this argument. But no of those fit my needs.

我只需要画这样的东西:

I just need to draw something like this :

进度线必须为黄色且没有拇指.

The progress line must be yellow with no thumb.

谢谢.

推荐答案

您可以使用类似于我解释的内容

You can use something similar to what I explained here.

仅使用矩形而不是圆形.使用on-size更改来计算矩形的实际大小,并可以使用一种方法来更改百分比并重新计算矩形大小/重新创建路径.

Just use a rectangle instead of a circle. Use on-size changed to compute the actual size of the rectangle and have a method to change the percentage and re-compute your rectangle size / recreate your path.

更改路径时,只需调用invalidate()即可重画.

When you change the path you just have to call invalidate() to cause a re-draw.

伪代码:

public void setProgress(float progress) {
    this.progress = progress;
    refreshProgressBar();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    this.currentW = w;
    this.currentH = h;
    refreshProgressBar();
}

private void refreshProgressBar() {
    path.reset();
    // build your clipping path using progress / currentW / currentH
    path.close();
    invalidate();
}

您可以使用单个自定义视图来完成所有操作,也可以将两个视图彼此重叠,其中包含条形图填充"的一个视图已用我上面介绍的方法进行了裁剪.

You can do all with a single custom view or have 2 views on top of each other, the one containing the "fill" of the bar is clipped with the method I explained above.

请记住,剪裁发生在画布上.因此,如果您使用自定义视图,则只需在绘制进度视图之前设置裁剪并在完成后将其删除(在这种情况下,您也可以直接绘制颜色-但是如果您使用的是复杂的颜色图案,则裁剪起来可能会更容易).如果您像在我给您的链接中那样使用裁剪的ViewGroup,则必须相应地构建布局,以便裁剪的视图仅是进度视图.

Keep in mind the clipping happens on the Canvas. So if you you use a custom View just set the clipping before drawing the progress view and remove it when you are done (you can also directly draw the color in this case - but if you have a complex color pattern it may be easier to clip). If you use a clipping ViewGroup like in the link I gave you you'll have to build your layout accordingly so that the clipped view is only the progress one.

如果您对数学有所了解,也许还可以绘制一条与您的艺术作品相匹配的路径,并在布局顶部的全彩色矩形中使用它.

If you play a bit with math you may also be able to draw a path that match your art work and use it on a full color rectangle on top of your layout.

如果将矩形和一个圆加在一起,然后删除一个圆(使用OP在路径上进行加/减/乘),则应获得与手绘图相似的路径(形状)

If you add together a Rectangle and a circle and then remove a circle (use the OP for adding / subtracting / multiplicating on the path) you should obtain a Path (shape) that resemble the one in your hand-drawing.

祝你好运.

这篇关于在Android上绘制自定义Seekbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆