Skip to content

Commit

Permalink
Additional color
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy671 committed Dec 16, 2017
1 parent ceba34c commit 4a88286
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public class PianoChartView extends View {

private List<Integer> blackKeys;
private List<Integer> checkedKeys;
private List<Integer> additionalCheckedKeys;

private List<String> keyLetters;

private int lightColor, darkColor, checkedColor;
private int lightColor, darkColor, checkedColor, additionalCheckedColor;

public enum Size{
Small, Large
Expand All @@ -65,8 +66,7 @@ public PianoChartView(Context context, AttributeSet attrs) {
setLightKeysColor(attributes.getColor( R.styleable.PianoChartView_lightKeysColor, DEFAULT_LIGHT_KEYS_COLOR));
setDarkKeysColor(attributes.getColor( R.styleable.PianoChartView_darkKeysColor, DEFAULT_DARK_KEYS_COLOR));
setCheckedKeysColor(attributes.getColor(R.styleable.PianoChartView_checkedKeysColor, DEFAULT_CHECKED_KEYS_COLOR));
setNamesOfKeys("C", "", "D", "", "E", "F","","G","G#/Ab","A","A#/Bb","B","C",
"C#/Db", "D", "D#/Eb", "E", "F","F#/Gb","G","G#/Ab","A","A#/Bb","B");
setAdditionalCheckedKeysColor(attributes.getColor(R.styleable.PianoChartView_additionalCheckedKeysColor, DEFAULT_CHECKED_KEYS_COLOR));

setSize(Size.values()[attributes.getInt(R.styleable.PianoChartView_size, DEFAULT_SIZE)]);

Expand All @@ -75,6 +75,11 @@ public PianoChartView(Context context, AttributeSet attrs) {
setCheckedKeys(getResources().getIntArray(checkedKeysArray));
}

int additionalCheckedKeysArray = attributes.getResourceId(R.styleable.PianoChartView_additionalCheckedKeys, 0);
if (additionalCheckedKeysArray != 0) {
setAdditionalCheckedKeys(getResources().getIntArray(additionalCheckedKeysArray));
}

int namesOfKeysArray = attributes.getResourceId(R.styleable.PianoChartView_namesOfKeys, 0);
if(namesOfKeysArray != 0){
setNamesOfKeys(getResources().getStringArray(namesOfKeysArray));
Expand All @@ -88,6 +93,7 @@ private void initView() {
blackKeys = intsToList(DARK_KEYS);

checkedKeys = new ArrayList<>();
additionalCheckedKeys = new ArrayList<>();
keyLetters = new ArrayList<>();

densityScale = getResources().getDisplayMetrics().density;
Expand Down Expand Up @@ -117,6 +123,13 @@ public void setCheckedKeys(int[] numbers) {
invalidate();
}

public void setAdditionalCheckedKeys(int[] numbers){
additionalCheckedKeys = intsToList(numbers);

requestLayout();
invalidate();
}

public void setNamesOfKeys(String... keyLetters){
this.keyLetters = Arrays.asList(keyLetters);

Expand Down Expand Up @@ -153,10 +166,19 @@ public void setCheckedKeysColor(int color) {
invalidate();
}

public void setAdditionalCheckedKeysColor(int color){
additionalCheckedColor = color;

requestLayout();
invalidate();
}

public int[] getCheckedKeys(){
return listToInts(checkedKeys);
}

public int[] getAdditionalCheckedKeys(){return listToInts(additionalCheckedKeys);}

public Size getSize(){
return size;
}
Expand All @@ -173,6 +195,8 @@ public int getCheckedKeysColor(){
return checkedColor;
}

public int getAdditionalCheckedKeysColor(){return additionalCheckedColor; }

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Expand All @@ -199,6 +223,8 @@ protected void onDraw(Canvas canvas) {
continue;
if(checkedKeys.contains(i)){
pianoFillPaint.setColor(checkedColor);
}else if (additionalCheckedKeys.contains(i)) {
pianoFillPaint.setColor(additionalCheckedColor);
}else{
pianoFillPaint.setColor(lightColor);
}
Expand Down Expand Up @@ -227,6 +253,8 @@ protected void onDraw(Canvas canvas) {
}
if(checkedKeys.contains(i)){
pianoFillPaint.setColor(checkedColor);
}else if (additionalCheckedKeys.contains(i)) {
pianoFillPaint.setColor(additionalCheckedColor);
}else{
pianoFillPaint.setColor(darkColor);
}
Expand Down
5 changes: 5 additions & 0 deletions pianochartview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
<declare-styleable name="PianoChartView">
<attr name="lightKeysColor" format="color" />
<attr name="darkKeysColor" format="color" />

<attr name="checkedKeysColor" format="color" />
<attr name="checkedKeys" format="reference"/>

<attr name="additionalCheckedKeysColor" format="color" />
<attr name="additionalCheckedKeys" format="reference"/>

<attr name="namesOfKeys" format="reference"/>
<attr name="size" format="enum">
<enum name="Small" value="0"/>
Expand Down

0 comments on commit 4a88286

Please sign in to comment.