2013-07-01 3 views

答えて

10

私は同じ問題に直面しました。 Commonswareが正しい方向を指してくれてありがとう。私は解決策を得るためにcode.google.com/p/range-seek-barに触発されたクラスを書いた。すべての言った上記のよう

https://github.com/vashisthg/StartPointSeekBar

+0

これ!非常に素晴らしい!私は親指を少し大きく見えるが、それはもちろん調節可能で、また、左右に色を変えるのも非常に簡単だった。 setNormalizedValueの設定に問題がある場合のみ、-1に設定すると左端からすべて開始され、1に設定すると右端から開始されます。中心に設定することはできません – CularBytes

+1

解:0と1との間の値。ここで0.5が中心です。 – CularBytes

+1

このカスタムシークバーを垂直に設定することはできますか? – filipst

2

途中で起動するには、あなたはあなたの範囲全体の半分の値にsetProgress()を呼び出すことができます。

SeekBarを途中で開始して終了するには(つまり、青い線が中央から左に、または中央から右に移動する)、SeekBarの代わりに自分自身を作成するか、 SeekBar自体ではサポートされていないため、これを行った他の人

+0

'自身の交換あなたは 'スタイル 'をメーン? – lysenkobv

+0

@lysenkobv:いいえ、Javaクラスを意味します。 'SeekBar'は継承元の' ProgressBar'と同様に、バーの開始点を左端に置くように設計されています。それがスタイルによって変わることができるなら、私はむしろ驚くだろう。これを処理する独自の 'View'を作成する必要があると思います。例えば、 'RangeSeekBar'(https://code.google.com/p/range-seek-bar/)は' ImageView'のサブクラスとして実装されていました。 – CommonsWare

4

customSeekBar

、それは私が私たちの独自のカスタムシークバーの実装を実現することができます。

私は次のようにして試してみました。

SeekBaためactivity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" 
    android:layout_margin="20dp"> 

<com.example.customseekbar.CustomSeekBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"   
    android:max="100" 
    android:progress="50" 
    android:progressDrawable="@android:color/transparent" 
    android:id="@+id/customSeekBar" 
/> 

</RelativeLayout> 

MainActivity.java

package com.example.customseekbar; 

import com.example.suricustomseekbar.R; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.SeekBar; 

public class MainActivity extends Activity { 

SeekBar mseekBar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mseekBar = (SeekBar) findViewById(R.id.customSeekBar); 
    mseekBar.setProgress(50); 
    } 
    } 

CustomSeekBar.java

package com.example.customseekbar; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.widget.SeekBar; 

    public class CustomSeekBar extends SeekBar { 

private Rect rect; 
private Paint paint ; 
private int seekbar_height; 

public CustomSeekBar(Context context) { 
    super(context); 

} 

public CustomSeekBar(Context context, AttributeSet attrs) { 

    super(context, attrs); 
    rect = new Rect(); 
    paint = new Paint(); 
    seekbar_height = 6; 
} 

public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected synchronized void onDraw(Canvas canvas) { 

    rect.set(0 + getThumbOffset(), 
      (getHeight()/2) - (seekbar_height/2), 
      getWidth()- getThumbOffset(), 
      (getHeight()/2) + (seekbar_height/2)); 

    paint.setColor(Color.GRAY); 

    canvas.drawRect(rect, paint); 



    if (this.getProgress() > 50) { 


     rect.set(getWidth()/2, 
       (getHeight()/2) - (seekbar_height/2), 
       getWidth()/2 + (getWidth()/100) * (getProgress() - 50), 
       getHeight()/2 + (seekbar_height/2)); 

     paint.setColor(Color.CYAN); 
     canvas.drawRect(rect, paint); 

    } 

    if (this.getProgress() < 50) { 

     rect.set(getWidth()/2 - ((getWidth()/100) * (50 - getProgress())), 
       (getHeight()/2) - (seekbar_height/2), 
       getWidth()/2, 
       getHeight()/2 + (seekbar_height/2)); 

     paint.setColor(Color.CYAN); 
     canvas.drawRect(rect, paint); 

    } 

    super.onDraw(canvas); 
    } 
} 
関連する問題