Align tick marks to an Android SeekBar

岁酱吖の 提交于 2019-12-10 18:22:42

问题


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.

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


回答1:


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.

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);

        }


来源:https://stackoverflow.com/questions/30462605/align-tick-marks-to-an-android-seekbar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!