0

我正在尝试通过覆盖 onDraw() 方法来自定义搜索栏的外观 - 我需要在屏幕上找到当前进度/辅助进度位置。

我知道如何获得当前进度/次要进度,getProgress()但这getSecondaryProgress()并不能反映屏幕上的位置。我想我可以得到可绘制的进度并获得可绘制的边界,或者用于进度的clipDrawable的级别,如下所示:

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
        }

但我得到的值也不反映屏幕上的位置!有谁知道我怎样才能得到这些值?

4

1 回答 1

0

我通过将最大进度设置为屏幕宽度来解决这个问题:

    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);
于 2012-06-12T11:43:54.820 回答