2017-02-22 83 views
0

我目前正在做一個浮動的文字出現在搜索欄上方,並解決發現的搜索欄的拇指上面的確切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值中減去了該值,並找到了正確的位置。

回答

0

更新:

我結束了在一個哈克的方式一點點解決這個問題。我使用下面的方法來檢索搜索欄上方的Y值:

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

getLocationOnscreen()返回由恰好118.75dp在所有屏幕尺寸出於某種原因偏移(我不知道爲什麼一個不正確的X值)。我簡單地從給定的Y值中減去了該值,並找到了正確的位置。