diff --git a/app/src/main/java/in/krharsh17/barview_sample/MainActivity.java b/app/src/main/java/in/krharsh17/barview_sample/MainActivity.java index 3d0fb8b..523a8ea 100644 --- a/app/src/main/java/in/krharsh17/barview_sample/MainActivity.java +++ b/app/src/main/java/in/krharsh17/barview_sample/MainActivity.java @@ -12,9 +12,7 @@ import static in.krharsh17.barview.BarView.getRandomColor; - public class MainActivity extends AppCompatActivity { - BarView barView; int cornerRadius = 20;//in dp @@ -27,48 +25,48 @@ protected void onCreate(Bundle savedInstanceState) { ArrayList barModels = new ArrayList<>(); barModels.add(new BarModel( - "Samsung", - "30.91", - getRandomColor(), - 0.31f,0,0 + "Samsung", + "30.91", + getRandomColor(), + 0.31f, 0, 0 )); barModels.add(new BarModel( - "Apple", - "25.89", - getRandomColor(), - 1f,5,5 + "Apple", + "25.89", + getRandomColor(), + 1f, 5, 5 )); barModels.add(new BarModel( - "Huawei", - "10.98", - getRandomColor(), - 0.11f,8,4 + "Huawei", + "10.98", + getRandomColor(), + 0.11f, 8, 4 )); barModels.add(new BarModel( - "Xiaomi", - "7.8", - getRandomColor(), - 0.07f,12,7 + "Xiaomi", + "7.8", + getRandomColor(), + 0.07f, 12, 7 )); barModels.add(new BarModel( - "Oppo", - "4.31", - getRandomColor(), - 0.04f,7,18 + "Oppo", + "4.31", + getRandomColor(), + 0.04f, 7, 18 )); barModels.add(new BarModel( - "Others", - "20.11", - getRandomColor(), - 0.20f,8,10 + "Others", + "20.11", + getRandomColor(), + 0.20f, 8, 10 )); barView.setData(barModels); - + barView.setCornerRadius(cornerRadius); barView.setOnBarClickListener(new BarView.OnBarClickListener() { @@ -77,8 +75,5 @@ public void onBarClicked(int pos) { Toast.makeText(MainActivity.this, "Bar at position " + pos, Toast.LENGTH_SHORT).show(); } }); - } - - } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7dd2db8..c0647e0 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,15 +1,14 @@ + android:layout_width="match_parent" + android:layout_height="match_parent" /> diff --git a/app/src/test/java/in/krharsh17/barview_sample/ExampleUnitTest.java b/app/src/test/java/in/krharsh17/barview_sample/ExampleUnitTest.java index 1edcae4..7b68161 100644 --- a/app/src/test/java/in/krharsh17/barview_sample/ExampleUnitTest.java +++ b/app/src/test/java/in/krharsh17/barview_sample/ExampleUnitTest.java @@ -11,7 +11,7 @@ */ public class ExampleUnitTest { @Test - public void addition_isCorrect() { + public void additionIsCorrect() { assertEquals(4, 2 + 2); } } \ No newline at end of file diff --git a/library/src/main/java/in/krharsh17/barview/Bar.java b/library/src/main/java/in/krharsh17/barview/Bar.java index b267a52..379a691 100644 --- a/library/src/main/java/in/krharsh17/barview/Bar.java +++ b/library/src/main/java/in/krharsh17/barview/Bar.java @@ -12,7 +12,6 @@ import android.view.animation.DecelerateInterpolator; class Bar extends View { - /** * It can be called by anyone directly from code to create a new instance of the view. * This constructor doesn’t have access to XML attributes, so you have to fill the parameters manually, using setters. @@ -30,12 +29,11 @@ class Bar extends View { * @param progress */ //increaseWidth is used to give some extra width to bar so that this extra width is used by shadow - public void setProgress(float progress,int increaseWidth,int animationType,int animationDuration) { + public void setProgress(float progress, int increaseWidth, int animationType, int animationDuration) { ViewGroup.LayoutParams params = this.getLayoutParams(); - if(animationType == BarView.INTRO_ANIM_EXPAND){ - expand(this,animationDuration,Math.round(params.width * (progress))); - } - else if (animationType == BarView.INTRO_ANIM_NONE){ + if (animationType == BarView.INTRO_ANIM_EXPAND) { + expand(this, animationDuration, Math.round(params.width * (progress))); + } else if (animationType == BarView.INTRO_ANIM_NONE) { this.setVisibility(VISIBLE); params.width = Math.round(params.width * (progress)) + increaseWidth; this.setLayoutParams(params); diff --git a/library/src/main/java/in/krharsh17/barview/BarGroup.java b/library/src/main/java/in/krharsh17/barview/BarGroup.java index dea1c7f..7706c36 100644 --- a/library/src/main/java/in/krharsh17/barview/BarGroup.java +++ b/library/src/main/java/in/krharsh17/barview/BarGroup.java @@ -24,7 +24,6 @@ import androidx.constraintlayout.widget.ConstraintSet; import androidx.constraintlayout.widget.Constraints; - import java.util.Hashtable; /** @@ -32,22 +31,18 @@ * extends ConstraintLayout and implements Constants interface */ class BarGroup extends ConstraintLayout implements Constants { - Context context; - TextView label; - View initial; - Bar bar; - TextView value; - ConstraintSet constraintSet; - LayoutParams labelParams; - LayoutParams initialParams; - String labelText; - String color; - String valueText; - float progress; - public int elevation; - public int radius; - public int numberOfLayers; - + private int elevation; + private int radius; + private int numberOfLayers; + private Context context; + private TextView label; + private View initial; + private Bar bar; + private TextView value; + private String labelText; + private String color; + private String valueText; + private float progress; private int animationType; private int animationDuration = Constants.DEFAULT_ANIMATION_DURATION; private int BAR_MARGIN = 6; @@ -56,39 +51,37 @@ class BarGroup extends ConstraintLayout implements Constants { private int LABEL_FONT_SIZE = 18; private int VALUE_FONT_SIZE = 9; private String labelTextColor = LABEL_TEXT_COLOR; - private String valueTextColor = VALUE_TEXT_COLOR,VALUE_FONT=null,LABEL_FONT=null; + private String valueTextColor = VALUE_TEXT_COLOR, VALUE_FONT = null, LABEL_FONT = null; private String rippleColor = RIPPLE_COLOR; private int CORNER_RADIUS; - /** * one bar has different drawable stacked together with same solid color but different alpha value * this array of string defined different alpha value from 0 to 1 * we will use maximum of 18 layers for shadow * numbers of layers can very depending on elevation value */ - public String alphaSet[] = { - "#00", - "#02", - "#04", - "#06", - "#08", - "#10", - "#12", - "#14", - "#16", - "#18", - "#20", - "#22", - "#24", - "#26", - "#28", - "#30", - "#32", - "#35", - "#" + public String alphaSet[] = { + "#00", + "#02", + "#04", + "#06", + "#08", + "#10", + "#12", + "#14", + "#16", + "#18", + "#20", + "#22", + "#24", + "#26", + "#28", + "#30", + "#32", + "#35", + "#" }; - - public static Hashtable fontCache = new Hashtable<>(); + public static Hashtable fontCache = new Hashtable<>(); /** * parameterized constructor @@ -122,8 +115,7 @@ class BarGroup extends ConstraintLayout implements Constants { * @param color hex color value for the fill of barGroup instance * @param valueText for approximating the length of Bargroup instance * @param progress marking the progress of the bar - * - * self explanatory constants + * self explanatory constants * @param BAR_MARGIN * @param VERTICAL_SPACING * @param BAR_HEIGHT @@ -137,20 +129,20 @@ class BarGroup extends ConstraintLayout implements Constants { * @param VALUE_FONT */ public BarGroup( - Context context, - String labelText, - String color, - String valueText, + Context context, + String labelText, + String color, + String valueText, float progress, int animationType, int animationDuration, - int BAR_MARGIN, - int VERTICAL_SPACING, - int BAR_HEIGHT, - int LABEL_FONT_SIZE, - int VALUE_FONT_SIZE, + int BAR_MARGIN, + int VERTICAL_SPACING, + int BAR_HEIGHT, + int LABEL_FONT_SIZE, + int VALUE_FONT_SIZE, String labelTextColor, - String VALUE_TEXT_COLOR, + String VALUE_TEXT_COLOR, String RIPPLE_COLOUR, int CORNER_RADIUS, String LABEL_FONT, @@ -173,8 +165,8 @@ public BarGroup( this.labelTextColor = labelTextColor; this.valueTextColor = VALUE_TEXT_COLOR; this.rippleColor = RIPPLE_COLOUR; - this.LABEL_FONT=LABEL_FONT; - this.VALUE_FONT=VALUE_FONT; + this.LABEL_FONT = LABEL_FONT; + this.VALUE_FONT = VALUE_FONT; this.CORNER_RADIUS = CORNER_RADIUS; label = new TextView(context); initial = new View(context); @@ -192,7 +184,7 @@ protected void onAttachedToWindow() { initial.setId(View.generateViewId()); bar.setId(View.generateViewId()); value.setId(View.generateViewId()); - if(this.elevation>0) { + if (this.elevation > 0) { switch (this.elevation) { case 1: case 2: @@ -238,27 +230,27 @@ protected void onAttachedToWindow() { setupInitial(); setupBar(); setupValue(); - applyConstraints(); - } /** * Initializer function for the label segment */ - void setupLabel() { - labelParams = new Constraints.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT); + private void setupLabel() { + LayoutParams labelParams = new Constraints.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT); labelParams.setMargins( - dp(8), - dp(VERTICAL_SPACING / 2), - dp(8), - dp(VERTICAL_SPACING / 2) + dp(8), + dp(VERTICAL_SPACING / 2), + dp(8), + dp(VERTICAL_SPACING / 2) ); + label.setText(parseLabel(labelText)); label.setTextColor(Color.parseColor(labelTextColor)); label.setTextSize(TypedValue.COMPLEX_UNIT_SP, LABEL_FONT_SIZE); - if(LABEL_FONT!=null) + if (LABEL_FONT != null) { label.setTypeface(Typeface.createFromAsset(context.getAssets(), LABEL_FONT)); + } label.setLayoutParams(labelParams); label.setTextAlignment(TEXT_ALIGNMENT_CENTER); label.setGravity(Gravity.CENTER_VERTICAL); @@ -267,23 +259,23 @@ void setupLabel() { /** * Initializer function for the initial block - * */ - void setupInitial() { - Drawable[] layers = new Drawable[this.numberOfLayers+1]; - for(int i =0 ;i<=this.numberOfLayers;i++){ + private void setupInitial() { + Drawable[] layers = new Drawable[this.numberOfLayers + 1]; + for (int i = 0; i <= this.numberOfLayers; i++) { layers[i] = null; } - for(int i=0;i<=this.numberOfLayers;i++){ - layers[i] = getRoundRect(color.substring(1),i,this.numberOfLayers,this.radius); + for (int i = 0; i <= this.numberOfLayers; i++) { + layers[i] = getRoundRect(color.substring(1), i, this.numberOfLayers, this.radius); } float increaseHeight = 1;//To give some extra height of bar for shadow int increaseWidth = 0;//To give some extra width of bar for shadow - if(this.numberOfLayers>0){ + if (this.numberOfLayers > 0) { increaseHeight = 1.2f + (this.numberOfLayers - 5) * 0.1f; increaseWidth = 15 + (this.numberOfLayers - 5) * 4; } - initialParams = new LayoutParams(dp(12) + increaseWidth, (int) (dp(BAR_HEIGHT) * increaseHeight)); + + LayoutParams initialParams = new LayoutParams(dp(12) + increaseWidth, (int) (dp(BAR_HEIGHT) * increaseHeight)); initialParams.rightMargin = dp(12); initial.setLayoutParams(initialParams); LayerDrawable splash_test = new LayerDrawable(layers); @@ -293,58 +285,53 @@ void setupInitial() { initial.setClickable(true); initial.setFocusable(true); this.addView(initial); - if(animationType == BarView.INTRO_ANIM_NONE){ + if (animationType == BarView.INTRO_ANIM_NONE) { initial.setVisibility(VISIBLE); initialParams = new LayoutParams(dp(12), dp(BAR_HEIGHT)); initialParams.rightMargin = dp(12); initial.setLayoutParams(initialParams); - } - else if (animationType == BarView.INTRO_ANIM_EXPAND){ - int screen_width = Math.round((160*context.getResources().getDisplayMetrics().widthPixels)/(context.getResources().getDisplayMetrics().xdpi)); + } else if (animationType == BarView.INTRO_ANIM_EXPAND) { + int screen_width = Math.round((160 * context.getResources().getDisplayMetrics().widthPixels) / (context.getResources().getDisplayMetrics().xdpi)); initialParams = new LayoutParams(screen_width, dp(BAR_HEIGHT)); initialParams.rightMargin = dp(12); initial.setLayoutParams(initialParams); - expand(initial,animationDuration,dp(12)); + expand(initial, animationDuration, dp(12)); } - } /** * Initializer function for the main bar - * */ public void setupBar() { - Drawable[] layers = new Drawable[this.numberOfLayers+1]; - for(int i =0 ;i<=this.numberOfLayers;i++){ + Drawable[] layers = new Drawable[this.numberOfLayers + 1]; + for (int i = 0; i <= this.numberOfLayers; i++) { layers[i] = null; } - for(int i=0;i<=this.numberOfLayers;i++){ - layers[i] = getRoundRect(color.substring(1),i,this.numberOfLayers,this.radius); + for (int i = 0; i <= this.numberOfLayers; i++) { + layers[i] = getRoundRect(color.substring(1), i, this.numberOfLayers, this.radius); } float increaseHeight = 1; int increaseWidth = 0; - if(this.numberOfLayers>0){ + if (this.numberOfLayers > 0) { increaseHeight = 1.2f + (this.numberOfLayers - 5) * 0.1f; increaseWidth = 20 + (this.numberOfLayers - 5) * 4; } - int screen_width = Math.round((160*context.getResources().getDisplayMetrics().widthPixels)/(context.getResources().getDisplayMetrics().xdpi)) ; + int screen_width = Math.round((160 * context.getResources().getDisplayMetrics().widthPixels) / (context.getResources().getDisplayMetrics().xdpi)); bar.setLayoutParams(new LinearLayout.LayoutParams( - screen_width, (int) (dp(BAR_HEIGHT) * increaseHeight) + screen_width, (int) (dp(BAR_HEIGHT) * increaseHeight) )); LayerDrawable splash_test = new LayerDrawable(layers); bar.setBackground(splash_test); this.addView(bar); bar.setVisibility(GONE); Bar.setRippleDrawable(bar, Color.parseColor(color), Color.parseColor(rippleColor)); - bar.setProgress(progress,increaseWidth,animationType,animationDuration); + bar.setProgress(progress, increaseWidth, animationType, animationDuration); } /** * Initializer function for the value tooltip - * */ - void setupValue() { - + private void setupValue() { value.setText(valueText); value.setBackground(context.getResources().getDrawable(R.drawable.label_background)); value.setRotation(90); @@ -353,7 +340,7 @@ void setupValue() { value.setTextColor(Color.parseColor(valueTextColor)); value.setTextSize(TypedValue.COMPLEX_UNIT_SP, VALUE_FONT_SIZE); value.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - if(VALUE_FONT !=null) { + if (VALUE_FONT != null) { Typeface tf = get(VALUE_FONT, context); value.setTypeface(tf); } @@ -369,7 +356,6 @@ void setupValue() { * * @param name * @param context - * @return */ public static Typeface get(String name, Context context) { Typeface tf = fontCache.get(name); @@ -386,10 +372,9 @@ public static Typeface get(String name, Context context) { /** * Sets constraints for all pieces of a BarGroup instance - * */ - void applyConstraints() { - constraintSet = new ConstraintSet(); + private void applyConstraints() { + ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(this); constraintSet.setHorizontalBias(initial.getId(), 0.30f); @@ -411,7 +396,6 @@ void applyConstraints() { constraintSet.connect(label.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP); constraintSet.connect(label.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM); - constraintSet.applyTo(this); } @@ -419,9 +403,8 @@ void applyConstraints() { * Converts density independent pixel units (dp) to pixel units (px) * * @param dp - * @return */ - int dp(float dp) { + private int dp(float dp) { return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics())); } @@ -433,52 +416,53 @@ int dp(float dp) { * @param currentLayer * @param totalLayer * @param radius - * @return */ - public Drawable getRoundRect(String color,int currentLayer,int totalLayer,int radius) { - - if(radius<0){ - radius = 0; - } - if(radius>20){ - radius = 20; - } + public Drawable getRoundRect(String color, int currentLayer, int totalLayer, int radius) { + int localRadius = radius; int leftTopPadding = 1; - if(currentLayer%3==0){ - leftTopPadding = 2; - } int rightPadding = 3; int bottomPadding = 3; - String shadowalpha = null; int increment = 16 / (totalLayer - 2); int layernumber = 0; + + if (radius < 0) { + localRadius = 0; + } + if (radius > 20) { + localRadius = 20; + } + + if (currentLayer % 3 == 0) { + leftTopPadding = 2; + } + if (currentLayer == totalLayer) { shadowalpha = "#"; - }else{ + } else { layernumber = increment * currentLayer; if (layernumber >= 17) { layernumber = 17; } - shadowalpha = alphaSet[layernumber] ; + shadowalpha = alphaSet[layernumber]; } RoundRectShape rectShape = new RoundRectShape(new float[]{ - radius * 2, radius * 2, radius * 2, radius * 2, - radius * 2, radius * 2, radius * 2, radius * 2 + localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 2, + localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 2 }, null, null); if (totalLayer == currentLayer) { - if (radius == 0) { - radius = 2; + if (localRadius == 0) { + localRadius = 2; } rectShape = new RoundRectShape(new float[]{ - radius * 2, radius * 2, radius * 2, radius * 2, - radius * 2, radius * 2, radius * 2, radius * 2 + localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 2, + localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 2 }, null, null); } ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape); - shapeDrawable.setPadding(leftTopPadding,leftTopPadding,rightPadding,bottomPadding); + shapeDrawable.setPadding(leftTopPadding, leftTopPadding, rightPadding, bottomPadding); shapeDrawable.getPaint().setColor(Color.parseColor(shadowalpha + color)); shapeDrawable.getPaint().setStyle(Paint.Style.FILL); shapeDrawable.getPaint().setAntiAlias(true); @@ -486,8 +470,6 @@ public Drawable getRoundRect(String color,int currentLayer,int totalLayer,int ra return shapeDrawable; } - - /** * Animator function for the 'expand' intro animation * @@ -517,23 +499,24 @@ public void onAnimationUpdate(ValueAnimator animation) { /** * Parses the label text to truncate or hyphenize the string to fit in the given space + * * @param labelText - * @return */ private String parseLabel(String labelText) { String[] tokens = labelText.split(" "); StringBuilder finalizedString = new StringBuilder(); for (String s : tokens) { - if (s.length() < 8) + if (s.length() < 8) { finalizedString.append(s + "\n"); - else if (s.length() >= 8 && s.length() < 12) { + } else if (s.length() >= 8 && s.length() < 12) { finalizedString.append(s.substring(0, 7) + "\n"); finalizedString.append("-" + s.substring(7) + "\n"); } else if (s.length() >= 12 && s.length() < 15) { finalizedString.append(s.substring(0, 7) + "\n"); finalizedString.append("-" + s.substring(7, 12) + "\n"); - if (s.length() > 13) + if (s.length() > 13) { finalizedString.append("-" + s.substring(13) + "\n"); + } } else { finalizedString.append(s.substring(0, 5) + "..\n"); } @@ -580,6 +563,7 @@ public boolean willChangeBounds() { /** * This creates a gradient drawable background for the bars - sets a shape, a color and radius for the corners. + * * @param color is the color in int which is used as the background color. */ public GradientDrawable setUpRoundBars(int color) { @@ -589,5 +573,4 @@ public GradientDrawable setUpRoundBars(int color) { gradientDrawable.setCornerRadius(CORNER_RADIUS); return gradientDrawable; } - } diff --git a/library/src/main/java/in/krharsh17/barview/BarModel.java b/library/src/main/java/in/krharsh17/barview/BarModel.java index 19ea264..dd5d9b0 100644 --- a/library/src/main/java/in/krharsh17/barview/BarModel.java +++ b/library/src/main/java/in/krharsh17/barview/BarModel.java @@ -31,44 +31,41 @@ public void setRadius(int radius) { /** * getters and setters */ - public String getLabel() { - return this.label; + return this.label; } public void setLabel(String label) { - this.label = label; + this.label = label; } - public String getValue() { - return this.value; + public String getValue() { + return this.value; } public void setValue(String value) { - this.value = value; + this.value = value; } - public String getColor() { - return this.color; + public String getColor() { + return this.color; } public void setColor(String color) { - this.color = color; + this.color = color; } - public Float getFillRatio() { - return this.fillRatio; + public Float getFillRatio() { + return this.fillRatio; } public void setFillRatio(Float fillRatio) { - this.fillRatio = fillRatio; + this.fillRatio = fillRatio; } - /** * parameterized constructor * - * * @param label for the barGroup instance * @param value for approximating the length of Bargroup instance * @param color hex color value for the fill of barGroup instance diff --git a/library/src/main/java/in/krharsh17/barview/BarView.java b/library/src/main/java/in/krharsh17/barview/BarView.java index 94e3eaf..0867ee5 100644 --- a/library/src/main/java/in/krharsh17/barview/BarView.java +++ b/library/src/main/java/in/krharsh17/barview/BarView.java @@ -6,33 +6,28 @@ import android.graphics.drawable.GradientDrawable; import android.util.AttributeSet; import android.util.Log; -import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.ScrollView; -import androidx.constraintlayout.widget.ConstraintLayout; - import java.util.ArrayList; import java.util.List; /** * This is the custom view which is the cumulation of various individual barGroups * extends from a ScrollView and implementing Constant interface - * */ public class BarView extends ScrollView implements Constants { + public static final int INTRO_ANIM_NONE = 0; + public static final int INTRO_ANIM_EXPAND = 1; private LinearLayout containerLayout; private Context context; private OnBarClickListener onBarClickListener; private List barGroups; private List data; private boolean isDataPopulated; - - public static final int INTRO_ANIM_NONE = 0; - public static final int INTRO_ANIM_EXPAND = 1; private int barMargin = 6; private int verticalSpacing = 48; private int barHeight = 20; @@ -47,9 +42,9 @@ public class BarView extends ScrollView implements Constants { private int cornerRadius; private String labelTextColor = Constants.LABEL_TEXT_COLOR; private String valueTextColor = Constants.VALUE_TEXT_COLOR; - private String LABEL_FONT=null,VALUE_FONT=null; - private String rippleColor = Constants.RIPPLE_COLOR; // has to be >2 - + private String LABEL_FONT = null; + private String VALUE_FONT = null; + private String rippleColor = Constants.RIPPLE_COLOR; /** * parameterized constructors @@ -69,7 +64,6 @@ public BarView(Context context) { containerLayout.setOrientation(LinearLayout.VERTICAL); this.addView(containerLayout); - } /** @@ -103,8 +97,8 @@ public BarView(Context context, AttributeSet attrs) { barHeight = a.getInteger(R.styleable.BarView_barHeight, barHeight); labelFontSize = a.getInteger(R.styleable.BarView_labelTextSize, labelFontSize); valueFontSize = a.getInteger(R.styleable.BarView_valueTextSize, valueFontSize); - VALUE_FONT=a.getString(R.styleable.BarView_labelFont); - LABEL_FONT=a.getString(R.styleable.BarView_labelFont); + VALUE_FONT = a.getString(R.styleable.BarView_labelFont); + LABEL_FONT = a.getString(R.styleable.BarView_labelFont); labelTextColor = a.getString(R.styleable.BarView_labelTextColor); valueTextColor = a.getString(R.styleable.BarView_valueTextColor); rippleColor = a.getString(R.styleable.BarView_rippleColor); @@ -112,24 +106,28 @@ public BarView(Context context, AttributeSet attrs) { gradientStart = a.getString(R.styleable.BarView_gradientStart); gradientEnd = a.getString(R.styleable.BarView_gradientEnd); gradientDirection = a.getString(R.styleable.BarView_gradientDirection); - if (labelTextColor == null) + if (labelTextColor == null) { labelTextColor = Constants.LABEL_TEXT_COLOR; - if (valueTextColor == null) + } + if (valueTextColor == null) { valueTextColor = Constants.VALUE_TEXT_COLOR; - if (rippleColor == null) + } + if (rippleColor == null) { rippleColor = RIPPLE_COLOR; - if (gradientDirection == null) + } + if (gradientDirection == null) { gradientDirection = "horizontal"; - if (backgroundColor != null) + } + if (backgroundColor != null) { setBackgroundColor(backgroundColor); - if (gradientStart != null && gradientEnd != null) + } + if (gradientStart != null && gradientEnd != null) { setBackgroundGradient(gradientStart, gradientEnd, gradientDirection); + } a.recycle(); } } - - /** * Returns a reference to the attached Listener */ @@ -147,31 +145,27 @@ public void setOnBarClickListener(OnBarClickListener onBarClickListener) { public interface OnBarClickListener { void onBarClicked(int pos); } - public void setData(List data) { this.data = data; - if(animationType == BarView.INTRO_ANIM_NONE){ - populateBarView(BarView.INTRO_ANIM_NONE,animationDuration); - } - else if (animationType == BarView.INTRO_ANIM_EXPAND){ - populateBarView(BarView.INTRO_ANIM_EXPAND,animationDuration); + if (animationType == BarView.INTRO_ANIM_NONE) { + populateBarView(BarView.INTRO_ANIM_NONE, animationDuration); + } else if (animationType == BarView.INTRO_ANIM_EXPAND) { + populateBarView(BarView.INTRO_ANIM_EXPAND, animationDuration); } isDataPopulated = true; } public void setData(List data, boolean isAnimationEnabled) { this.data = data; - if (isAnimationEnabled){ - if(animationType == BarView.INTRO_ANIM_NONE){ - populateBarView(BarView.INTRO_ANIM_NONE,animationDuration); + if (isAnimationEnabled) { + if (animationType == BarView.INTRO_ANIM_NONE) { + populateBarView(BarView.INTRO_ANIM_NONE, animationDuration); + } else if (animationType == BarView.INTRO_ANIM_EXPAND) { + populateBarView(BarView.INTRO_ANIM_EXPAND, animationDuration); } - else if (animationType == BarView.INTRO_ANIM_EXPAND){ - populateBarView(BarView.INTRO_ANIM_EXPAND,animationDuration); - } - } - else { - populateBarView(BarView.INTRO_ANIM_NONE,animationDuration); + } else { + populateBarView(BarView.INTRO_ANIM_NONE, animationDuration); } } @@ -181,7 +175,7 @@ else if (animationType == BarView.INTRO_ANIM_EXPAND){ */ private void populateBarView(int animationType, int animationDuration) { for (BarModel b : data) { - addBar(b,animationType,animationDuration); + addBar(b, animationType, animationDuration); } } @@ -190,16 +184,15 @@ private void populateBarView(int animationType, int animationDuration) { * a param and cumulates it into barGroups. * * @param data is a BarModel that contains all the required to - * construct a BarGroup instance. + * construct a BarGroup instance. */ - - private void addBar(BarModel data,int animationType,int animationDuration) { + private void addBar(BarModel data, int animationType, int animationDuration) { BarGroup barGroup = new BarGroup( - context, - data.getLabel(), - data.getColor(), - data.getValue(), - data.getFillRatio(), + context, + data.getLabel(), + data.getColor(), + data.getValue(), + data.getFillRatio(), animationType, animationDuration, barMargin, @@ -213,11 +206,10 @@ private void addBar(BarModel data,int animationType,int animationDuration) { cornerRadius, LABEL_FONT, VALUE_FONT, - data.getElevation(), - data.getRadius() + data.getElevation(), + data.getRadius() ); - barGroup.setOnTouchListener(new OnTouchListener() { private int CLICK_ACTION_THRESHOLD = 200; private float startX; @@ -240,7 +232,7 @@ public boolean onTouch(View v, MotionEvent event) { break; default: Log.d("BarView", "onTouch:Unknown Event "); - break; + break; } return true; } @@ -265,23 +257,23 @@ public void setBackgroundColor(String color) { public void setBackgroundGradient(String startColor, String endColor, String direction) { GradientDrawable gd; switch (direction) { - case "horizontal": - gd = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, - new int[] { Color.parseColor(startColor), Color.parseColor(endColor) }); - break; - case "vertical": - gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, - new int[] { Color.parseColor(startColor), Color.parseColor(endColor) }); - break; - default: - gd = null; - break; + case "horizontal": + gd = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, + new int[]{Color.parseColor(startColor), Color.parseColor(endColor)}); + break; + case "vertical": + gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, + new int[]{Color.parseColor(startColor), Color.parseColor(endColor)}); + break; + default: + gd = null; + break; } - if (gd != null) + if (gd != null) { containerLayout.setBackground(gd); + } } - /** * This function returns a random color based on a constant {@value #CHAR_ARRAY} * It builds up the random hex value by randomly choosing between any of the 16 @@ -300,7 +292,7 @@ public static String getRandomColor() { public void setCornerRadius(int radius) { this.cornerRadius = radius; - if(isDataPopulated) { + if (isDataPopulated) { containerLayout.removeAllViews(); populateBarView(animationType, animationDuration); } @@ -309,34 +301,42 @@ public void setCornerRadius(int radius) { /** * setters and getters */ + public int getAnimationType() { + return animationType; + } - public int getAnimationType(){ return animationType; } - - public void setAnimationType(int animationType){ + public void setAnimationType(int animationType) { this.animationType = animationType; } - public int getAnimationDuration(){ return animationDuration; } + public int getAnimationDuration() { + return animationDuration; + } - public void setAnimationDuration(int animationDuration){ + public void setAnimationDuration(int animationDuration) { this.animationDuration = animationDuration; } public int getBarMargin() { return barMargin; } + public void setBarMargin(int barMargin) { this.barMargin = barMargin; } + public int getVerticalSpacing() { return verticalSpacing; } + public void setVerticalSpacing(int verticalSpacing) { this.verticalSpacing = verticalSpacing; } + public int getBarHeight() { return barHeight; } + public void setBarHeight(int barHeight) { this.barHeight = barHeight; } @@ -372,7 +372,7 @@ public String getValueTextColor() { public void setValueTextColor(String valueTextColor) { this.valueTextColor = valueTextColor; } - + public String getRippleColor() { return rippleColor; } @@ -380,5 +380,4 @@ public String getRippleColor() { public void setRippleColor(String rippleColor) { this.rippleColor = rippleColor; } - } diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 0c11bb5..59313ae 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -3,13 +3,13 @@ - - + + - - + + - + diff --git a/library/src/main/res/values/styles.xml b/library/src/main/res/values/styles.xml index b060bf5..2d66274 100644 --- a/library/src/main/res/values/styles.xml +++ b/library/src/main/res/values/styles.xml @@ -9,7 +9,7 @@ diff --git a/library/src/test/java/in/krharsh17/barview/ExampleUnitTest.java b/library/src/test/java/in/krharsh17/barview/ExampleUnitTest.java index 3589dc0..489d82e 100644 --- a/library/src/test/java/in/krharsh17/barview/ExampleUnitTest.java +++ b/library/src/test/java/in/krharsh17/barview/ExampleUnitTest.java @@ -11,7 +11,7 @@ */ public class ExampleUnitTest { @Test - public void addition_isCorrect() { + public void additionIsCorrect() { assertEquals(4, 2 + 2); } } \ No newline at end of file