Skip to content
This repository was archived by the owner on Mar 27, 2022. It is now read-only.

Commit b21d7b7

Browse files
committed
added color transition animation on text and image as tint
1 parent 0329724 commit b21d7b7

File tree

4 files changed

+142
-17
lines changed

4 files changed

+142
-17
lines changed

library/src/main/java/co/ceryle/radiorealbutton/RadioRealButton.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package co.ceryle.radiorealbutton;
1717

18+
import android.animation.ArgbEvaluator;
19+
import android.animation.ValueAnimator;
1820
import android.annotation.TargetApi;
1921
import android.content.Context;
2022
import android.content.res.TypedArray;
@@ -25,12 +27,15 @@
2527
import android.support.v7.widget.AppCompatImageView;
2628
import android.support.v7.widget.AppCompatTextView;
2729
import android.util.AttributeSet;
30+
import android.util.Log;
2831
import android.util.TypedValue;
2932
import android.view.Gravity;
3033
import android.view.View;
3134
import android.view.ViewGroup;
3235
import android.view.animation.Interpolator;
36+
import android.widget.ImageView;
3337
import android.widget.LinearLayout;
38+
import android.widget.TextView;
3439

3540
public class RadioRealButton extends LinearLayout {
3641
public RadioRealButton(Context context) {
@@ -275,14 +280,44 @@ private void setPaddingAttrs() {
275280
setPadding(padding, padding, padding, padding);
276281
}
277282

278-
protected void bounceDrawable(float scale, int duration, Interpolator interpolator, boolean hasAnimation) {
283+
void colorTransitionDrawable(int colorFrom, int colorTo, int duration, boolean hasAnimation) {
284+
if (hasAnimation)
285+
colorTransition(imageView, colorFrom, colorTo, duration);
286+
else
287+
setDrawableTint(colorTo);
288+
}
289+
290+
void colorTransitionText(int colorFrom, int colorTo, int duration, boolean hasAnimation) {
291+
if (hasAnimation)
292+
colorTransition(textView, colorFrom, colorTo, duration);
293+
else
294+
setTextColor(colorTo);
295+
}
296+
297+
private void colorTransition(final View v, int colorFrom, int colorTo, int duration) {
298+
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
299+
colorAnimation.setDuration(duration);
300+
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
301+
@Override
302+
public void onAnimationUpdate(ValueAnimator animator) {
303+
if (v instanceof ImageView) {
304+
((ImageView) v).setColorFilter((int) animator.getAnimatedValue());
305+
} else {
306+
((TextView) v).setTextColor((int) animator.getAnimatedValue());
307+
}
308+
}
309+
});
310+
colorAnimation.start();
311+
}
312+
313+
void bounceDrawable(float scale, int duration, Interpolator interpolator, boolean hasAnimation) {
279314
if (hasAnimation)
280315
bounce(imageView, scale, duration, interpolator);
281316
else
282317
bounceDrawable(scale);
283318
}
284319

285-
protected void bounceText(float scale, int duration, Interpolator interpolator, boolean hasAnimation) {
320+
void bounceText(float scale, int duration, Interpolator interpolator, boolean hasAnimation) {
286321
if (hasAnimation)
287322
bounce(textView, scale, duration, interpolator);
288323
else
@@ -307,11 +342,11 @@ private void bounce(View view, float scale, int duration, Interpolator interpola
307342
set.start();
308343
}*/
309344

310-
protected void bounceDrawable(float scale) {
345+
void bounceDrawable(float scale) {
311346
bounce(imageView, scale);
312347
}
313348

314-
protected void bounceText(float scale) {
349+
void bounceText(float scale) {
315350
bounce(textView, scale);
316351
}
317352

library/src/main/java/co/ceryle/radiorealbutton/RadioRealButtonGroup.java

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,30 @@ private void setBorderAttrs() {
234234
animateImagesExitDuration, animateTextsExit, animateTextsExitDuration, lastPosition, buttonPadding,
235235
buttonPaddingLeft, buttonPaddingRight, buttonPaddingTop, buttonPaddingBottom, groupBackgroundColor, dividerBackgroundColor,
236236
dividerPadding, dividerSize, dividerRadius, bottomLineSize, bottomLineRadius, selectorSize, selectorRadius, initialPosition,
237-
selectorDividerSize, selectorDividerRadius, selectorDividerColor, selectorDividerPadding, checkedButtonId;
237+
selectorDividerSize, selectorDividerRadius, selectorDividerColor, selectorDividerPadding, checkedButtonId,
238+
animateTextsTextColorFrom, animateTextsTextColorTo, animateTextsTextColorDuration,
239+
animateDrawablesTintColorFrom, animateDrawablesTintColorTo, animateDrawablesTintColorDuration;
238240

239241
private float radius, animateImagesScale, animateTextsScale;
240242

241243
private boolean bottomLineBringToFront, selectorBringToFront, selectorAboveOfBottomLine, selectorTop, selectorBottom, hasPadding,
242244
hasPaddingLeft, hasPaddingRight, hasPaddingTop, hasPaddingBottom, hasDividerBackgroundColor, clickable, enabled,
243-
enableDeselection, hasEnabled, hasClickable, hasBorder, hasAnimateImages, hasAnimateTexts, hasAnimation, selectorFullSize;
245+
enableDeselection, hasEnabled, hasClickable, hasBorder, hasAnimateImages, hasAnimateTexts, hasAnimation, selectorFullSize,
246+
hasAnimateTextsColor, hasAnimateDrawablesColor;
244247

245248
private void getAttributes(AttributeSet attrs) {
246249
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.RadioRealButtonGroup);
247250

251+
animateTextsTextColorFrom = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateTexts_textColorFrom, Color.BLACK);
252+
animateTextsTextColorTo = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateTexts_textColorTo, Color.WHITE);
253+
hasAnimateTextsColor = ta.hasValue(R.styleable.RadioRealButtonGroup_rrbg_animateTexts_textColorTo);
254+
animateTextsTextColorDuration = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateTexts_textColorDuration, 500);
255+
256+
animateDrawablesTintColorFrom = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateDrawables_tintColorFrom, Color.BLACK);
257+
animateDrawablesTintColorTo = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateDrawables_tintColorTo, Color.WHITE);
258+
hasAnimateDrawablesColor = ta.hasValue(R.styleable.RadioRealButtonGroup_rrbg_animateDrawables_tintColorTo);
259+
animateDrawablesTintColorDuration = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateDrawables_tintColorDuration, 500);
260+
248261
bottomLineColor = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_bottomLineColor, Color.GRAY);
249262
bottomLineSize = ta.getDimensionPixelSize(R.styleable.RadioRealButtonGroup_rrbg_bottomLineSize, 0);
250263
bottomLineBringToFront = ta.getBoolean(R.styleable.RadioRealButtonGroup_rrbg_bottomLineBringToFront, false);
@@ -366,9 +379,20 @@ else if (checkedButtonId == NO_ID && button.isChecked())
366379
button.bounceDrawable(animateImagesScale);
367380
if (hasAnimateTexts)
368381
button.bounceText(animateTextsScale);
369-
} else
382+
if (hasAnimateTextsColor)
383+
button.setTextColor(animateTextsTextColorTo);
384+
if (hasAnimateDrawablesColor)
385+
button.setDrawableTint(animateDrawablesTintColorTo);
386+
387+
} else{
370388
button.setChecked(false);
371389

390+
if (hasAnimateTextsColor)
391+
button.setTextColor(animateTextsTextColorFrom);
392+
if (hasAnimateDrawablesColor)
393+
button.setDrawableTint(animateDrawablesTintColorFrom);
394+
}
395+
372396
initButtonListener(button, position);
373397
setButtonPadding(button);
374398
container.addView(button);
@@ -484,11 +508,10 @@ private void makeSelection(int position, boolean isToggledByTouch, boolean hasAn
484508
if (null != buttonOut)
485509
buttonOut.setChecked(false);
486510
} else {
487-
if (lastPosition == position && buttonIn.isChecked()){
511+
if (lastPosition == position && buttonIn.isChecked()) {
488512
buttonIn.setChecked(false);
489513
position = -1;
490-
}
491-
else
514+
} else
492515
buttonIn.setChecked(true);
493516
}
494517

@@ -525,13 +548,21 @@ private void animateExit(RadioRealButton button, boolean hasAnimation) {
525548
button.bounceText(1, animateTextsExitDuration, interpolatorTextExit, hasAnimation);
526549
if (hasAnimateImages)
527550
button.bounceDrawable(1, animateImagesExitDuration, interpolatorImageExit, hasAnimation);
551+
if (hasAnimateTextsColor)
552+
button.colorTransitionText(animateTextsTextColorTo, animateTextsTextColorFrom, animateTextsTextColorDuration, hasAnimation);
553+
if (hasAnimateDrawablesColor)
554+
button.colorTransitionDrawable(animateDrawablesTintColorTo, animateDrawablesTintColorFrom, animateDrawablesTintColorDuration, hasAnimation);
528555
}
529556

530557
private void animateEnter(RadioRealButton button, boolean hasAnimation) {
531558
if (hasAnimateTexts)
532559
button.bounceText(animateTextsScale, animateTextsDuration, interpolatorText, hasAnimation);
533560
if (hasAnimateImages)
534561
button.bounceDrawable(animateImagesScale, animateImagesDuration, interpolatorImage, hasAnimation);
562+
if (hasAnimateTextsColor)
563+
button.colorTransitionText(animateTextsTextColorFrom, animateTextsTextColorTo, animateTextsTextColorDuration, hasAnimation);
564+
if (hasAnimateDrawablesColor)
565+
button.colorTransitionDrawable(animateDrawablesTintColorFrom, animateDrawablesTintColorTo, animateDrawablesTintColorDuration, hasAnimation);
535566
}
536567
/* DRAWABLE AND TEXT ANIMATION ENDS */
537568

@@ -701,12 +732,63 @@ public boolean onLongClick(View v) {
701732

702733
public interface OnLongClickedButtonListener {
703734
boolean onLongClickedButton(RadioRealButton button, int position);
735+
704736
}
705737

706738
/**
707739
* LISTENERS --- ENDS
708740
*/
709741

742+
743+
public int getAnimateTextsTextColorFrom() {
744+
return animateTextsTextColorFrom;
745+
}
746+
747+
public void setAnimateTextsTextColorFrom(int animateTextsTextColorFrom) {
748+
this.animateTextsTextColorFrom = animateTextsTextColorFrom;
749+
}
750+
751+
public int getAnimateTextsTextColorTo() {
752+
return animateTextsTextColorTo;
753+
}
754+
755+
public void setAnimateTextsTextColorTo(int animateTextsTextColorTo) {
756+
this.animateTextsTextColorTo = animateTextsTextColorTo;
757+
}
758+
759+
public int getAnimateTextsTextColorDuration() {
760+
return animateTextsTextColorDuration;
761+
}
762+
763+
public void setAnimateTextsTextColorDuration(int animateTextsTextColorDuration) {
764+
this.animateTextsTextColorDuration = animateTextsTextColorDuration;
765+
}
766+
767+
public int getAnimateDrawablesTintColorFrom() {
768+
return animateDrawablesTintColorFrom;
769+
}
770+
771+
public void setAnimateDrawablesTintColorFrom(int animateDrawablesTintColorFrom) {
772+
this.animateDrawablesTintColorFrom = animateDrawablesTintColorFrom;
773+
}
774+
775+
public int getAnimateDrawablesTintColorTo() {
776+
return animateDrawablesTintColorTo;
777+
}
778+
779+
public void setAnimateDrawablesTintColorTo(int animateDrawablesTintColorTo) {
780+
this.animateDrawablesTintColorTo = animateDrawablesTintColorTo;
781+
}
782+
783+
public int getAnimateDrawablesTintColorDuration() {
784+
return animateDrawablesTintColorDuration;
785+
}
786+
787+
public void setAnimateDrawablesTintColorDuration(int animateDrawablesTintColorDuration) {
788+
this.animateDrawablesTintColorDuration = animateDrawablesTintColorDuration;
789+
}
790+
791+
710792
public List<RadioRealButton> getButtons() {
711793
return buttons;
712794
}

library/src/main/res/values/attrs.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@
122122

123123
<attr name="rrbg_animate" format="boolean"/>
124124

125+
<attr name="rrbg_animateDrawables_tintColorDuration" format="integer"/>
126+
<attr name="rrbg_animateDrawables_tintColorFrom" format="color"/>
127+
<attr name="rrbg_animateDrawables_tintColorTo" format="color"/>
128+
129+
<attr name="rrbg_animateTexts_textColorDuration" format="integer"/>
130+
<attr name="rrbg_animateTexts_textColorFrom" format="color"/>
131+
<attr name="rrbg_animateTexts_textColorTo" format="color"/>
132+
125133
<attr name="rrbg_animateSelector_delay" format="integer"/>
126134
<attr name="rrbg_animateSelector_duration" format="integer"/>
127135
<attr name="rrbg_animateSelector" format="integer">

sample/src/main/res/layout/activity_main.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
android:layout_margin="4dp"
1717
android:elevation="2dp"
1818
app:rrbg_animateDrawables_enter="overshoot"
19-
app:rrbg_animateDrawables_enterDuration="500"
2019
app:rrbg_animateDrawables_exit="overshoot"
21-
app:rrbg_animateDrawables_exitDuration="500"
2220
app:rrbg_animateDrawables_scale="1.2"
21+
app:rrbg_animateDrawables_tintColorDuration="400"
22+
app:rrbg_animateDrawables_tintColorFrom="@color/white"
23+
app:rrbg_animateDrawables_tintColorTo="@android:color/transparent"
2324
app:rrbg_animateTexts_enter="overshoot"
24-
app:rrbg_animateTexts_enterDuration="500"
25-
app:rrbg_backgroundColor="@color/red_900"
25+
app:rrbg_animateTexts_textColorDuration="400"
26+
app:rrbg_animateTexts_textColorFrom="@color/white"
27+
app:rrbg_animateTexts_textColorTo="@color/black"
28+
app:rrbg_backgroundColor="@color/red_500"
2629
app:rrbg_borderColor="@color/white"
2730
app:rrbg_borderSize="1dp"
2831
app:rrbg_bottomLineColor="@color/blue_300"
@@ -48,7 +51,6 @@
4851
app:rrb_drawableGravity="left"
4952
app:rrb_drawableHeight="36dp"
5053
app:rrb_drawablePadding="4dp"
51-
app:rrb_drawableTint="@color/white"
5254
app:rrb_drawableWidth="36dp"
5355
app:rrb_rippleColor="@color/black"
5456
app:rrb_text="Bruce is happy because he is batman"
@@ -64,7 +66,6 @@
6466
app:rrb_drawableGravity="top"
6567
app:rrb_drawableHeight="36dp"
6668
app:rrb_drawablePadding="4dp"
67-
app:rrb_drawableTint="@color/white"
6869
app:rrb_drawableWidth="36dp"
6970
app:rrb_rippleColor="@color/blue_500"
7071
app:rrb_text="Clark is super jealous of Batman"
@@ -80,7 +81,6 @@
8081
app:rrb_drawableGravity="right"
8182
app:rrb_drawableHeight="36dp"
8283
app:rrb_drawablePadding="4dp"
83-
app:rrb_drawableTint="@color/white"
8484
app:rrb_drawableWidth="36dp"
8585
app:rrb_rippleColor="@color/yellow_600"
8686
app:rrb_text="Diana wonders why"

0 commit comments

Comments
 (0)