Skip to content

Commit

Permalink
custom view now accepts any Number.class extension (byte, short, int …
Browse files Browse the repository at this point in the history
…long etc.)
  • Loading branch information
maxViolet committed Apr 18, 2019
1 parent 776e9dc commit 954e8f4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 57 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ An easy way to visualize in proportions any of your data.
### Configuring your project dependencies
```groovy
dependencies {
implementation 'com.github.maxViolet:ProportionsBarLibrary:0.1.0'
implementation 'com.github.maxViolet:ProportionsBarLibrary:0.1.1'
}
```

### Creating the view
### Creating the view via code
```java
ProportionsBar proportionsBar = new ProportionsBar(context);
proportionsBar.setId(R.id.proportions_bar_1);
```

### Adding the view
### Adding the view to container view (LinearLayout in this case)
```java
LinearLayout container = findViewById(R.id.container_pb);
container.addView(proportionsBar);
```

### Adding single values
### Adding values (accepts any Number.class)
```java
proportionsBar.addValues(11, 30, 24, 12, 41, 27);
proportionsBar.addValues(11, 30.0, 24.21, -12.1, 41, 27.999);
```

### Adding colors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public class ProportionsBar extends View {
//minimal segment value to be shown in % of the bar width (meaning: values between >0% and <2% will be shown as 2% section)
private int minimalSegmentValue = 2;
//list of data values
private ArrayList<Integer> valueList = new ArrayList<>();
private ArrayList<Double> valueDoubleList = new ArrayList<>();
// private ArrayList<Double> valueDoubleList = new ArrayList<>();
// private ArrayList<Float> valueFloatList = new ArrayList<>();
//height of custom view in % of parent view
private double scaleH = 100;

Expand All @@ -42,12 +44,15 @@ public class ProportionsBar extends View {
//animation duration
private int animationDuration = 1000;

public List<Float> percentValueList = new ArrayList<>();
//proportion values list
public List<Float> proportionValueList = new ArrayList<>();

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private List<Integer> intColors = new ArrayList<>();
private List<String> stringColors = new ArrayList<>();

private Queue<Integer> colorQueue = new ArrayDeque<>();
ArrayList<String> defaultColors = new ArrayList<>(Arrays.asList("#81DAF5", "#008db9", "#1c0a63"));
private final ArrayList<String> defaultColors = new ArrayList<>(Arrays.asList("#81DAF5", "#008db9", "#1c0a63"));

public ProportionsBar showRoundedCorners(boolean show) {
this.showRoundedCorners = show;
Expand All @@ -73,6 +78,7 @@ public ProportionsBar gapColor(int gapColor) {
this.gapColor = gapColor;
return this;
}

public ProportionsBar gapColor(String gapColor) {
this.gapColor = Color.parseColor(gapColor);
return this;
Expand All @@ -93,9 +99,10 @@ public ProportionsBar animationDuration(int duration) {
return this;
}

public ProportionsBar addValues(int... values) {
for (int iterator : values) {
if (checkValue(iterator)) this.valueList.add(iterator);
public ProportionsBar addValues(Number... values) {
for (Number iterator : values) {
if (iterator != null)
this.valueDoubleList.add(Math.abs(Double.valueOf(String.valueOf(iterator))));
}
return this;
}
Expand Down Expand Up @@ -127,6 +134,22 @@ public ProportionsBar(Context context, @Nullable AttributeSet attrs, int defStyl
super(context, attrs, defStyleAttr);
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
ArrayList<Float> tempDouble = new ArrayList<>();
//instantiate proportions list
if (valueDoubleList != null) tempDouble = getProportionValues(valueDoubleList);
//fill percent list used for segment drawing
if (valueDoubleList != null) {
for (int i = 0; i < valueDoubleList.size(); i++)
proportionValueList.add(tempDouble.get(i));
}
//check for the first view launch animation
if (animated && firstLaunch) playAnimation();
firstLaunch = false;
}

@Override
protected void onDraw(Canvas canvas) {
initColorQueue();
Expand All @@ -144,13 +167,13 @@ protected void onDraw(Canvas canvas) {
if (!showRoundedCorners) {
wSmall = wBig;
} else {
wSmall = (float) (getWidth() - 2 * r - gapSize * (valueList.size() - 1));
wSmall = (float) (getWidth() - 2 * r - gapSize * (valueDoubleList.size() - 1));
}
//size of gaps (depends from container view width and denominator)
float gapSize = (float) (wBig * this.gapSize / 100);

//draw SEGMENTS based in the percent values proportions
for (int k = 0; k < percentValueList.size(); k++) {
for (int k = 0; k < proportionValueList.size(); k++) {
if (k == 0) {
//FIRST segment
paint.setColor(getColorFromQueue());
Expand All @@ -160,11 +183,10 @@ protected void onDraw(Canvas canvas) {
tempX = r;
}
//draw first rectangle
drawRectangle(canvas, tempX - 1, tempX + (wSmall * percentValueList.get(k) / 100) - gapSize / 2, h);
tempX += (wSmall * percentValueList.get(k) / 100) - gapSize / 2;
// }
drawRectangle(canvas, tempX - 1, tempX + (wSmall * proportionValueList.get(k) / 100) - gapSize / 2, h);
tempX += (wSmall * proportionValueList.get(k) / 100) - gapSize / 2;

} else if (k == percentValueList.size() - 1) {
} else if (k == proportionValueList.size() - 1) {
//LAST SEGMENT
//draw gap
if (showGaps) {
Expand All @@ -183,7 +205,6 @@ protected void onDraw(Canvas canvas) {
drawRectangle(canvas, tempX, wSmall, h);
tempX = 0;
}

} else {
//MID segments
//draw gap
Expand All @@ -193,8 +214,8 @@ protected void onDraw(Canvas canvas) {
}
//draw mid rectangle
paint.setColor(getColorFromQueue());
drawRectangle(canvas, tempX, tempX + (wSmall * percentValueList.get(k) / 100) - gapSize / 2, h);
tempX += (wSmall * percentValueList.get(k) / 100) - gapSize / 2;
drawRectangle(canvas, tempX, tempX + (wSmall * proportionValueList.get(k) / 100) - gapSize / 2, h);
tempX += (wSmall * proportionValueList.get(k) / 100) - gapSize / 2;
}
}
}
Expand All @@ -213,20 +234,18 @@ private void drawGap(Canvas canvas, float start, float gap, float h) {
}

//transform array of values into array of proportions ( % values )
private ArrayList<Float> getPercentValues(ArrayList<Integer> val) {
int sum = 0;
private ArrayList<Float> getProportionValues(ArrayList<Double> val) {
double sum = 0;
ArrayList<Float> percentValues = new ArrayList<>();

//get sum of all arguments
for (int iterator : val) sum += iterator;

for (double iterator : val) sum += iterator;
//divide each element by sum to get % values
for (int v = 0; v < val.size(); v++) {
//check for minimalSegmentValue
if ((val.get(v) * 100 / sum) > 0 && (val.get(v) * 100 / sum) < minimalSegmentValue) {
percentValues.add((float) minimalSegmentValue);
} else {
percentValues.add((float) val.get(v) * 100 / sum);
percentValues.add((float) (val.get(v) * 100 / sum));
}
}
return percentValues;
Expand Down Expand Up @@ -256,56 +275,37 @@ private Integer getColorFromQueue() {
return temp;
}

public boolean checkValue(int v) {
return v > 0;
}

//currently only for percentValueList.size() = 3
//currently only for proportionValueList.size() = 3
public void playAnimation() {
//setup animations for proportionsBar4
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator animIntList1 = ObjectAnimator
.ofFloat(this, "FirstSegment", 0, this.percentValueList.get(0));
.ofFloat(this, "FirstSegment", 0, this.proportionValueList.get(0));
animIntList1.setDuration(animationDuration);
ObjectAnimator animIntList2 = ObjectAnimator
.ofFloat(this, "SecondSegment", this.percentValueList.get(0), this.percentValueList.get(1));
.ofFloat(this, "SecondSegment", this.proportionValueList.get(0), this.proportionValueList.get(1));
animIntList2.setDuration(animationDuration);
animSet.playTogether(animIntList1, animIntList2);
animSet.start();
}

//setters are needed to animate custom view via external ObjectAnimator
public void setFirstSegment(float i) {
this.percentValueList.set(0, i);
this.proportionValueList.set(0, i);
//redraw custom view on every argument change
invalidate();
}

public int setSecondSegment(float j) {
this.percentValueList.set(1, j);
return valueList.get(0);
public void setSecondSegment(float j) {
this.proportionValueList.set(1, j);
invalidate();
}

public void setThirdSegment(float k) {
this.percentValueList.set(2, k);
this.proportionValueList.set(2, k);
invalidate();
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

ArrayList<Float> k = getPercentValues(valueList);
//fill percent list used for segment drawing
for (int i = 0; i < valueList.size(); i++) {
percentValueList.add(k.get(i));
}

//check for the first view launch animation
if (animated && firstLaunch) playAnimation();
firstLaunch = false;
}

@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
Expand Down
4 changes: 3 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
implementation 'org.jetbrains:annotations-java5:15.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//proportionsbarlibrary
implementation 'com.github.maxViolet:ProportionsBarLibrary:0.1.0'
// implementation 'com.github.maxViolet:ProportionsBarLibrary:0.1.0'
implementation project(":proportionsbarlibrary")
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private void setup1stExample() {
.radiusRoundedCorners(3.5)
.minimalSegmentValue(1)
.addColors("#81DAF5", "#008db9", "#1c0a63")
.addValues(4, 37, 11, 4, 4, 4);
.addValues(4.0, 37, 11, 3.9, -4.89, 4);

LinearLayout container1 = findViewById(R.id.container_pb1);
container1.addView(proportionsBar1);
Expand All @@ -45,13 +45,19 @@ private void setup2ndExample() {
.addColors(getResources().getColor(R.color.example2_1),
getResources().getColor(R.color.example2_2),
getResources().getColor(R.color.example2_3))
.addValues(11, 30, 24, 12, 41, 27);
.addValues(11.8, 30.05, 24.79, 12.001, 41.238, 27.11);

LinearLayout container2 = findViewById(R.id.container_pb2);
container2.addView(proportionsBar2);
}

private void setup3rdExample() {
long a = 22;
short b = 3;
int c = -10;
byte d = 9;
float e = -20;

ProportionsBar proportionsBar3 = new ProportionsBar(this);
proportionsBar3.setId(R.id.proportions_bar_3);
proportionsBar3.showGaps(true)
Expand All @@ -62,7 +68,7 @@ private void setup3rdExample() {
getResources().getColor(R.color.example3_3),
getResources().getColor(R.color.example3_4),
getResources().getColor(R.color.example3_5))
.addValues(33, 2, 11, 40, 20);
.addValues(a, b, c, d, e);

LinearLayout container3 = findViewById(R.id.container_pb3);
container3.addView(proportionsBar3);
Expand Down

0 comments on commit 954e8f4

Please sign in to comment.