我目前正在尝试使浮动文本出现在 SeekBar 上方,并已解决在 SeekBar 的拇指上方找到确切的 X 位置,如下所示:
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);
现在我很难找到 SeekBar 拇指上方的确切 Y 位置。我目前正在尝试执行以下操作:
textY = seekBar.getY() - seekBar.getThumbOffset();
seekBarTextView.setX(textX);
seekBarTextView.setY(textY);
但是,这会导致 SeekBar 在拇指上方数百像素,如屏幕截图所示(有问题的 SeekBar 是绿色的,设置的 TextView 位于右上角,数字为“10”):
更新:
我最终以一种 hacky 的方式解决了这个问题。我使用以下方法来检索 SeekBar 上方的 Y 值:
int [] xyArray = new int[2];
seekBar.getLocationOnScreen(xyArray);
float textY = xyArray[1] - DPtoPixel((int) 118.75);
getLocationOnscreen() 返回一个不正确的 X 值,由于某种原因(我不知道为什么),该值在所有屏幕尺寸上正好偏移 118.75dp。我只是从给定的 Y 值中减去该值并能够找到正确的位置。