Skip to content
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

allow drawables as indicator #127

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
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;
import android.view.Gravity;
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 {

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -153,25 +156,24 @@ 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);
}
}
}

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();

Expand Down Expand Up @@ -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);
}
}
Expand Down
13 changes: 13 additions & 0 deletions circleindicator/src/main/java/me/relex/circleindicator/Config.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down