3

シークバーの進行状況バーに個々の色付きの垂直線を動的に追加するにはどうすればよいですか?

私はすでに、個々のドローアブルとして作成された、progress.xml、progress_fill.xml、および background_fill ファイルを持っているので、シークバーをある程度カスタマイズできます。ただし、個々の垂直線は、状況に応じて、さまざまな色でシークバーの任意の位置に描画する必要がある場合があります。XML レイアウト ファイルでは設定できません。

プログラムで background_fill.xml ドローアブルに小さな色付きの四角形を書き込む必要があると思います (私の progress_fill はほとんど透明に設定されています)。

これらの小さな四角形をプログラムで background_fill.xml ドローアブルに書き込むにはどうすればよいですか?

SpannableStringBuilder と ImageSpan を使用して小さな四角形を TextView に書き込むという点で、TextView で同様のことを行いました。しかし、それがシークバー ウィジェットで利用できるとは思えません。

4

1 に答える 1

2

SeekBarウィジェットを拡張することでこれを行いました。2つのステップ:最初に、CustomSeekBarという新しいクラスを作成してSeekBarを拡張します。

package com.example.seekbar;

class CustomSeekBar extends SeekBar {

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

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

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

protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    private Paint paintRect = new Paint();
    paintRect.setColor(Color.rgb(142, 196, 0));

    private Rect audioRect = new Rect();
    audioRect.set(5, 7, 4, 18);

    canvas.drawRect(audioRect, paintRect);
}
}

次に、レイアウトxmlファイルでこのCustomSeekBarを次のように参照します。

    <view class="com.example.seekbar.CustomSeekBar"
    android:id="@+id/seekBar0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp" />
于 2012-08-17T14:55:35.123 に答える