From 3c7cb42d73b50e3f31760bebf5eed81c7c7ae7f1 Mon Sep 17 00:00:00 2001 From: Brill Pappin Date: Thu, 10 Dec 2020 10:15:53 -0500 Subject: [PATCH 01/12] Added ability to turn off the text --- .../CircularProgressIndicator.java | 15 ++++++++++++++- .../src/main/res/values/attrs.xml | 3 ++- .../MainActivity.java | 12 ++++++++++++ example/src/main/res/layout/activity_main.xml | 7 +++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java index 28e44e4..95d27c8 100644 --- a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java +++ b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java @@ -94,6 +94,8 @@ public class CircularProgressIndicator extends View { private boolean isFillBackgroundEnabled; + private boolean isShowTextEnabled = true; + @Direction private int direction = DIRECTION_COUNTERCLOCKWISE; @@ -165,6 +167,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) { isAnimationEnabled = a.getBoolean(R.styleable.CircularProgressIndicator_enableProgressAnimation, true); isFillBackgroundEnabled = a.getBoolean(R.styleable.CircularProgressIndicator_fillBackground, false); + isShowTextEnabled = a.getBoolean(R.styleable.CircularProgressIndicator_showText, true); direction = a.getInt(R.styleable.CircularProgressIndicator_direction, DIRECTION_COUNTERCLOCKWISE); @@ -335,7 +338,7 @@ protected void onDraw(Canvas canvas) { drawProgressBackground(canvas); drawProgress(canvas); if (shouldDrawDot) drawDot(canvas); - drawText(canvas); + if(isShowTextEnabled) drawText(canvas); } private void drawProgressBackground(Canvas canvas) { @@ -696,6 +699,16 @@ public boolean isFillBackgroundEnabled() { return isFillBackgroundEnabled; } + public void setShowTextEnabled(boolean enabled) { + if (isShowTextEnabled = enabled) return; + isShowTextEnabled = enabled; + invalidateEverything(); + } + + public boolean isShowTextEnabled() { + return isShowTextEnabled; + } + public void setInterpolator(@NonNull Interpolator interpolator) { animationInterpolator = interpolator; } diff --git a/circularprogressindicator/src/main/res/values/attrs.xml b/circularprogressindicator/src/main/res/values/attrs.xml index 4a6986e..9622338 100644 --- a/circularprogressindicator/src/main/res/values/attrs.xml +++ b/circularprogressindicator/src/main/res/values/attrs.xml @@ -31,6 +31,7 @@ + @@ -42,4 +43,4 @@ - \ No newline at end of file + diff --git a/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java b/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java index 84f7c97..2b413c4 100644 --- a/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java +++ b/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java @@ -76,6 +76,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { } }); CheckBox fillBackground = findViewById(R.id.cb_fill_background); + fillBackground.setChecked(circularProgress.isFillBackgroundEnabled()); fillBackground.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { @@ -83,6 +84,17 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { } }); + CheckBox showText = findViewById(R.id.cb_show_text); + showText.setChecked(circularProgress.isShowTextEnabled()); + showText.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + circularProgress.setShowTextEnabled(isChecked); + } + }); + + + RadioGroup progressCap = findViewById(R.id.rg_cap); progressCap.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override diff --git a/example/src/main/res/layout/activity_main.xml b/example/src/main/res/layout/activity_main.xml index e050f43..f3603a6 100644 --- a/example/src/main/res/layout/activity_main.xml +++ b/example/src/main/res/layout/activity_main.xml @@ -111,6 +111,13 @@ android:layout_marginLeft="8dp" android:text="Fill background" /> + + Date: Thu, 17 Dec 2020 16:19:43 -0500 Subject: [PATCH 02/12] change to android x instead of android.support --- circularprogressindicator/build.gradle | 2 +- .../CircularProgressIndicator.java | 12 ++++++------ .../PatternProgressTextAdapter.java | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/circularprogressindicator/build.gradle b/circularprogressindicator/build.gradle index 00cf015..96cfd2c 100644 --- a/circularprogressindicator/build.gradle +++ b/circularprogressindicator/build.gradle @@ -23,5 +23,5 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.android.support:support-annotations:27.1.1' + implementation "androidx.annotation:annotation:1.1.0" } diff --git a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java index 28e44e4..71fbdfc 100644 --- a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java +++ b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java @@ -18,12 +18,12 @@ import android.graphics.Shader; import android.graphics.SweepGradient; import android.os.Build; -import android.support.annotation.ColorInt; -import android.support.annotation.Dimension; -import android.support.annotation.IntDef; -import android.support.annotation.IntRange; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; +import androidx.annotation.ColorInt; +import androidx.annotation.Dimension; +import androidx.annotation.IntDef; +import androidx.annotation.IntRange; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import android.text.TextPaint; import android.util.AttributeSet; import android.util.DisplayMetrics; diff --git a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/PatternProgressTextAdapter.java b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/PatternProgressTextAdapter.java index 617344c..ac90f0a 100644 --- a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/PatternProgressTextAdapter.java +++ b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/PatternProgressTextAdapter.java @@ -1,6 +1,6 @@ package antonkozyriatskyi.circularprogressindicator; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; /** * Created by Anton on 06.06.2018. From 431d1223fd7b7281d6c24742e3ce07924cb1ae84 Mon Sep 17 00:00:00 2001 From: Nicolas Marchal <22968076+nicolasmarchal@users.noreply.github.com> Date: Sat, 13 Nov 2021 11:30:15 +0100 Subject: [PATCH 03/12] Change animation duration setter & getter --- .../CircularProgressIndicator.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java index 28e44e4..e1ecdd4 100644 --- a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java +++ b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java @@ -94,6 +94,8 @@ public class CircularProgressIndicator extends View { private boolean isFillBackgroundEnabled; + private int animationDuration = DEFAULT_ANIMATION_DURATION; + @Direction private int direction = DIRECTION_COUNTERCLOCKWISE; @@ -417,7 +419,7 @@ public Double evaluate(float fraction, Double startValue, Double endValue) { return (startValue + (endValue - startValue) * fraction); } }, oldCurrentProgress, progressValue); - progressAnimator.setDuration(DEFAULT_ANIMATION_DURATION); + progressAnimator.setDuration(animationDuration); progressAnimator.setValues(angleProperty); progressAnimator.setInterpolator(animationInterpolator); progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @@ -696,6 +698,13 @@ public boolean isFillBackgroundEnabled() { return isFillBackgroundEnabled; } + public void setAnimationDuration(int duration) { + animationDuration = duration; + } + + + public int getAnimationDuration() { return animationDuration; } + public void setInterpolator(@NonNull Interpolator interpolator) { animationInterpolator = interpolator; } From fe91fbc1431a6dcae53a612fd331e37d2da8184c Mon Sep 17 00:00:00 2001 From: Daniel Pichl Date: Thu, 10 Mar 2022 12:16:24 +0100 Subject: [PATCH 04/12] Updated to the 21st century --- build.gradle | 2 +- circularprogressindicator/build.gradle | 10 +- .../CircularProgressIndicator.java | 36 ++-- .../DefaultProgressTextAdapter.java | 3 + .../PatternProgressTextAdapter.java | 2 +- example/build.gradle | 8 +- example/src/main/AndroidManifest.xml | 4 +- .../ColorPickerDialogFragment.java | 54 +++--- .../MainActivity.java | 159 ++++++------------ example/src/main/res/layout/activity_main.xml | 18 +- gradle.properties | 5 + gradle/wrapper/gradle-wrapper.properties | 6 +- 12 files changed, 130 insertions(+), 177 deletions(-) diff --git a/build.gradle b/build.gradle index 0dec223..4f923ee 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.3' + classpath 'com.android.tools.build:gradle:7.1.2' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' // NOTE: Do not place your application dependencies here; they belong diff --git a/circularprogressindicator/build.gradle b/circularprogressindicator/build.gradle index 96cfd2c..ab844b9 100644 --- a/circularprogressindicator/build.gradle +++ b/circularprogressindicator/build.gradle @@ -1,15 +1,11 @@ apply plugin: 'com.android.library' -apply plugin: 'com.github.dcendents.android-maven' -group='com.github.antonKozyriatskyi.CircularProgressIndicator' +//apply plugin: 'com.github.dcendents.android-maven' android { - compileSdkVersion 27 + compileSdkVersion 31 defaultConfig { minSdkVersion 15 - targetSdkVersion 27 - versionCode 1 - versionName "1.0" } buildTypes { @@ -23,5 +19,5 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation "androidx.annotation:annotation:1.1.0" + implementation 'androidx.annotation:annotation:1.3.0' } diff --git a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java index 4b5743c..d7bb0bd 100644 --- a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java +++ b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java @@ -4,6 +4,7 @@ import android.animation.PropertyValuesHolder; import android.animation.TypeEvaluator; import android.animation.ValueAnimator; +import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; @@ -39,7 +40,7 @@ * Created by Anton on 03.03.2018. */ -@SuppressWarnings("FieldCanBeLocal") +@SuppressWarnings({"FieldCanBeLocal", "unused"}) public class CircularProgressIndicator extends View { public static final int DIRECTION_CLOCKWISE = 0; @@ -103,6 +104,7 @@ public class CircularProgressIndicator extends View { private ValueAnimator progressAnimator; + @SuppressWarnings("NotNullFieldNotInitialized") // initialized in init method @NonNull private ProgressTextAdapter progressTextAdapter; @@ -112,6 +114,9 @@ public class CircularProgressIndicator extends View { @NonNull private Interpolator animationInterpolator = new AccelerateDecelerateInterpolator(); + @NonNull + private final Rect textBoundsRect = new Rect(); + public CircularProgressIndicator(Context context) { super(context); init(context, null); @@ -193,12 +198,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) { throw new IllegalArgumentException("did you forget to specify gradientColorEnd?"); } - post(new Runnable() { - @Override - public void run() { - setGradient(gradientType, gradientColorEnd); - } - }); + post(() -> setGradient(gradientType, gradientColorEnd)); } a.recycle(); @@ -234,6 +234,7 @@ public void run() { circleBounds = new RectF(); } + @SuppressLint("SwitchIntDef") @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); @@ -249,10 +250,8 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); - Rect textBoundsRect = new Rect(); textPaint.getTextBounds(progressText, 0, progressText.length(), textBoundsRect); - float dotWidth = dotPaint.getStrokeWidth(); float progressWidth = progressPaint.getStrokeWidth(); float progressBackgroundWidth = progressBackgroundPaint.getStrokeWidth(); @@ -416,21 +415,14 @@ public void setProgress(double current, double max) { private void startProgressAnimation(double oldCurrentProgress, final double finalAngle) { final PropertyValuesHolder angleProperty = PropertyValuesHolder.ofInt(PROPERTY_ANGLE, sweepAngle, (int) finalAngle); - progressAnimator = ValueAnimator.ofObject(new TypeEvaluator() { - @Override - public Double evaluate(float fraction, Double startValue, Double endValue) { - return (startValue + (endValue - startValue) * fraction); - } - }, oldCurrentProgress, progressValue); + progressAnimator = ValueAnimator.ofObject((TypeEvaluator) + (fraction, startValue, endValue) -> (startValue + (endValue - startValue) * fraction), oldCurrentProgress, progressValue); progressAnimator.setDuration(animationDuration); progressAnimator.setValues(angleProperty); progressAnimator.setInterpolator(animationInterpolator); - progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { - @Override - public void onAnimationUpdate(ValueAnimator animation) { - sweepAngle = (int) animation.getAnimatedValue(PROPERTY_ANGLE); - invalidate(); - } + progressAnimator.addUpdateListener(animation -> { + sweepAngle = (int) animation.getAnimatedValue(PROPERTY_ANGLE); + invalidate(); }); progressAnimator.addListener(new DefaultAnimatorListener() { @Override @@ -744,6 +736,8 @@ public void setGradient(@GradientType int type, @ColorInt int endColor) { case SWEEP_GRADIENT: gradient = new SweepGradient(cx, cy, new int[]{startColor, endColor}, null); break; + case NO_GRADIENT: + return; } if (gradient != null) { diff --git a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/DefaultProgressTextAdapter.java b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/DefaultProgressTextAdapter.java index acd8375..e515646 100644 --- a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/DefaultProgressTextAdapter.java +++ b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/DefaultProgressTextAdapter.java @@ -1,11 +1,14 @@ package antonkozyriatskyi.circularprogressindicator; +import androidx.annotation.NonNull; + /** * Created by Anton on 06.06.2018. */ public final class DefaultProgressTextAdapter implements CircularProgressIndicator.ProgressTextAdapter { + @NonNull @Override public String formatText(double currentProgress) { return String.valueOf((int) currentProgress); diff --git a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/PatternProgressTextAdapter.java b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/PatternProgressTextAdapter.java index ac90f0a..037a5d4 100644 --- a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/PatternProgressTextAdapter.java +++ b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/PatternProgressTextAdapter.java @@ -8,7 +8,7 @@ public final class PatternProgressTextAdapter implements CircularProgressIndicator.ProgressTextAdapter { - private String pattern; + private final String pattern; public PatternProgressTextAdapter(String pattern) { this.pattern = pattern; diff --git a/example/build.gradle b/example/build.gradle index 924b8a7..dac7527 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -1,11 +1,11 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 27 + compileSdkVersion 31 defaultConfig { applicationId "antonkozyriatskyi.circularprogressindicatorexample" minSdkVersion 15 - targetSdkVersion 27 + targetSdkVersion 31 versionCode 1 versionName "1.0" } @@ -21,6 +21,6 @@ dependencies { implementation project(':circularprogressindicator') implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.android.support:appcompat-v7:27.1.0' - implementation 'com.android.support:design:27.1.0' + implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'com.google.android.material:material:1.5.0' } diff --git a/example/src/main/AndroidManifest.xml b/example/src/main/AndroidManifest.xml index 580474d..8e3b35b 100644 --- a/example/src/main/AndroidManifest.xml +++ b/example/src/main/AndroidManifest.xml @@ -10,7 +10,9 @@ android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> - + diff --git a/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/ColorPickerDialogFragment.java b/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/ColorPickerDialogFragment.java index 8790663..7b6805d 100644 --- a/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/ColorPickerDialogFragment.java +++ b/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/ColorPickerDialogFragment.java @@ -4,15 +4,17 @@ import android.graphics.PorterDuff; import android.os.Build; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.design.widget.BottomSheetDialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.SeekBar; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.google.android.material.bottomsheet.BottomSheetDialogFragment; + /** * Created by Anton on 13.03.2018. */ @@ -55,25 +57,22 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { colorResult.setBackgroundColor(Color.rgb(redProgress, greenProgress, blueProgress)); - switch (seekBar.getId()) { - case R.id.sb_red: - seekBar.getProgressDrawable().setColorFilter(Color.rgb(redProgress, 0, 0), mode); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - seekBar.getThumb().setColorFilter(Color.rgb(redProgress, 0, 0), mode); - } - break; - case R.id.sb_green: - seekBar.getProgressDrawable().setColorFilter(Color.rgb(0, greenProgress, 0), mode); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - seekBar.getThumb().setColorFilter(Color.rgb(0, greenProgress, 0), mode); - } - break; - case R.id.sb_blue: - seekBar.getProgressDrawable().setColorFilter(Color.rgb(0, 0, blueProgress), mode); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - seekBar.getThumb().setColorFilter(Color.rgb(0, 0, blueProgress), mode); - } - break; + int id = seekBar.getId(); + if (id == R.id.sb_red) { + seekBar.getProgressDrawable().setColorFilter(Color.rgb(redProgress, 0, 0), mode); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + seekBar.getThumb().setColorFilter(Color.rgb(redProgress, 0, 0), mode); + } + } else if (id == R.id.sb_green) { + seekBar.getProgressDrawable().setColorFilter(Color.rgb(0, greenProgress, 0), mode); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + seekBar.getThumb().setColorFilter(Color.rgb(0, greenProgress, 0), mode); + } + } else if (id == R.id.sb_blue) { + seekBar.getProgressDrawable().setColorFilter(Color.rgb(0, 0, blueProgress), mode); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + seekBar.getThumb().setColorFilter(Color.rgb(0, 0, blueProgress), mode); + } } } }; @@ -82,13 +81,10 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { green.setOnSeekBarChangeListener(seekBarChangeListener); blue.setOnSeekBarChangeListener(seekBarChangeListener); - selectColor.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - onColorSelectedListener.onColorChosen(ColorPickerDialogFragment.this, - red.getProgress(), green.getProgress(), blue.getProgress()); - dismiss(); - } + selectColor.setOnClickListener(v -> { + onColorSelectedListener.onColorChosen(ColorPickerDialogFragment.this, + red.getProgress(), green.getProgress(), blue.getProgress()); + dismiss(); }); return rootView; diff --git a/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java b/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java index 2b413c4..6a291f7 100644 --- a/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java +++ b/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java @@ -2,18 +2,18 @@ import android.graphics.Color; import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.CheckBox; -import android.widget.CompoundButton; import android.widget.RadioGroup; import android.widget.SeekBar; import android.widget.SimpleAdapter; import android.widget.Spinner; -import android.widget.Switch; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.SwitchCompat; import java.util.ArrayList; import java.util.HashMap; @@ -60,70 +60,36 @@ protected void onCreate(Bundle savedInstanceState) { dotWidth.setOnSeekBarChangeListener(this); CheckBox drawDot = findViewById(R.id.cb_draw_dot); - drawDot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - circularProgress.setShouldDrawDot(isChecked); - dotWidth.setEnabled(isChecked); - dotColor.setEnabled(isChecked); - } + drawDot.setOnCheckedChangeListener((buttonView, isChecked) -> { + circularProgress.setShouldDrawDot(isChecked); + dotWidth.setEnabled(isChecked); + dotColor.setEnabled(isChecked); }); CheckBox useCustomTextAdapter = findViewById(R.id.cb_custom_text_adapter); - useCustomTextAdapter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - circularProgress.setProgressTextAdapter(isChecked ? TIME_TEXT_ADAPTER : null); - } - }); + useCustomTextAdapter.setOnCheckedChangeListener((buttonView, isChecked) -> circularProgress.setProgressTextAdapter(isChecked ? TIME_TEXT_ADAPTER : null)); CheckBox fillBackground = findViewById(R.id.cb_fill_background); fillBackground.setChecked(circularProgress.isFillBackgroundEnabled()); - fillBackground.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - circularProgress.setFillBackgroundEnabled(isChecked); - } - }); + fillBackground.setOnCheckedChangeListener((buttonView, isChecked) -> circularProgress.setFillBackgroundEnabled(isChecked)); CheckBox showText = findViewById(R.id.cb_show_text); showText.setChecked(circularProgress.isShowTextEnabled()); - showText.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - circularProgress.setShowTextEnabled(isChecked); - } - }); + showText.setOnCheckedChangeListener((buttonView, isChecked) -> circularProgress.setShowTextEnabled(isChecked)); RadioGroup progressCap = findViewById(R.id.rg_cap); - progressCap.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(RadioGroup group, int checkedId) { - switch (checkedId) { - case R.id.rb_cap_butt: - circularProgress.setProgressStrokeCap(CircularProgressIndicator.CAP_BUTT); - break; - case R.id.rb_cap_round: - circularProgress.setProgressStrokeCap(CircularProgressIndicator.CAP_ROUND); - break; - } + progressCap.setOnCheckedChangeListener((group, checkedId) -> { + if (checkedId == R.id.rb_cap_butt) { + circularProgress.setProgressStrokeCap(CircularProgressIndicator.CAP_BUTT); + } else if (checkedId == R.id.rb_cap_round) { + circularProgress.setProgressStrokeCap(CircularProgressIndicator.CAP_ROUND); } }); - circularProgress.setOnProgressChangeListener(new CircularProgressIndicator.OnProgressChangeListener() { - @Override - public void onProgressChanged(double progress, double maxProgress) { - Log.d("PROGRESS", String.format("Current: %1$.0f, max: %2$.0f", progress, maxProgress)); - } - }); + circularProgress.setOnProgressChangeListener((progress1, maxProgress) -> Log.d("PROGRESS", String.format("Current: %1$.0f, max: %2$.0f", progress1, maxProgress))); - Switch animationSwitch = findViewById(R.id.sw_enable_animation); - animationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - circularProgress.setAnimationEnabled(isChecked); - } - }); + SwitchCompat animationSwitch = findViewById(R.id.sw_enable_animation); + animationSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> circularProgress.setAnimationEnabled(isChecked)); Spinner gradientType = findViewById(R.id.sp_gradient_type); @@ -148,7 +114,6 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { gradient.put("value", "3"); gradients.add(gradient); - gradientType.setAdapter( new SimpleAdapter( this, @@ -175,19 +140,15 @@ public void onClick(View v) { ColorPickerDialogFragment dialog = new ColorPickerDialogFragment(); dialog.setOnColorSelectedListener(this); String tag = null; - switch (v.getId()) { - case R.id.btn_progress_color: - tag = "progressColor"; - break; - case R.id.btn_background_color: - tag = "progressBackgroundColor"; - break; - case R.id.btn_text_color: - tag = "textColor"; - break; - case R.id.btn_dot_color: - tag = "dotColor"; - break; + int id = v.getId(); + if (id == R.id.btn_progress_color) { + tag = "progressColor"; + } else if (id == R.id.btn_background_color) { + tag = "progressBackgroundColor"; + } else if (id == R.id.btn_text_color) { + tag = "textColor"; + } else if (id == R.id.btn_dot_color) { + tag = "dotColor"; } dialog.show(getSupportFragmentManager(), tag); @@ -195,22 +156,17 @@ public void onClick(View v) { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - switch (seekBar.getId()) { - case R.id.sb_progress: - circularProgress.setCurrentProgress(progress); - break; - case R.id.sb_progress_width: - circularProgress.setProgressStrokeWidthDp(progress); - break; - case R.id.sb_dot_width: - circularProgress.setDotWidthDp(progress); - break; - case R.id.sb_text_size: - circularProgress.setTextSizeSp(progress); - break; - case R.id.sb_progress_background_width: - circularProgress.setProgressBackgroundStrokeWidthDp(progress); - break; + int id = seekBar.getId(); + if (id == R.id.sb_progress) { + circularProgress.setCurrentProgress(progress); + } else if (id == R.id.sb_progress_width) { + circularProgress.setProgressStrokeWidthDp(progress); + } else if (id == R.id.sb_dot_width) { + circularProgress.setDotWidthDp(progress); + } else if (id == R.id.sb_text_size) { + circularProgress.setTextSizeSp(progress); + } else if (id == R.id.sb_progress_background_width) { + circularProgress.setProgressBackgroundStrokeWidthDp(progress); } } @@ -245,27 +201,24 @@ public void onColorChosen(ColorPickerDialogFragment dialog, int r, int g, int b) } } - private static final CircularProgressIndicator.ProgressTextAdapter TIME_TEXT_ADAPTER = new CircularProgressIndicator.ProgressTextAdapter() { - @Override - public String formatText(double time) { - int hours = (int) (time / 3600); - time %= 3600; - int minutes = (int) (time / 60); - int seconds = (int) (time % 60); - StringBuilder sb = new StringBuilder(); - if (hours < 10) { - sb.append(0); - } - sb.append(hours).append(":"); - if (minutes < 10) { - sb.append(0); - } - sb.append(minutes).append(":"); - if (seconds < 10) { - sb.append(0); - } - sb.append(seconds); - return sb.toString(); + private static final CircularProgressIndicator.ProgressTextAdapter TIME_TEXT_ADAPTER = time -> { + int hours = (int) (time / 3600); + time %= 3600; + int minutes = (int) (time / 60); + int seconds = (int) (time % 60); + StringBuilder sb = new StringBuilder(); + if (hours < 10) { + sb.append(0); + } + sb.append(hours).append(":"); + if (minutes < 10) { + sb.append(0); + } + sb.append(minutes).append(":"); + if (seconds < 10) { + sb.append(0); } + sb.append(seconds); + return sb.toString(); }; } diff --git a/example/src/main/res/layout/activity_main.xml b/example/src/main/res/layout/activity_main.xml index f3603a6..eab4406 100644 --- a/example/src/main/res/layout/activity_main.xml +++ b/example/src/main/res/layout/activity_main.xml @@ -5,15 +5,16 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" - tools:context="antonkozyriatskyi.circularprogressindicatorexample.MainActivity"> + tools:context="antonkozyriatskyi.circularprogressindicatorexample.MainActivity" + tools:ignore="HardcodedText"> - @@ -101,6 +102,7 @@ android:id="@+id/cb_custom_text_adapter" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:text="Use custom text adapter (HH:MM:SS)" /> @@ -108,6 +110,7 @@ android:id="@+id/cb_fill_background" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:text="Fill background" /> @@ -115,6 +118,7 @@ android:id="@+id/cb_show_text" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:text="Show Text" /> @@ -136,8 +140,8 @@ - + android:layout_height="wrap_content" />