1

実行時にプライマリとセカンダリの進行状況で SeekBar を作成する必要があります。LayerDrawableをどのように扱うべきかについてtileify 関数を見て、適切な ID についてはprogress_horizo ​​ntal_holo_dark.xml を見て、私は次のことを思いつきました:

    // parent view
    LinearLayout parent = (LinearLayout) findViewById(R.id.parent);

    // create widget
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    SeekBar seekBar = new SeekBar(this);
    seekBar.setLayoutParams(params);

    // set bg resources
    Drawable background = getResources().getDrawable(R.drawable.scrubber_track_holo_light);
    ScaleDrawable primary = new ScaleDrawable(getResources().getDrawable(R.drawable.scrubber_primary_holo), 0x10|0x03, 100.0f, 100);

    ScaleDrawable secondary = new ScaleDrawable(getResources().getDrawable(R.drawable.scrubber_secondary_holo), 0x100x03, 100.0f, 85);

    LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { background, secondary, primary});
    layerDrawable.setId(0, android.R.id.background);
    layerDrawable.setId(1, android.R.id.secondaryProgress);
    layerDrawable.setId(2, android.R.id.progress);

    seekBar.setBackgroundDrawable(layerDrawable);
    seekBar.setThumb(getResources().getDrawable(R.drawable.seekbar_btn));

    // set values
    seekBar.setMax(50);
    seekBar.setSecondaryProgress(25);
    seekBar.setProgress(15);
    parent.addView(seekBar);  

SeekBar は次のようになります。

理想

そして、これは私のリソースファイルです:

主要な-scrubber_primary_holo.9.png
二次-scrubber_secondary_holo.9.png

問題:

  • setProgressDrawable(layerDrawable) が期待どおりに機能しません。私はこの関数を XML コードで使用していますが、実行時に使用すると、下の図に示すように resultult が得られます。

ここに画像の説明を入力

何か案は?

4

1 に答える 1

2

他の誰かが同じ問題を抱えている場合に備えて、解決策を見つけました。2 つの問題がありました。1 つ目は、XML で使用されている ScaleBitmap がコードからは機能しないことです。ClipDrawable を使用する必要がありました。2 つ目の問題は、1 次および 2 次の進行状況の表示に関するもので、最初の進行状況の変更後に表示されていました。これは、最初に seekBar を親ビューに追加してから、プライマリとセカンダリの進行状況を設定することで解決されます。以下のソースコードを見ることができます。乾杯!

    // CREATE WIDGET
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    SeekBar seekBar = new SeekBar(this, null, android.R.attr.seekBarStyle);
    seekBar.setLayoutParams(params);
    parent.addView(seekBar);

    seekBar.setMax(50);
    seekBar.setProgress(15);
    seekBar.setSecondaryProgress(25);


    // CREATE PROGRESS BAR
    LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
    seekBar.setThumb(getResources().getDrawable(R.drawable.seekbar_btn));

    Drawable progress = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.progress);
    Bitmap progressBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_secondary_holo);
    progress = new ClipDrawable(new BitmapDrawable(getResources(), progressBmp), Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);
    layerDrawable.setDrawableByLayerId(android.R.id.progress, progress);

    Drawable secondary = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
    Bitmap secondaryBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_primary_holo);
    secondary = new ClipDrawable(new BitmapDrawable(getResources(), secondaryBmp), Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);
    layerDrawable.setDrawableByLayerId(android.R.id.secondaryProgress, secondary);

    Drawable background = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.background);
    Bitmap backgroundBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_track_holo_light);
    background = new BitmapDrawable(getResources(), backgroundBmp);
    layerDrawable.setDrawableByLayerId(android.R.id.background, background);
于 2014-07-24T15:55:22.200 に答える