2016-07-25 77 views
0

我有Android的默認SeekBar有3個可能的值:0,1,2(左,中,右)。Android SeekBar奇怪的行爲

但是,只有當手指剛好位於該點(對應於該值)或右側時纔會有一定的價值。如果手指距離該點左側1個像素,則seekbar取前一個值。

我想要它取值時,手指是附近點。 我的解釋技巧很差,所以看圖片: drawing

回答

1

如果使用3分是不夠的,爲什麼不添加更多的點和映射回你的3?例如

private static final double RATIOS = new double[]{ .33, 66, 100 } 
private static final int FAKED_POINTS = new int[]{ 0, 50, 100 } 

private void setupSeekBar() { 
    // Your normal setup 
    seekBar.setMax(100); 
} 

@Override 
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
    if(!fromUser) // We only want to do this when the user changes the values 
     return; 

    for(int i = 0; i < RATIOS.length; i++){ 
     if(progress <= RATIOS[i]){ 
      onRealProgressChanged(i); 

      seekBar.setProgress(FAKED_POINTS[i]); // This way it always stays on our 3 faked points 
     } 
    } 
} 

private void onRealProgressChanged(int realProgress){ 
    // Deal with the real changed value (being 0, 1, or 2) 
} 

如果您需要更多的微調,可以縮放,高達另一個量級。

不是很乾淨或漂亮,但應指向正確的方向。

0

我工作在一個自定義的Seekbar視圖庫,可以幫助你。

請使用它,如果你喜歡它,分享它!

此行添加到您的gradle這個文件的應用模塊

compile 'mx.segundamano.doubleseekbarview:seekbars:1.0.2' 

並添加到您的build.gradle項目級別文件

repositories { 
    maven { 
     url "http://dl.bintray.com/segundamanomx/maven" 
    } 
} 

裏面有兩個自定義視圖,一個一個選擇另一個有兩個選擇器。

享受它;)

+0

錯誤:(25,13)無法解析:mx.segundamano.doubleseekbarview:seekbars:1.0.1 請您上傳至GitHub上? – Egor

+0

@Egor,當然,這是github項目https://github.com/SegundamanoMX/Seekbars –