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/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 fa2b9c0..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; /** @@ -40,9 +39,6 @@ class BarGroup extends ConstraintLayout implements Constants { private View initial; private Bar bar; private TextView value; - private ConstraintSet constraintSet; - private LayoutParams labelParams; - private LayoutParams initialParams; private String labelText; private String color; private String valueText; @@ -55,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 @@ -121,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 @@ -136,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, @@ -172,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); @@ -191,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: @@ -244,18 +237,20 @@ protected void onAttachedToWindow() { * Initializer function for the label segment */ private void setupLabel() { - labelParams = new Constraints.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT); + 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); @@ -264,23 +259,23 @@ private void setupLabel() { /** * Initializer function for the initial block - * */ private void setupInitial() { - 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;//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); @@ -290,55 +285,51 @@ private 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 - * */ private void setupValue() { value.setText(valueText); @@ -349,7 +340,7 @@ private 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); } @@ -365,7 +356,6 @@ private void setupValue() { * * @param name * @param context - * @return */ public static Typeface get(String name, Context context) { Typeface tf = fontCache.get(name); @@ -382,10 +372,9 @@ public static Typeface get(String name, Context context) { /** * Sets constraints for all pieces of a BarGroup instance - * */ private void applyConstraints() { - constraintSet = new ConstraintSet(); + ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(this); constraintSet.setHorizontalBias(initial.getId(), 0.30f); @@ -407,7 +396,6 @@ private 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); } @@ -415,7 +403,6 @@ private void applyConstraints() { * Converts density independent pixel units (dp) to pixel units (px) * * @param dp - * @return */ private int dp(float dp) { return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics())); @@ -429,11 +416,10 @@ private int dp(float dp) { * @param currentLayer * @param totalLayer * @param radius - * @return */ - public Drawable getRoundRect(String color,int currentLayer,int totalLayer,int radius) { + public Drawable getRoundRect(String color, int currentLayer, int totalLayer, int radius) { - int localRadius=radius; + int localRadius = radius; int leftTopPadding = 1; int rightPadding = 3; int bottomPadding = 3; @@ -441,43 +427,42 @@ public Drawable getRoundRect(String color,int currentLayer,int totalLayer,int ra int increment = 16 / (totalLayer - 2); int layernumber = 0; - - if(radius<0){ + if (radius < 0) { localRadius = 0; } - if(radius>20){ + if (radius > 20) { localRadius = 20; } - if(currentLayer%3==0){ + 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[]{ - localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 2, - localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 2 + localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 2, + localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 2 }, null, null); if (totalLayer == currentLayer) { if (localRadius == 0) { localRadius = 2; } rectShape = new RoundRectShape(new float[]{ - localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 2, - localRadius * 2, localRadius * 2, localRadius * 2, localRadius * 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); @@ -485,8 +470,6 @@ public Drawable getRoundRect(String color,int currentLayer,int totalLayer,int ra return shapeDrawable; } - - /** * Animator function for the 'expand' intro animation * @@ -516,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"); } @@ -579,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) { @@ -588,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 a50e1f7..0867ee5 100644 --- a/library/src/main/java/in/krharsh17/barview/BarView.java +++ b/library/src/main/java/in/krharsh17/barview/BarView.java @@ -22,7 +22,6 @@ 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; diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index 471dc81..256cef7 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -3,22 +3,22 @@ - - + + - - + + - + - - - - - - - - - + + + + + + + + + \ No newline at end of file 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 @@