From e96630ad1f3fb05e6a90052402c5699246621eb7 Mon Sep 17 00:00:00 2001 From: RichyHBM Date: Wed, 20 Feb 2019 21:37:30 +0000 Subject: [PATCH 1/3] Allow setting font to use for central text --- circularprogressindicator/build.gradle | 1 + .../CircularProgressIndicator.java | 41 ++++++++++++++++++- .../src/main/res/values/attrs.xml | 1 + 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/circularprogressindicator/build.gradle b/circularprogressindicator/build.gradle index 00cf015..623537c 100644 --- a/circularprogressindicator/build.gradle +++ b/circularprogressindicator/build.gradle @@ -24,4 +24,5 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:support-annotations:27.1.1' + implementation 'com.android.support:support-compat:27.1.1' } diff --git a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java index 28e44e4..f7723a9 100644 --- a/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java +++ b/circularprogressindicator/src/main/java/antonkozyriatskyi/circularprogressindicator/CircularProgressIndicator.java @@ -6,6 +6,7 @@ import android.animation.ValueAnimator; import android.annotation.TargetApi; import android.content.Context; +import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; @@ -17,6 +18,7 @@ import android.graphics.RectF; import android.graphics.Shader; import android.graphics.SweepGradient; +import android.graphics.Typeface; import android.os.Build; import android.support.annotation.ColorInt; import android.support.annotation.Dimension; @@ -24,6 +26,7 @@ import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.content.res.ResourcesCompat; import android.text.TextPaint; import android.util.AttributeSet; import android.util.DisplayMetrics; @@ -83,6 +86,18 @@ public class CircularProgressIndicator extends View { private float textX; private float textY; + private ResourcesCompat.FontCallback fontCallback = new ResourcesCompat.FontCallback() { + @Override + public void onFontRetrieved(@NonNull Typeface typeface) { + setTextFont(typeface); + } + + @Override + public void onFontRetrievalFailed(int reason) { + + } + }; + private float radius; private boolean shouldDrawDot; @@ -154,6 +169,17 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) { textColor = a.getColor(R.styleable.CircularProgressIndicator_textColor, progressColor); textSize = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_textSize, textSize); + String fontPath = a.getString(R.styleable.CircularProgressIndicator_textFont); + if (fontPath != null && !fontPath.isEmpty()) { + try { + int fontResourceId = a.getResourceId(R.styleable.CircularProgressIndicator_textFont, 0); + if(fontResourceId != 0) { + ResourcesCompat.getFont(getContext(), fontResourceId, fontCallback, null); + } + } catch (Resources.NotFoundException ignored) { + } + } + shouldDrawDot = a.getBoolean(R.styleable.CircularProgressIndicator_drawDot, true); dotColor = a.getColor(R.styleable.CircularProgressIndicator_dotColor, progressColor); dotWidth = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_dotWidth, progressStrokeWidth); @@ -507,8 +533,7 @@ public void setProgressBackgroundStrokeWidthPx(@Dimension int strokeWidth) { public void setTextColor(@ColorInt int color) { textPaint.setColor(color); - Rect textRect = new Rect(); - textPaint.getTextBounds(progressText, 0, progressText.length(), textRect); + Rect textRect = calculateTextBounds(); invalidate(textRect); } @@ -535,6 +560,14 @@ public void setTextSizePx(@Dimension int size) { invalidate(textBounds); } + public void setTextFont(Typeface font) { + textPaint.setTypeface(font); + + Rect textRect = calculateTextBounds(); + + invalidate(textRect); + } + public void setShouldDrawDot(boolean shouldDrawDot) { this.shouldDrawDot = shouldDrawDot; @@ -607,6 +640,10 @@ public float getTextSize() { return textPaint.getTextSize(); } + public Typeface getTextFont() { + return textPaint.getTypeface(); + } + public boolean isDotEnabled() { return shouldDrawDot; diff --git a/circularprogressindicator/src/main/res/values/attrs.xml b/circularprogressindicator/src/main/res/values/attrs.xml index 4a6986e..057b7be 100644 --- a/circularprogressindicator/src/main/res/values/attrs.xml +++ b/circularprogressindicator/src/main/res/values/attrs.xml @@ -9,6 +9,7 @@ + From 2f6668c3a3b204f80d2677ec433a7dbd716f7fe6 Mon Sep 17 00:00:00 2001 From: RichyHBM Date: Wed, 20 Feb 2019 21:37:43 +0000 Subject: [PATCH 2/3] Add fonts to sample application --- example/src/main/AndroidManifest.xml | 3 ++ .../MainActivity.java | 43 +++++++++++++++++++ .../src/main/res/font/architects_daughter.xml | 8 ++++ example/src/main/res/font/open_sans_light.xml | 8 ++++ example/src/main/res/font/vt323.xml | 8 ++++ example/src/main/res/layout/activity_main.xml | 11 +++++ example/src/main/res/values/font_certs.xml | 17 ++++++++ .../src/main/res/values/preloaded_fonts.xml | 8 ++++ 8 files changed, 106 insertions(+) create mode 100644 example/src/main/res/font/architects_daughter.xml create mode 100644 example/src/main/res/font/open_sans_light.xml create mode 100644 example/src/main/res/font/vt323.xml create mode 100644 example/src/main/res/values/font_certs.xml create mode 100644 example/src/main/res/values/preloaded_fonts.xml diff --git a/example/src/main/AndroidManifest.xml b/example/src/main/AndroidManifest.xml index 580474d..973bf2c 100644 --- a/example/src/main/AndroidManifest.xml +++ b/example/src/main/AndroidManifest.xml @@ -17,6 +17,9 @@ + \ 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..08b912e 100644 --- a/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java +++ b/example/src/main/java/antonkozyriatskyi/circularprogressindicatorexample/MainActivity.java @@ -1,11 +1,15 @@ package antonkozyriatskyi.circularprogressindicatorexample; +import android.content.Context; import android.graphics.Color; +import android.graphics.Typeface; import android.os.Bundle; +import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; @@ -17,6 +21,7 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import antonkozyriatskyi.circularprogressindicator.CircularProgressIndicator; @@ -156,6 +161,44 @@ public void onItemSelected(AdapterView parent, View view, int position, long public void onNothingSelected(AdapterView parent) { } }); + + Spinner spinner = findViewById(R.id.sb_text_font); + + final Context context = this; + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + switch (position) { + case 0: + circularProgress.setTextFont(Typeface.DEFAULT); + break; + case 1: + circularProgress.setTextFont(ResourcesCompat.getFont(context, R.font.architects_daughter)); + break; + case 2: + circularProgress.setTextFont(ResourcesCompat.getFont(context, R.font.open_sans_light)); + break; + case 3: + circularProgress.setTextFont(ResourcesCompat.getFont(context, R.font.vt323)); + break; + } + } + + @Override + public void onNothingSelected(AdapterView parent) { + } + }); + + // Spinner Drop down elements + List fonts = new ArrayList(); + fonts.add("Default"); + fonts.add("Architects Daughter"); + fonts.add("Open Sans Light"); + fonts.add("VT323"); + + ArrayAdapter dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, fonts); + dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(dataAdapter); } @Override diff --git a/example/src/main/res/font/architects_daughter.xml b/example/src/main/res/font/architects_daughter.xml new file mode 100644 index 0000000..b723214 --- /dev/null +++ b/example/src/main/res/font/architects_daughter.xml @@ -0,0 +1,8 @@ + + + diff --git a/example/src/main/res/font/open_sans_light.xml b/example/src/main/res/font/open_sans_light.xml new file mode 100644 index 0000000..036371f --- /dev/null +++ b/example/src/main/res/font/open_sans_light.xml @@ -0,0 +1,8 @@ + + + diff --git a/example/src/main/res/font/vt323.xml b/example/src/main/res/font/vt323.xml new file mode 100644 index 0000000..3ecc47a --- /dev/null +++ b/example/src/main/res/font/vt323.xml @@ -0,0 +1,8 @@ + + + diff --git a/example/src/main/res/layout/activity_main.xml b/example/src/main/res/layout/activity_main.xml index e050f43..2ebea95 100644 --- a/example/src/main/res/layout/activity_main.xml +++ b/example/src/main/res/layout/activity_main.xml @@ -57,6 +57,17 @@ android:min="8" android:progress="24" /> + + + + + + + @array/com_google_android_gms_fonts_certs_dev + @array/com_google_android_gms_fonts_certs_prod + + + + MIIEqDCCA5CgAwIBAgIJANWFuGx90071MA0GCSqGSIb3DQEBBAUAMIGUMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEQMA4GA1UEChMHQW5kcm9pZDEQMA4GA1UECxMHQW5kcm9pZDEQMA4GA1UEAxMHQW5kcm9pZDEiMCAGCSqGSIb3DQEJARYTYW5kcm9pZEBhbmRyb2lkLmNvbTAeFw0wODA0MTUyMzM2NTZaFw0zNTA5MDEyMzM2NTZaMIGUMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEQMA4GA1UEChMHQW5kcm9pZDEQMA4GA1UECxMHQW5kcm9pZDEQMA4GA1UEAxMHQW5kcm9pZDEiMCAGCSqGSIb3DQEJARYTYW5kcm9pZEBhbmRyb2lkLmNvbTCCASAwDQYJKoZIhvcNAQEBBQADggENADCCAQgCggEBANbOLggKv+IxTdGNs8/TGFy0PTP6DHThvbbR24kT9ixcOd9W+EaBPWW+wPPKQmsHxajtWjmQwWfna8mZuSeJS48LIgAZlKkpFeVyxW0qMBujb8X8ETrWy550NaFtI6t9+u7hZeTfHwqNvacKhp1RbE6dBRGWynwMVX8XW8N1+UjFaq6GCJukT4qmpN2afb8sCjUigq0GuMwYXrFVee74bQgLHWGJwPmvmLHC69EH6kWr22ijx4OKXlSIx2xT1AsSHee70w5iDBiK4aph27yH3TxkXy9V89TDdexAcKk/cVHYNnDBapcavl7y0RiQ4biu8ymM8Ga/nmzhRKya6G0cGw8CAQOjgfwwgfkwHQYDVR0OBBYEFI0cxb6VTEM8YYY6FbBMvAPyT+CyMIHJBgNVHSMEgcEwgb6AFI0cxb6VT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main/res/values/preloaded_fonts.xml new file mode 100644 index 0000000..acd9d47 --- /dev/null +++ b/example/src/main/res/values/preloaded_fonts.xml @@ -0,0 +1,8 @@ + + + + @font/architects_daughter + @font/open_sans_light + @font/vt323 + + From 45da0d8eaebe3624d7dac8c5005adbf6c2169a7f Mon Sep 17 00:00:00 2001 From: RichyHBM Date: Wed, 20 Feb 2019 21:44:32 +0000 Subject: [PATCH 3/3] Update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2463619..0543c9d 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Add view to your layout: app:startAngle="270" app:textColor="#fffc59" app:textSize="14sp" + app:textFont="@font/open_sans" app:fillBackground="false" app:gradientType="linear" app:gradientEndColor="@color/colorAccent" /> @@ -105,6 +106,7 @@ circularProgress.getMaxProgress() // returns 10000 | Dot width | `app:dotWidth` | setters: `setDotWidthDp(widthInDp)` or `setDotWidthPx(widthInPx)`
getter: `getDotWidth()` (returns width in pixels) | same as progress stroke width | | Progress text size | `app:textSize` | setters: `setTextSizeSp(sizeInSp)` or `setTextSizePx(sizeInPx)`
getter: `getTextSize()` (returns size in pixels) | `24sp` | | Progress text color | `app:textColor` | setter: `setTextColor(textColor)`
getter: `getTextColor()` | same as progress color | +| Progress text font | `app:textFont` | setter: `setTextFont(typeface)`
getter: `getTextFont()` | System default font (Roboto) | | Formatting pattern to be used in `PatternProgressTextAdapter`. Checkout [Formatting progress text](#formatting-progress-text) section. | `app:formattingPattern` | setter: `setProgressTextAdapter(progressTextAdapter)`
getter: `getProgressTextAdapter()` | not specified | | Direction of the progress arc (`clockwise` or `counterclockwise`) | `app:direction` | setter: `setDirection(direction)`
getter: `getDirection()` | `counterclockwise` | | Start angle. Checkout [Start angle](#setting-start-angle) section. | `app:startAngle` | setter: `setStartAngle(startAngle)`
getter: `getStartAngle()` | `270` |