diff --git a/circleindicator/src/main/java/me/relex/circleindicator/BaseCircleIndicator.java b/circleindicator/src/main/java/me/relex/circleindicator/BaseCircleIndicator.java index b718c61..6c64b19 100644 --- a/circleindicator/src/main/java/me/relex/circleindicator/BaseCircleIndicator.java +++ b/circleindicator/src/main/java/me/relex/circleindicator/BaseCircleIndicator.java @@ -5,6 +5,7 @@ import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; import android.os.Build; import android.util.AttributeSet; import android.util.TypedValue; @@ -12,8 +13,9 @@ import android.view.View; import android.view.animation.Interpolator; import android.widget.LinearLayout; -import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import androidx.core.view.ViewCompat; class BaseCircleIndicator extends LinearLayout { @@ -23,8 +25,8 @@ class BaseCircleIndicator extends LinearLayout { protected int mIndicatorWidth = -1; protected int mIndicatorHeight = -1; - protected int mIndicatorBackgroundResId; - protected int mIndicatorUnselectedBackgroundResId; + protected Drawable mIndicatorBackground; + protected Drawable mIndicatorUnselectedBackground; protected Animator mAnimatorOut; protected Animator mAnimatorIn; @@ -52,7 +54,7 @@ public BaseCircleIndicator(Context context, AttributeSet attrs, int defStyleAttr @TargetApi(Build.VERSION_CODES.LOLLIPOP) public BaseCircleIndicator(Context context, AttributeSet attrs, int defStyleAttr, - int defStyleRes) { + int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(context, attrs); } @@ -76,15 +78,15 @@ private Config handleTypedArray(Context context, AttributeSet attrs) { config.margin = typedArray.getDimensionPixelSize(R.styleable.BaseCircleIndicator_ci_margin, -1); config.animatorResId = typedArray.getResourceId(R.styleable.BaseCircleIndicator_ci_animator, - R.animator.scale_with_alpha); + R.animator.scale_with_alpha); config.animatorReverseResId = typedArray.getResourceId(R.styleable.BaseCircleIndicator_ci_animator_reverse, 0); config.backgroundResId = typedArray.getResourceId(R.styleable.BaseCircleIndicator_ci_drawable, - R.drawable.white_radius); + R.drawable.white_radius); config.unselectedBackgroundId = typedArray.getResourceId(R.styleable.BaseCircleIndicator_ci_drawable_unselected, - config.backgroundResId); + config.backgroundResId); config.orientation = typedArray.getInt(R.styleable.BaseCircleIndicator_ci_orientation, -1); config.gravity = typedArray.getInt(R.styleable.BaseCircleIndicator_ci_gravity, -1); typedArray.recycle(); @@ -94,7 +96,7 @@ private Config handleTypedArray(Context context, AttributeSet attrs) { public void initialize(Config config) { int miniSize = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, - DEFAULT_INDICATOR_WIDTH, getResources().getDisplayMetrics()) + 0.5f); + DEFAULT_INDICATOR_WIDTH, getResources().getDisplayMetrics()) + 0.5f); mIndicatorWidth = (config.width < 0) ? miniSize : config.width; mIndicatorHeight = (config.height < 0) ? miniSize : config.height; mIndicatorMargin = (config.margin < 0) ? miniSize : config.margin; @@ -107,17 +109,18 @@ public void initialize(Config config) { mImmediateAnimatorIn = createAnimatorIn(config); mImmediateAnimatorIn.setDuration(0); - mIndicatorBackgroundResId = - (config.backgroundResId == 0) ? R.drawable.white_radius : config.backgroundResId; - mIndicatorUnselectedBackgroundResId = - (config.unselectedBackgroundId == 0) ? config.backgroundResId - : config.unselectedBackgroundId; + mIndicatorBackground = config.backgroundDrawable != null ? config.backgroundDrawable : + ContextCompat.getDrawable(getContext(), (config.backgroundResId == 0) ? R.drawable.white_radius : config.backgroundResId); + mIndicatorUnselectedBackground = config.unselectedBackgroundDrawable != null ? config.unselectedBackgroundDrawable : + ContextCompat.getDrawable(getContext(), + (config.unselectedBackgroundId == 0) ? config.backgroundResId : config.unselectedBackgroundId); setOrientation(config.orientation == VERTICAL ? VERTICAL : HORIZONTAL); setGravity(config.gravity >= 0 ? config.gravity : Gravity.CENTER); } public interface IndicatorCreatedListener { + /** * IndicatorCreatedListener * @@ -153,10 +156,10 @@ protected void createIndicators(int count, int currentPosition) { for (int i = 0; i < count; i++) { if (currentPosition == i) { indicator = - addIndicator(orientation, mIndicatorBackgroundResId, mImmediateAnimatorOut); + addIndicator(orientation, mIndicatorBackground, mImmediateAnimatorOut); } else { - indicator = addIndicator(orientation, mIndicatorUnselectedBackgroundResId, - mImmediateAnimatorIn); + indicator = addIndicator(orientation, mIndicatorUnselectedBackground, + mImmediateAnimatorIn); } if (mIndicatorCreatedListener != null) { mIndicatorCreatedListener.onIndicatorCreated(indicator, i); @@ -164,14 +167,13 @@ protected void createIndicators(int count, int currentPosition) { } } - protected View addIndicator(int orientation, @DrawableRes int backgroundDrawableId, - Animator animator) { + protected View addIndicator(int orientation, Drawable backgroundDrawable, Animator animator) { if (animator.isRunning()) { animator.end(); animator.cancel(); } View indicator = new View(getContext()); - indicator.setBackgroundResource(backgroundDrawableId); + ViewCompat.setBackground(indicator, backgroundDrawable); addView(indicator, mIndicatorWidth, mIndicatorHeight); LayoutParams lp = (LayoutParams) indicator.getLayoutParams(); @@ -204,21 +206,23 @@ protected void internalPageSelected(int position) { View currentIndicator; if (mLastPosition >= 0 && (currentIndicator = getChildAt(mLastPosition)) != null) { - currentIndicator.setBackgroundResource(mIndicatorUnselectedBackgroundResId); + ViewCompat.setBackground(currentIndicator, mIndicatorUnselectedBackground); mAnimatorIn.setTarget(currentIndicator); mAnimatorIn.start(); } View selectedIndicator = getChildAt(position); if (selectedIndicator != null) { - selectedIndicator.setBackgroundResource(mIndicatorBackgroundResId); + ViewCompat.setBackground(selectedIndicator, mIndicatorBackground); mAnimatorOut.setTarget(selectedIndicator); mAnimatorOut.start(); } } protected class ReverseInterpolator implements Interpolator { - @Override public float getInterpolation(float value) { + + @Override + public float getInterpolation(float value) { return Math.abs(1.0f - value); } } diff --git a/circleindicator/src/main/java/me/relex/circleindicator/Config.java b/circleindicator/src/main/java/me/relex/circleindicator/Config.java index cc5babf..6a178be 100644 --- a/circleindicator/src/main/java/me/relex/circleindicator/Config.java +++ b/circleindicator/src/main/java/me/relex/circleindicator/Config.java @@ -1,5 +1,6 @@ package me.relex.circleindicator; +import android.graphics.drawable.Drawable; import android.view.Gravity; import android.widget.LinearLayout; import androidx.annotation.AnimatorRes; @@ -14,6 +15,8 @@ public class Config { @AnimatorRes int animatorReverseResId = 0; @DrawableRes int backgroundResId = R.drawable.white_radius; @DrawableRes int unselectedBackgroundId; + Drawable backgroundDrawable; + Drawable unselectedBackgroundDrawable; int orientation = LinearLayout.HORIZONTAL; int gravity = Gravity.CENTER; @@ -53,6 +56,16 @@ public Builder animatorReverse(@AnimatorRes int animatorReverseResId) { return this; } + public Builder drawable(Drawable background) { + mConfig.backgroundDrawable = background; + return this; + } + + public Builder drawableUnselected(Drawable unselectedBackground) { + mConfig.unselectedBackgroundDrawable = unselectedBackground; + return this; + } + public Builder drawable(@DrawableRes int backgroundResId) { mConfig.backgroundResId = backgroundResId; return this;