如何在SeekBar的拇指正上方找到Y位置?

问题描述:

我目前正在做一个浮动的文字出现在搜索栏上方,并解决发现的搜索栏的拇指上面的确切X位置,像这样:如何在SeekBar的拇指正上方找到Y位置?

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); 

现在我无法找到上面的搜索栏的确切的Y位置拇指。目前我正在努力做到以下几点:

textY = seekBar.getY() - seekBar.getThumbOffset(); 
seekBarTextView.setX(textX); 
seekBarTextView.setY(textY); 

但是,这导致在搜索栏是几百个像素拇指上面,如截图看到(有问题的搜索栏是绿色的,TextView的设定与数字“10”的右上角):

enter image description here

更新:

我结束了在的一点点解决这个问题哈克的方式。我使用下面的方法来检索搜索栏上方的Y值:

int [] xyArray = new int[2]; 
     seekBar.getLocationOnScreen(xyArray); 
     float textY = xyArray[1] - DPtoPixel((int) 118.75); 

getLocationOnscreen()返回由恰好118.75dp在所有屏幕尺寸出于某种原因偏移(我不知道为什么一个不正确的X值)。我简单地从给定的Y值中减去了该值,并找到了正确的位置。

更新:

我结束了在一个哈克的方式一点点解决这个问题。我使用下面的方法来检索搜索栏上方的Y值:

int [] xyArray = new int[2]; 
     seekBar.getLocationOnScreen(xyArray); 
     float textY = xyArray[1] - DPtoPixel((int) 118.75); 

getLocationOnscreen()返回由恰好118.75dp在所有屏幕尺寸出于某种原因偏移(我不知道为什么一个不正确的X值)。我简单地从给定的Y值中减去了该值,并找到了正确的位置。