将刻度线与 Android SeekBar 对齐 [英] Align tick marks to an Android SeekBar

查看:90
本文介绍了将刻度线与 Android SeekBar 对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 上的 SeekBar 中添加一些刻度线指示器.我能够生成带有刻度线的背景就好了,但是我想不出一种方法来将刻度线与实际的 SeekBar 线对齐.从屏幕截图中可以看出,刻度线实际上在 SeekBar 行之前开始.

I'm attempting to put some tick mark indicators to a SeekBar on Android. I am able to generate a background with the tick marks just fine, however I can't figure out a way to align the tick marks with the actual SeekBar line. As you can see from the screenshot, the tick marks actually start before the SeekBar line does.

我可以通过反复试验找到距离,但我怀疑它是否会在每台设备上保持不变.我还注意到线条和背景边缘之间的空间与拇指滑块的半径相同.但我找不到确定半径是多少的方法,而且我不确定它是否适用于所有设备.

I could find the distance by trial and error, but I doubt it's going to remain the same on every device. I also noticed the space between the line and the edge of the background is the same as the radius of the thumb slider. But I can't find a way to determine what that radius is, and again, am not sure it'll work on every device.

我知道有可用的自定义搜索栏,但我试图避免添加第 3 方依赖项.

I know there are custom seekbars available, but I'm trying to avoid adding a 3rd party dependency.

推荐答案

这使用 getThumb().getMinimumWidth() 来近似到 SeekBar 行开头的偏移量.它似乎有效,但不确定是否适用于所有设备.

This uses getThumb().getMinimumWidth() to approximate the offset to the start of the SeekBar's line. It seems to work, but not sure it will on every device.

更新:此代码适用于默认情况,但如果您更改填充,则会中断.最好使用 getPaddingLeft() 和 getPaddingRight() 来确定滑块的长度和位置.这些函数通常会返回 getThumb().getMinimumWidth()/2,除非填充或缩略图已更改.getPaddlingLeft() 和 getPaddingRight() 将始终有效.

UPDATE: This code does work for the default case, but will break if you change the padding. It is better to use getPaddingLeft() and getPaddingRight() to determine the length and position of the slider. These functions will usually return getThumb().getMinimumWidth()/2, unless the padding or thumbnail has been changed. getPaddlingLeft() and getPaddingRight() will always work.

        Bitmap b= Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);

        Canvas c=new Canvas(b);
        Paint p=new Paint();
        p.setColor(Color.WHITE);

        int offset=this.getThumb().getMinimumWidth();
        float interval=(float)((width-offset*1.0)/24);
        for(int i=0;i<25;i++){
            float x=i*interval+offset/2;
            c.drawLine(x,0,x,height,p);
            c.drawText(Integer.toString(i),x,height,p);

        }

这篇关于将刻度线与 Android SeekBar 对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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