How do I find the Y position directly above a SeekBar's thumb?
I am currently trying to make a floating text appear above a SeekBar and have resolved finding the exact X position above the SeekBar's thumb like so:
double percent = seekBar.getProgress() / (double) seekBar.getMax();
int offset = seekBar.getThumbOffset();
int seekWidth = seekBar.getWidth();
int val = (int) Math.round(percent * (seekWidth - 2 * offset));
int labelWidth = seekBarText.getWidth();
float textX = offset + seekBar.getX() + val
- Math.round(percent * offset)
- Math.round(percent * labelWidth / 2);
Now I am having trouble finding the exact Y position above the SeekBar's thumb. I am currently trying to do the following:
textY = seekBar.getY() - seekBar.getThumbOffset();
seekBarTextView.setX(textX);
seekBarTextView.setY(textY);
However, this results in the SeekBar being several hundred pixels above the thumb, as seen in the screenshot (the SeekBar in question is the green one, the TextView being set is in the upper right corner with number "10"):
Update:
I ended up resolving this issue in a little bit of a hacky way. I utilized the following method to retrieve the Y value above the SeekBar:
int [] xyArray = new int[2];
seekBar.getLocationOnScreen(xyArray);
float textY = xyArray[1] - DPtoPixel((int) 118.75);
getLocationOnscreen() returns an incorrect X value that is offset by exactly 118.75dp across all screen sizes for some reason (I don't know why). I simply subtracted the value from the given Y value and was able to find the right location.
Update:
I ended up resolving this issue in a little bit of a hacky way. I utilized the following method to retrieve the Y value above the SeekBar:
int [] xyArray = new int[2];
seekBar.getLocationOnScreen(xyArray);
float textY = xyArray[1] - DPtoPixel((int) 118.75);
getLocationOnscreen() returns an incorrect X value that is offset by exactly 118.75dp across all screen sizes for some reason (I don't know why). I simply subtracted the value from the given Y value and was able to find the right location.
链接地址: http://www.djcxy.com/p/64344.html上一篇: 从本地播放HTML5视频