Skip to content

Add Functionality to Set IndeterminateHorizontalProgressDrawable Background Color #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.6.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Feb 24 16:53:08 PST 2019
#Fri Jul 03 13:57:12 EDT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.annotation:annotation:1.0.1'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.annotation:annotation:1.1.0'
}

apply from: '../gradle-mvn-push.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import android.animation.Animator;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
Expand All @@ -16,6 +17,7 @@
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Px;

import me.zhanghai.android.materialprogressbar.internal.ThemeUtils;

/**
Expand Down Expand Up @@ -48,6 +50,8 @@ public class IndeterminateHorizontalProgressDrawable extends BaseIndeterminatePr
@NonNull
private final RectTransformX mRect2TransformX = new RectTransformX(RECT_2_TRANSFORM_X);

private ColorStateList mBackgroundTintList;

/**
* Create a new {@code IndeterminateHorizontalProgressDrawable}.
*
Expand All @@ -62,7 +66,7 @@ public IndeterminateHorizontalProgressDrawable(@NonNull Context context) {

mBackgroundAlpha = ThemeUtils.getFloatFromAttrRes(android.R.attr.disabledAlpha, 0, context);

mAnimators = new Animator[] {
mAnimators = new Animator[]{
Animators.createIndeterminateHorizontalRect1(mRect1TransformX),
Animators.createIndeterminateHorizontalRect2(mRect2TransformX)
};
Expand All @@ -76,6 +80,10 @@ public boolean getShowBackground() {
return mShowBackground;
}

public void setBackgroundTintList(ColorStateList tintList) {
mBackgroundTintList = tintList;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -113,9 +121,15 @@ protected void onDraw(Canvas canvas, int width, int height, Paint paint) {
}

if (mShowBackground) {
paint.setAlpha(Math.round(mAlpha * mBackgroundAlpha));
drawBackgroundRect(canvas, paint);
paint.setAlpha(mAlpha);
if (mBackgroundTintList == null) {
paint.setAlpha(Math.round(mAlpha * mBackgroundAlpha));
drawBackgroundRect(canvas, paint);
paint.setAlpha(mAlpha);
} else {
Paint backgroundPaint = new Paint();
backgroundPaint.setColor(mBackgroundTintList.getDefaultColor());
drawBackgroundRect(canvas, backgroundPaint);
}
}
drawProgressRect(canvas, mRect2TransformX, paint);
drawProgressRect(canvas, mRect1TransformX, paint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.TintTypedArray;

import me.zhanghai.android.materialprogressbar.internal.DrawableCompat;

/**
Expand Down Expand Up @@ -122,6 +123,11 @@ private void init(@Nullable AttributeSet attrs, int defStyleAttr, int defStyleRe
R.styleable.MaterialProgressBar_mpb_indeterminateTint);
mProgressTintInfo.mHasIndeterminateTint = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_indeterminateBackgroundTint)) {
mProgressTintInfo.mIndeterminateBackgroundTint = a.getColorStateList(
R.styleable.MaterialProgressBar_mpb_indeterminateBackgroundTint);
mProgressTintInfo.mHasIndeterminateBackgroundTint = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_indeterminateTintMode)) {
mProgressTintInfo.mIndeterminateTintMode = DrawableCompat.parseTintMode(a.getInt(
R.styleable.MaterialProgressBar_mpb_indeterminateTintMode, -1), null);
Expand Down Expand Up @@ -212,7 +218,7 @@ public Drawable getCurrentDrawable() {
* Get whether the current drawable is using an intrinsic padding. The default is {@code true}.
*
* @return Whether the current drawable is using an intrinsic padding, or {@code false} if the
* drawable does not implement {@link IntrinsicPaddingDrawable}.
* drawable does not implement {@link IntrinsicPaddingDrawable}.
*/
public boolean getUseIntrinsicPadding() {
Drawable drawable = getCurrentDrawable();
Expand Down Expand Up @@ -247,7 +253,7 @@ public void setUseIntrinsicPadding(boolean useIntrinsicPadding) {
* Get whether the current drawable is showing a background. The default is {@code true}.
*
* @return Whether the current drawable is showing a background, or {@code false} if the
* drawable does not implement {@link ShowBackgroundDrawable}.
* drawable does not implement {@link ShowBackgroundDrawable}.
*/
public boolean getShowProgressBackground() {
Drawable drawable = getCurrentDrawable();
Expand Down Expand Up @@ -430,6 +436,26 @@ public void setIndeterminateTintList(@Nullable ColorStateList tint) {
setSupportIndeterminateTintList(tint);
}

/**
* Get the indeterminate progress bar background {@link ColorStateList}.
*/
public ColorStateList getIndeterminateBackgroundTintList() {
logProgressBarTintWarning();
return mProgressTintInfo.mIndeterminateBackgroundTint;
}

/**
* Set the indeterminate progress bar background {@link ColorStateList}.
*/
public void setIndeterminateBackgroundTintList(@Nullable ColorStateList tint) {
logProgressBarTintWarning();

mProgressTintInfo.mIndeterminateBackgroundTint = tint;
mProgressTintInfo.mHasIndeterminateBackgroundTint = true;

applyIndeterminateTint();
}

/**
* @deprecated Use {@link #getSupportIndeterminateTintMode()} instead.
*/
Expand Down Expand Up @@ -674,26 +700,40 @@ private Drawable getTintTargetFromProgressDrawable(int layerId, boolean shouldFa

private void applyIndeterminateTint() {
Drawable indeterminateDrawable = getIndeterminateDrawable();

if (indeterminateDrawable == null) {
return;
}

if (mProgressTintInfo.mHasIndeterminateTint
|| mProgressTintInfo.mHasIndeterminateTintMode) {
indeterminateDrawable.mutate();
applyTintForDrawable(indeterminateDrawable, mProgressTintInfo.mIndeterminateTint,
mProgressTintInfo.mHasIndeterminateTint,
mProgressTintInfo.mIndeterminateTintMode,
mProgressTintInfo.mHasIndeterminateTintMode);
applyTintForDrawable(
indeterminateDrawable
, mProgressTintInfo.mIndeterminateTint
, mProgressTintInfo.mHasIndeterminateTint
, mProgressTintInfo.mIndeterminateTintMode
, mProgressTintInfo.mHasIndeterminateTintMode
);
}

if (mProgressTintInfo.mHasIndeterminateBackgroundTint) {
((IndeterminateHorizontalProgressDrawable) indeterminateDrawable).setBackgroundTintList(
mProgressTintInfo.mIndeterminateBackgroundTint
);
}
}

// Progress drawables in this library has already rewritten tint related methods for
// compatibility.
@SuppressLint("NewApi")
private void applyTintForDrawable(@NonNull Drawable drawable, @Nullable ColorStateList tint,
boolean hasTint, @Nullable PorterDuff.Mode tintMode,
boolean hasTintMode) {

private void applyTintForDrawable(
@NonNull Drawable drawable
, @Nullable ColorStateList tint
, boolean hasTint
, @Nullable PorterDuff.Mode tintMode
, boolean hasTintMode
) {
if (hasTint || hasTintMode) {

if (hasTint) {
Expand Down Expand Up @@ -759,8 +799,11 @@ private static class TintInfo {
@Nullable
public ColorStateList mIndeterminateTint;
@Nullable
public ColorStateList mIndeterminateBackgroundTint;
@Nullable
public PorterDuff.Mode mIndeterminateTintMode;
public boolean mHasIndeterminateTint;
public boolean mHasIndeterminateBackgroundTint;
public boolean mHasIndeterminateTintMode;
}
}
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<enum name="add" value="16" />
</attr>
<attr name="mpb_indeterminateTint" format="color|reference" />
<attr name="mpb_indeterminateBackgroundTint" format="color|reference" />
<attr name="mpb_indeterminateTintMode" format="enum">
<enum name="src_over" value="3" />
<enum name="src_in" value="5" />
Expand Down
12 changes: 9 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ android {
signingConfig signingConfigs.release
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.jakewharton:butterknife:9.0.0-rc1'
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

implementation project(':library')
}
1 change: 1 addition & 0 deletions sample/src/main/res/layout/main_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
android:indeterminate="true"
app:mpb_progressStyle="horizontal"
app:mpb_indeterminateTint="@color/tint"
app:mpb_indeterminateBackgroundTint="@color/tint_background"
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal" />

<!-- Determinate circular progress -->
Expand Down