-
-
Notifications
You must be signed in to change notification settings - Fork 998
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
implement rippleRadius for TouchableNativeFeedback #1069
Merged
jakub-gonet
merged 4 commits into
software-mansion:master
from
vonovak:support-ripple-radius
Jul 7, 2020
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
import android.view.MotionEvent; | ||
import android.view.ViewGroup; | ||
|
||
import com.facebook.react.bridge.SoftAssertions; | ||
import com.facebook.react.uimanager.PixelUtil; | ||
import com.facebook.react.uimanager.ThemedReactContext; | ||
import com.facebook.react.uimanager.ViewGroupManager; | ||
import com.facebook.react.uimanager.ViewProps; | ||
|
@@ -29,10 +31,13 @@ static class ButtonViewGroup extends ViewGroup { | |
int mBackgroundColor = Color.TRANSPARENT; | ||
// Using object because of handling null representing no value set. | ||
Integer mRippleColor; | ||
Integer mRippleRadius; | ||
boolean mUseForeground = false; | ||
boolean mUseBorderless = false; | ||
float mBorderRadius = 0; | ||
boolean mNeedBackgroundUpdate = false; | ||
public static final String SELECTABLE_ITEM_BACKGROUND = "selectableItemBackground"; | ||
public static final String SELECTABLE_ITEM_BACKGROUND_BORDERLESS = "selectableItemBackgroundBorderless"; | ||
|
||
|
||
public ButtonViewGroup(Context context) { | ||
|
@@ -55,21 +60,31 @@ public void setRippleColor(Integer color) { | |
mNeedBackgroundUpdate = true; | ||
} | ||
|
||
public void setRippleRadius(Integer radius) { | ||
mRippleRadius = radius; | ||
mNeedBackgroundUpdate = true; | ||
} | ||
|
||
public void setBorderRadius(float borderRadius) { | ||
mBorderRadius = borderRadius * (float)getResources().getDisplayMetrics().density; | ||
mBorderRadius = borderRadius * getResources().getDisplayMetrics().density; | ||
mNeedBackgroundUpdate = true; | ||
} | ||
|
||
private Drawable applyRippleEffectWhenNeeded(Drawable selectable) { | ||
if (mRippleColor != null | ||
&& selectable != null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP | ||
&& selectable instanceof RippleDrawable) { | ||
int[][] states = new int[][]{ new int[]{ android.R.attr.state_enabled } }; | ||
int[] colors = new int[]{ mRippleColor }; | ||
ColorStateList colorStateList = new ColorStateList(states, colors); | ||
((RippleDrawable) selectable).setColor(colorStateList); | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M | ||
&& mRippleRadius != null | ||
&& selectable instanceof RippleDrawable) { | ||
RippleDrawable rippleDrawable = (RippleDrawable) selectable; | ||
rippleDrawable.setRadius((int) PixelUtil.toPixelFromDIP(mRippleRadius)); | ||
} | ||
return selectable; | ||
} | ||
|
||
|
@@ -81,10 +96,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) { | |
// We call `onTouchEvent` to and wait until button changes state to `pressed`, if it's pressed | ||
// we return true so that the gesture handler can activate | ||
onTouchEvent(ev); | ||
if (isPressed()) { | ||
return true; | ||
} | ||
return false; | ||
return isPressed(); | ||
} | ||
|
||
private void updateBackground() { | ||
|
@@ -144,9 +156,9 @@ public void setUseBorderlessDrawable(boolean useBorderless) { | |
|
||
private Drawable createSelectableDrawable() { | ||
final int version = Build.VERSION.SDK_INT; | ||
String identifier = mUseBorderless && version >= 21 ? "selectableItemBackgroundBorderless" | ||
: "selectableItemBackground"; | ||
int attrID = getResources().getIdentifier(identifier, "attr", "android"); | ||
String identifier = mUseBorderless && version >= 21 ? SELECTABLE_ITEM_BACKGROUND_BORDERLESS | ||
: SELECTABLE_ITEM_BACKGROUND; | ||
int attrID = getAttrId(getContext(), identifier); | ||
getContext().getTheme().resolveAttribute(attrID, sResolveOutValue, true); | ||
if (version >= 21) { | ||
return getResources().getDrawable(sResolveOutValue.resourceId, getContext().getTheme()); | ||
|
@@ -155,6 +167,18 @@ private Drawable createSelectableDrawable() { | |
} | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||
private static int getAttrId(Context context, String attr) { | ||
SoftAssertions.assertNotNull(attr); | ||
if (SELECTABLE_ITEM_BACKGROUND.equals(attr)) { | ||
return android.R.attr.selectableItemBackground; | ||
} else if (SELECTABLE_ITEM_BACKGROUND_BORDERLESS.equals(attr)) { | ||
return android.R.attr.selectableItemBackgroundBorderless; | ||
} else { | ||
return context.getResources().getIdentifier(attr, "attr", "android"); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onLayout(boolean changed, int l, int t, int r, int b) { | ||
// No-op | ||
|
@@ -225,6 +249,11 @@ public void setRippleColor(ButtonViewGroup view, Integer rippleColor) { | |
view.setRippleColor(rippleColor); | ||
} | ||
|
||
@ReactProp(name = "rippleRadius") | ||
public void setRippleRadius(ButtonViewGroup view, Integer rippleColor) { | ||
jakub-gonet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
view.setRippleRadius(rippleColor); | ||
} | ||
|
||
@Override | ||
protected void onAfterUpdateTransaction(ButtonViewGroup view) { | ||
view.updateBackground(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the float cast was not needed