问题
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