From b58bc41c9de48403abd1f78c0c3cb4e49a42a62d Mon Sep 17 00:00:00 2001 From: Dekola Date: Tue, 31 Jul 2018 15:53:50 +0100 Subject: [PATCH 1/2] Added feature to change arc color as the value progresses --- .../philjay/circledisplay/CircleDisplay.java | 19 +++++++++++++++++++ .../philjay/circledisplay/MainActivity.java | 1 + README.md | 4 +++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CircleDisplay/src/com/philjay/circledisplay/CircleDisplay.java b/CircleDisplay/src/com/philjay/circledisplay/CircleDisplay.java index b76a3bf..6567aab 100644 --- a/CircleDisplay/src/com/philjay/circledisplay/CircleDisplay.java +++ b/CircleDisplay/src/com/philjay/circledisplay/CircleDisplay.java @@ -157,6 +157,9 @@ protected void onDraw(Canvas canvas) { else drawText(canvas); } + + if (mColors !=null ) + setColors(mValue * mPhase); } /** @@ -816,4 +819,20 @@ public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } + + private int mNoOfColors; + private int[] mColors; + + public void setColors(int[] mColors) { + this.mNoOfColors = mColors.length; + this.mColors = mColors; + } + + private void setColors(float val) { + float mEqualPart = mMaxValue/ mNoOfColors; + int index = (int) (val/mEqualPart); + if (index == mNoOfColors) + index-=1; + mArcPaint.setColor(mColors[index]); + } } diff --git a/CircleDisplay/src/com/philjay/circledisplay/MainActivity.java b/CircleDisplay/src/com/philjay/circledisplay/MainActivity.java index f1727fc..3cf266c 100644 --- a/CircleDisplay/src/com/philjay/circledisplay/MainActivity.java +++ b/CircleDisplay/src/com/philjay/circledisplay/MainActivity.java @@ -30,6 +30,7 @@ protected void onCreate(Bundle savedInstanceState) { mCircleDisplay.setTouchEnabled(true); mCircleDisplay.setUnit("%"); mCircleDisplay.setStepSize(0.5f); + mCircleDisplay.setColors(new int[]{Color.RED, Color.YELLOW, Color.GREEN}); mCircleDisplay.showValue(75f, 100f, true); } diff --git a/README.md b/README.md index 2fafec1..e37b9be 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,8 @@ or create it in code: - setStepSize(float stepsize): Sets the stepsize (minimum selection interval) of the circle display, default 1f. It is recommended to make this value not higher than 1/5 of the maximum selectable value, and not lower than 1/200 of the maximum selectable value. For example, if a maximum of 100 has been chosen, a stepsize between 0.5 and 20 is recommended. - setCustomText(String[] custom): Sets an array of custom Strings to be drawn instead of the actual value in the center of the CircleDisplay. If set to null, the custom text will be reset and the value will be drawn. Make sure the length of the array corresponds with the maximum number of steps (maxvalue / stepsize). - + - setColors(int[] colors): Sets an array of colors to set the color for the arc/bar to change with the value as it progresses. You can either use Color.COLORNAME as a parameter or getColor(resid). + **Showing stuff:** - public void showValue(float toShow, float total, boolean animated): Shows the given value. A maximumvalue also needs to be provided. Set animated to true to animate the displaying of the value. @@ -81,6 +82,7 @@ default 1f. It is recommended to make this value not higher than 1/5 of the maxi cd.setUnit("%"); cd.setStepSize(0.5f); // cd.setCustomText(...); // sets a custom array of text + cd.setColors(new int[]{Color.RED, Color.YELLOW, Color.GREEN}); cd.showValue(75f, 100f, true); ``` From b7b2e539ba737a19efaf9dd1cd58823a959fcef9 Mon Sep 17 00:00:00 2001 From: Dekola Date: Tue, 31 Jul 2018 16:14:18 +0100 Subject: [PATCH 2/2] Added comments --- .../philjay/circledisplay/CircleDisplay.java | 27 ++++++++++++++----- .../philjay/circledisplay/MainActivity.java | 1 + 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CircleDisplay/src/com/philjay/circledisplay/CircleDisplay.java b/CircleDisplay/src/com/philjay/circledisplay/CircleDisplay.java index 6567aab..c52eb0b 100644 --- a/CircleDisplay/src/com/philjay/circledisplay/CircleDisplay.java +++ b/CircleDisplay/src/com/philjay/circledisplay/CircleDisplay.java @@ -92,6 +92,12 @@ public class CircleDisplay extends View implements OnGestureListener { /** object animator for doing the drawing animations */ private ObjectAnimator mDrawAnimator; + /** This represents the total number of colors in mColors arrays */ + private int mNoOfColors; + + /** array that contains values for the arc colors */ + private int[] mColors; + public CircleDisplay(Context context) { super(context); init(); @@ -820,14 +826,21 @@ public void onShowPress(MotionEvent e) { } - private int mNoOfColors; - private int[] mColors; - - public void setColors(int[] mColors) { - this.mNoOfColors = mColors.length; - this.mColors = mColors; + /** + * Sets an array of colors to set the color for the arc/bar to change with the value as it progresses. + * You can either use Color.COLORNAME as a parameter or getColor(resid) + * @param colors + */ + public void setColors(int[] colors) { + this.mNoOfColors = colors.length; + this.mColors = colors; } + /** + * If the colors array isn't null, the maximum value will be divide by the number of colors to give each color equal amount of value + * @param val + */ + private void setColors(float val) { float mEqualPart = mMaxValue/ mNoOfColors; int index = (int) (val/mEqualPart); @@ -835,4 +848,4 @@ private void setColors(float val) { index-=1; mArcPaint.setColor(mColors[index]); } -} +} \ No newline at end of file diff --git a/CircleDisplay/src/com/philjay/circledisplay/MainActivity.java b/CircleDisplay/src/com/philjay/circledisplay/MainActivity.java index 3cf266c..7bb8173 100644 --- a/CircleDisplay/src/com/philjay/circledisplay/MainActivity.java +++ b/CircleDisplay/src/com/philjay/circledisplay/MainActivity.java @@ -8,6 +8,7 @@ import android.util.Log; import android.view.Menu; import android.view.MenuItem; +import android.graphics.Color; import com.philjay.circledisplay.CircleDisplay.SelectionListener;