9

画面の背景色に応じて色が変更されるため、プログレスバーのprimaryProgress、secondaryProgressにプログラムで色を設定したいと思います。

コード:

LayerDrawable progressDrawable = (LayerDrawable) ProgressBar1.getProgressDrawable();
Drawable backgroundColor = progressDrawable.getDrawable(0);
Drawable secondaryColor = progressDrawable.getDrawable(1);
Drawable primaryColor = progressDrawable.getDrawable(2);

final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
primaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
secondaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));

primaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null);
secondaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null);

progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));

...

編集:

** ここのカラー コードはテスト用です。その後、カラーコードは更新のために他の部分に参照されます。

    secondaryColor.setColorFilter((Color.rgb(255, 0, 0)), PorterDuff.Mode.SRC_OVER);
    primaryColor.setColorFilter((Color.rgb(0, 255, 213)), PorterDuff.Mode.SRC_OVER);        

    progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));
    progressDrawable.setDrawableByLayerId(progressDrawable.getId(1), new ClipDrawable(secondaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));
    ProgressBar1.setProgressDrawable(progressDrawable); 
    ProgressBar1.setProgress(progress);
    ProgressBar1.setSecondaryProgress(secondaryProgress);

質問:

に赤で下線を付け、primanyColor.setColorを報告しThe method setColor(int, null) is undefined for the type Drawableます。

上記のコードを変更して機能させるにはどうすればよいですか? ありがとう!

4

3 に答える 3

3

できることは、レイヤーリストを最初に xml を介して描画可能にすることです(最初のレイヤーとして背景、2 番目のレイヤーとして二次的な進捗を表す描画可能、最後のレイヤーとして主要な進捗を表す別の描画可能を意味します)、次に変更します。次のようにして、コードの色を変更します。

public void setPrimaryProgressColor(int colorInstance) {
      if (progressBar.getProgressDrawable() instanceof LayerDrawable) {
        Log.d(mLogTag, "Drawable is a layer drawable");
        LayerDrawable layered = (LayerDrawable) progressBar.getProgressDrawable();
        Drawable circleDrawableExample = layered.getDrawable(<whichever is your index of android.R.id.progress>);
        circleDrawableExample.setColorFilter(colorInstance, PorterDuff.Mode.SRC_IN);
        progressBar.setProgressDrawable(layered);
    } else {
        Log.d(mLogTag, "Fallback in case it's not a LayerDrawable");
        progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
    }
}

この方法は、元のドローアブルの測定値を xml で宣言し、後でその構造を変更することなく、特に xml ファイルの画面フォルダーを特定する必要がある場合に、最高の柔軟性を提供します。コード。新しい ShapeDrawable を最初から再インスタンス化する必要はありません。

于 2016-06-30T14:50:50.303 に答える
2

Drawable の色を設定するには、 Drawable.setColorFilter を使用します。お気に入り:

primaryColor.setColorFilter((Color.rgb(0,128,0)), 
                                                 PorterDuff.Mode.SRC_OVER);        
于 2015-07-03T07:13:50.273 に答える