Get onscreen position of current seekbar progress/secondary progress?

淺唱寂寞╮ 提交于 2019-12-13 05:26:46

问题


I'm trying to customize the look of a seekbar by overriding the onDraw() method - I need to find the current progress/secondary progress position on screen.

I know how to get the current progress/secondary progress with getProgress() and getSecondaryProgress() but this does not reflect the position onscreen. I thought I could get the progress drawable and get the bounds of the drawable, or the level of the clipDrawable used for the progress like so:

Drawable progress = getProgressDrawable();
        if (progress instanceof LayerDrawable)
        {
            Drawable primaryProgress = ((LayerDrawable)progress).findDrawableByLayerId(android.R.id.progress);
            Rect bounds= primaryProgress.copyBounds();//bounds.left is 0, bounds.right is the screen width
            ((ClipDrawable)primaryProgress).getLevel();//just gives a value between 0 and 10,000
        }

but the values I get do no reflect the onscreen position either! Does anyone know how I can get these values?


回答1:


I got around this by setting the max progress to the width of the screen:

    WindowManager wm = (WindowManager) myAppContext.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    try{
        display.getSize(size);//only works on later versions of android
    }
    catch (Exception e) {

        int width = display.getWidth();  // deprecated
        int height = display.getHeight();  // deprecated
        size = new Point(width, height);
    }
myProgressBar.setMax(size.x);


来源:https://stackoverflow.com/questions/10451838/get-onscreen-position-of-current-seekbar-progress-secondary-progress

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