Skip to content

Commit

Permalink
fix(android): support transparent backgrounds with touchFeedback (#12539
Browse files Browse the repository at this point in the history
)

apply mask for transparent backgrounds

Fixes TIMOB-26663
  • Loading branch information
build committed Mar 10, 2021
1 parent 7893408 commit 5b5ac48
Showing 1 changed file with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.os.Build;
Expand Down Expand Up @@ -1183,22 +1184,34 @@ private void applyTouchFeedback(Integer rippleColor)
if (backgroundDrawable == null) {
backgroundDrawable = this.nativeView.getBackground();
}
if (backgroundDrawable instanceof TiBackgroundDrawable) {
final TiBackgroundDrawable drawable = (TiBackgroundDrawable) backgroundDrawable;

// Create a mask if a background doesn't exist or if it's completely transparent.
// Note: Ripple effect won't work unless it has something opaque to draw to. Use mask as a fallback.
ShapeDrawable maskDrawable = null;
boolean isVisible = (backgroundDrawable != null);
if (backgroundDrawable instanceof ColorDrawable) {
int colorValue = ((ColorDrawable) backgroundDrawable).getColor();
if (Color.alpha(colorValue) <= 0) {
isVisible = false;
}
backgroundDrawable = drawable.getBackground();
}
if (!isVisible) {
maskDrawable = new ShapeDrawable();

// Parse background color to determine transparency.
int backgroundColor = -1;
if (backgroundDrawable instanceof PaintDrawable) {
final PaintDrawable drawable = (PaintDrawable) backgroundDrawable;

backgroundColor = drawable.getPaint().getColor();
} else if (backgroundDrawable instanceof ColorDrawable) {
final ColorDrawable drawable = (ColorDrawable) backgroundDrawable;

backgroundColor = drawable.getColor();
}
if (Color.alpha(backgroundColor) <= 0) {

// Do not use drawable for transparent backgrounds.
backgroundDrawable = null;
}

// Create a mask if a background doesn't exist or if it's completely transparent.
// Note: Ripple effect won't work unless it has something opaque to draw to. Use mask as a fallback.
final ShapeDrawable maskDrawable = backgroundDrawable == null ? new ShapeDrawable() : null;

// Replace view's existing background with ripple effect wrapping the old drawable.
// Replace existing background with ripple effect wrapping the old drawable.
nativeView.setBackground(
new RippleDrawable(ColorStateList.valueOf(rippleColor), backgroundDrawable, maskDrawable));
}
Expand Down

0 comments on commit 5b5ac48

Please sign in to comment.