Android:ColorPicker仅显示一种颜色,并从Pallete中选择错误的颜色

时间:2016-04-11 13:53:36

标签: java android colors color-picker

我正在开展一个项目,我需要一个颜色选择器,具有特定的颜色。由于我的要求更简单,我不想要任何想象力,我最终得到了来自Github的this。实现很简单,虽然我只能看到一种颜色。我有我想要在colors.xml中指定的颜色,所以我添加了它们,但没有帮助,它对于整个条形只是紫色。

屏幕截图:

enter image description here

XML:

   <uz.shift.colorpicker.LineColorPicker
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/colorPicker"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:orientation="horizontal"/>

代码:

public class EditNoteActivity extends Activity {

    LineColorPicker colorPicker;

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_note);
 colorPicker = (LineColorPicker)findViewById(R.id.colorPicker);

        colorPicker.setColors(new int[] {R.color.noteAqua,R.color.noteBlue,R.color.noteDarkBlue,R.color.noteDeepBlue,
                R.color.noteDeepRed,R.color.noteGreen,R.color.noteGrey,
                R.color.noteOrange,R.color.notePink,R.color.notePurple,R.color.noteRed,
                R.color.noteYellow});
  colorPicker.setOnColorChangedListener(new OnColorChangedListener() {
            @Override
            public void onColorChanged(int c) {
                Log.d("Selected color ",Integer.toHexString(c));
            }
        });
}
private void setColorForPicker(String noteColor){
    if (noteColor.equals("#1abc9c")) {
        colorPicker.setSelectedColor(R.color.noteAqua);

    }
    if (noteColor.equals("#3498db")) {
        colorPicker.setSelectedColor(R.color.noteBlue);
    }
    if (noteColor.equals("#34495e")) {
        colorPicker.setSelectedColor(R.color.noteDarkBlue);
    }
    if (noteColor.equals("#0071c1")) {
        colorPicker.setSelectedColor(R.color.noteDeepBlue);
    }
    if (noteColor.equals("#c00000")) {
        colorPicker.setSelectedColor(R.color.noteDeepRed);
    }
    if (noteColor.equals("#2ecc71")) {
        colorPicker.setSelectedColor(R.color.noteGreen);

    }
    if (noteColor.equals("#95a5a6")) {
        colorPicker.setSelectedColor(R.color.noteGrey);
       }
    if (noteColor.equals("#e67e22")) {
        colorPicker.setSelectedColor(R.color.noteOrange);
    }
    if (noteColor.equals("#ff56bb")) {
        colorPicker.setSelectedColor(R.color.notePink);
    }
    if (noteColor.equals("#9b59b6")) {
        colorPicker.setSelectedColor(R.color.notePurple);
    }

    if (noteColor.equals("#e74c3c")) {
        colorPicker.setSelectedColor(R.color.noteRed);
    }
    if (noteColor.equals("#f1c40f")) {
        colorPicker.setSelectedColor(R.color.noteYellow);
    }
}

color.xml文件:

  <color name="noteAqua">#1abc9c</color>
    <color name="noteBlue">#3498db</color>
    <color name="noteDarkBlue">#34495e</color>
    <color name="noteDeepBlue">#0071c1</color>
    <color name="noteDeepRed">#c00000</color>
    <color name="noteGreen">#2ecc71</color>
    <color name="noteGrey">#95a5a6</color>
    <color name="noteOrange">#e67e22</color>
    <color name="notePink">#ff56bb</color>
    <color name="notePurple">#9b59b6</color>
    <color name="noteRed">#e74c3c</color>
    <color name="noteYellow">#f1c40f</color>

那么,我做错了什么?谢谢。

1 个答案:

答案 0 :(得分:1)

您正在将颜色标识符传递给数组而不是值本身,您需要使用以下命令从资源加载值:

0:00:00.064480642  4059      0x12c66d0 WARN    codecparsers_mpegvideo gstmpegvideoparser.c:887:gst_mpeg_video_packet_parse_picture_header: Unsupported picture type : 0

或者您可以使用常量而不是使用颜色资源来定义数组:

int noteAqua = getResources().getColor(R.color.noteAqua);
colorPicker.setColors(new int[] {noteAqua});