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

Commit

Permalink
added color transition animation on text and image as tint
Browse files Browse the repository at this point in the history
  • Loading branch information
ceryle committed May 4, 2017
1 parent 0329724 commit b21d7b7
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package co.ceryle.radiorealbutton;

import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
Expand All @@ -25,12 +27,15 @@
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RadioRealButton extends LinearLayout {
public RadioRealButton(Context context) {
Expand Down Expand Up @@ -275,14 +280,44 @@ private void setPaddingAttrs() {
setPadding(padding, padding, padding, padding);
}

protected void bounceDrawable(float scale, int duration, Interpolator interpolator, boolean hasAnimation) {
void colorTransitionDrawable(int colorFrom, int colorTo, int duration, boolean hasAnimation) {
if (hasAnimation)
colorTransition(imageView, colorFrom, colorTo, duration);
else
setDrawableTint(colorTo);
}

void colorTransitionText(int colorFrom, int colorTo, int duration, boolean hasAnimation) {
if (hasAnimation)
colorTransition(textView, colorFrom, colorTo, duration);
else
setTextColor(colorTo);
}

private void colorTransition(final View v, int colorFrom, int colorTo, int duration) {
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(duration);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
if (v instanceof ImageView) {
((ImageView) v).setColorFilter((int) animator.getAnimatedValue());
} else {
((TextView) v).setTextColor((int) animator.getAnimatedValue());
}
}
});
colorAnimation.start();
}

void bounceDrawable(float scale, int duration, Interpolator interpolator, boolean hasAnimation) {
if (hasAnimation)
bounce(imageView, scale, duration, interpolator);
else
bounceDrawable(scale);
}

protected void bounceText(float scale, int duration, Interpolator interpolator, boolean hasAnimation) {
void bounceText(float scale, int duration, Interpolator interpolator, boolean hasAnimation) {
if (hasAnimation)
bounce(textView, scale, duration, interpolator);
else
Expand All @@ -307,11 +342,11 @@ private void bounce(View view, float scale, int duration, Interpolator interpola
set.start();
}*/

protected void bounceDrawable(float scale) {
void bounceDrawable(float scale) {
bounce(imageView, scale);
}

protected void bounceText(float scale) {
void bounceText(float scale) {
bounce(textView, scale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,30 @@ private void setBorderAttrs() {
animateImagesExitDuration, animateTextsExit, animateTextsExitDuration, lastPosition, buttonPadding,
buttonPaddingLeft, buttonPaddingRight, buttonPaddingTop, buttonPaddingBottom, groupBackgroundColor, dividerBackgroundColor,
dividerPadding, dividerSize, dividerRadius, bottomLineSize, bottomLineRadius, selectorSize, selectorRadius, initialPosition,
selectorDividerSize, selectorDividerRadius, selectorDividerColor, selectorDividerPadding, checkedButtonId;
selectorDividerSize, selectorDividerRadius, selectorDividerColor, selectorDividerPadding, checkedButtonId,
animateTextsTextColorFrom, animateTextsTextColorTo, animateTextsTextColorDuration,
animateDrawablesTintColorFrom, animateDrawablesTintColorTo, animateDrawablesTintColorDuration;

private float radius, animateImagesScale, animateTextsScale;

private boolean bottomLineBringToFront, selectorBringToFront, selectorAboveOfBottomLine, selectorTop, selectorBottom, hasPadding,
hasPaddingLeft, hasPaddingRight, hasPaddingTop, hasPaddingBottom, hasDividerBackgroundColor, clickable, enabled,
enableDeselection, hasEnabled, hasClickable, hasBorder, hasAnimateImages, hasAnimateTexts, hasAnimation, selectorFullSize;
enableDeselection, hasEnabled, hasClickable, hasBorder, hasAnimateImages, hasAnimateTexts, hasAnimation, selectorFullSize,
hasAnimateTextsColor, hasAnimateDrawablesColor;

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

animateTextsTextColorFrom = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateTexts_textColorFrom, Color.BLACK);
animateTextsTextColorTo = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateTexts_textColorTo, Color.WHITE);
hasAnimateTextsColor = ta.hasValue(R.styleable.RadioRealButtonGroup_rrbg_animateTexts_textColorTo);
animateTextsTextColorDuration = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateTexts_textColorDuration, 500);

animateDrawablesTintColorFrom = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateDrawables_tintColorFrom, Color.BLACK);
animateDrawablesTintColorTo = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateDrawables_tintColorTo, Color.WHITE);
hasAnimateDrawablesColor = ta.hasValue(R.styleable.RadioRealButtonGroup_rrbg_animateDrawables_tintColorTo);
animateDrawablesTintColorDuration = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_animateDrawables_tintColorDuration, 500);

bottomLineColor = ta.getColor(R.styleable.RadioRealButtonGroup_rrbg_bottomLineColor, Color.GRAY);
bottomLineSize = ta.getDimensionPixelSize(R.styleable.RadioRealButtonGroup_rrbg_bottomLineSize, 0);
bottomLineBringToFront = ta.getBoolean(R.styleable.RadioRealButtonGroup_rrbg_bottomLineBringToFront, false);
Expand Down Expand Up @@ -366,9 +379,20 @@ else if (checkedButtonId == NO_ID && button.isChecked())
button.bounceDrawable(animateImagesScale);
if (hasAnimateTexts)
button.bounceText(animateTextsScale);
} else
if (hasAnimateTextsColor)
button.setTextColor(animateTextsTextColorTo);
if (hasAnimateDrawablesColor)
button.setDrawableTint(animateDrawablesTintColorTo);

} else{
button.setChecked(false);

if (hasAnimateTextsColor)
button.setTextColor(animateTextsTextColorFrom);
if (hasAnimateDrawablesColor)
button.setDrawableTint(animateDrawablesTintColorFrom);
}

initButtonListener(button, position);
setButtonPadding(button);
container.addView(button);
Expand Down Expand Up @@ -484,11 +508,10 @@ private void makeSelection(int position, boolean isToggledByTouch, boolean hasAn
if (null != buttonOut)
buttonOut.setChecked(false);
} else {
if (lastPosition == position && buttonIn.isChecked()){
if (lastPosition == position && buttonIn.isChecked()) {
buttonIn.setChecked(false);
position = -1;
}
else
} else
buttonIn.setChecked(true);
}

Expand Down Expand Up @@ -525,13 +548,21 @@ private void animateExit(RadioRealButton button, boolean hasAnimation) {
button.bounceText(1, animateTextsExitDuration, interpolatorTextExit, hasAnimation);
if (hasAnimateImages)
button.bounceDrawable(1, animateImagesExitDuration, interpolatorImageExit, hasAnimation);
if (hasAnimateTextsColor)
button.colorTransitionText(animateTextsTextColorTo, animateTextsTextColorFrom, animateTextsTextColorDuration, hasAnimation);
if (hasAnimateDrawablesColor)
button.colorTransitionDrawable(animateDrawablesTintColorTo, animateDrawablesTintColorFrom, animateDrawablesTintColorDuration, hasAnimation);
}

private void animateEnter(RadioRealButton button, boolean hasAnimation) {
if (hasAnimateTexts)
button.bounceText(animateTextsScale, animateTextsDuration, interpolatorText, hasAnimation);
if (hasAnimateImages)
button.bounceDrawable(animateImagesScale, animateImagesDuration, interpolatorImage, hasAnimation);
if (hasAnimateTextsColor)
button.colorTransitionText(animateTextsTextColorFrom, animateTextsTextColorTo, animateTextsTextColorDuration, hasAnimation);
if (hasAnimateDrawablesColor)
button.colorTransitionDrawable(animateDrawablesTintColorFrom, animateDrawablesTintColorTo, animateDrawablesTintColorDuration, hasAnimation);
}
/* DRAWABLE AND TEXT ANIMATION ENDS */

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

public interface OnLongClickedButtonListener {
boolean onLongClickedButton(RadioRealButton button, int position);

}

/**
* LISTENERS --- ENDS
*/


public int getAnimateTextsTextColorFrom() {
return animateTextsTextColorFrom;
}

public void setAnimateTextsTextColorFrom(int animateTextsTextColorFrom) {
this.animateTextsTextColorFrom = animateTextsTextColorFrom;
}

public int getAnimateTextsTextColorTo() {
return animateTextsTextColorTo;
}

public void setAnimateTextsTextColorTo(int animateTextsTextColorTo) {
this.animateTextsTextColorTo = animateTextsTextColorTo;
}

public int getAnimateTextsTextColorDuration() {
return animateTextsTextColorDuration;
}

public void setAnimateTextsTextColorDuration(int animateTextsTextColorDuration) {
this.animateTextsTextColorDuration = animateTextsTextColorDuration;
}

public int getAnimateDrawablesTintColorFrom() {
return animateDrawablesTintColorFrom;
}

public void setAnimateDrawablesTintColorFrom(int animateDrawablesTintColorFrom) {
this.animateDrawablesTintColorFrom = animateDrawablesTintColorFrom;
}

public int getAnimateDrawablesTintColorTo() {
return animateDrawablesTintColorTo;
}

public void setAnimateDrawablesTintColorTo(int animateDrawablesTintColorTo) {
this.animateDrawablesTintColorTo = animateDrawablesTintColorTo;
}

public int getAnimateDrawablesTintColorDuration() {
return animateDrawablesTintColorDuration;
}

public void setAnimateDrawablesTintColorDuration(int animateDrawablesTintColorDuration) {
this.animateDrawablesTintColorDuration = animateDrawablesTintColorDuration;
}


public List<RadioRealButton> getButtons() {
return buttons;
}
Expand Down
8 changes: 8 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@

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

<attr name="rrbg_animateDrawables_tintColorDuration" format="integer"/>
<attr name="rrbg_animateDrawables_tintColorFrom" format="color"/>
<attr name="rrbg_animateDrawables_tintColorTo" format="color"/>

<attr name="rrbg_animateTexts_textColorDuration" format="integer"/>
<attr name="rrbg_animateTexts_textColorFrom" format="color"/>
<attr name="rrbg_animateTexts_textColorTo" format="color"/>

<attr name="rrbg_animateSelector_delay" format="integer"/>
<attr name="rrbg_animateSelector_duration" format="integer"/>
<attr name="rrbg_animateSelector" format="integer">
Expand Down
14 changes: 7 additions & 7 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
android:layout_margin="4dp"
android:elevation="2dp"
app:rrbg_animateDrawables_enter="overshoot"
app:rrbg_animateDrawables_enterDuration="500"
app:rrbg_animateDrawables_exit="overshoot"
app:rrbg_animateDrawables_exitDuration="500"
app:rrbg_animateDrawables_scale="1.2"
app:rrbg_animateDrawables_tintColorDuration="400"
app:rrbg_animateDrawables_tintColorFrom="@color/white"
app:rrbg_animateDrawables_tintColorTo="@android:color/transparent"
app:rrbg_animateTexts_enter="overshoot"
app:rrbg_animateTexts_enterDuration="500"
app:rrbg_backgroundColor="@color/red_900"
app:rrbg_animateTexts_textColorDuration="400"
app:rrbg_animateTexts_textColorFrom="@color/white"
app:rrbg_animateTexts_textColorTo="@color/black"
app:rrbg_backgroundColor="@color/red_500"
app:rrbg_borderColor="@color/white"
app:rrbg_borderSize="1dp"
app:rrbg_bottomLineColor="@color/blue_300"
Expand All @@ -48,7 +51,6 @@
app:rrb_drawableGravity="left"
app:rrb_drawableHeight="36dp"
app:rrb_drawablePadding="4dp"
app:rrb_drawableTint="@color/white"
app:rrb_drawableWidth="36dp"
app:rrb_rippleColor="@color/black"
app:rrb_text="Bruce is happy because he is batman"
Expand All @@ -64,7 +66,6 @@
app:rrb_drawableGravity="top"
app:rrb_drawableHeight="36dp"
app:rrb_drawablePadding="4dp"
app:rrb_drawableTint="@color/white"
app:rrb_drawableWidth="36dp"
app:rrb_rippleColor="@color/blue_500"
app:rrb_text="Clark is super jealous of Batman"
Expand All @@ -80,7 +81,6 @@
app:rrb_drawableGravity="right"
app:rrb_drawableHeight="36dp"
app:rrb_drawablePadding="4dp"
app:rrb_drawableTint="@color/white"
app:rrb_drawableWidth="36dp"
app:rrb_rippleColor="@color/yellow_600"
app:rrb_text="Diana wonders why"
Expand Down

0 comments on commit b21d7b7

Please sign in to comment.