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

Add dynamic hover duration support #80

Open
wants to merge 3 commits 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
112 changes: 2 additions & 110 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,111 +1,3 @@
Material Ripple Layout
===============
# Material Ripple Layout (with dynamic hover duration)

Ripple effect wrapper for Android Views

![Demo Image][1]

Including in your project
-------------------------

```groovy
compile 'com.balysv:material-ripple:1.0.2'
```

Check for latest version number on the widget below or visit [Releases](https://github.com/balysv/material-ripple/releases)

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.balysv/material-ripple/badge.svg?style=flat)](http://mvnrepository.com/artifact/com.balysv/material-ripple)

Usage
-----

Use static initializer on your `View` (see `xml` attributes below for customization)

```java
MaterialRippleLayout.on(view)
.rippleColor(Color.BLACK)
.create();
```

Or wrap your `View` with `MaterialRippleLayout` in your layout file:

```xml
<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/ripple"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button inside a ripple"/>

</com.balysv.materialripple.MaterialRippleLayout>
```

If using in an `AdapterView` you must set `rippleInAdapter` to `true`


Configure using xml attributes or setters in code:

```java
app:mrl_rippleOverlay="true" // if true, ripple is drawn in foreground; false - background
app:mrl_rippleColor="#ff0000" // color of ripple
app:mrl_rippleAlpha="0.1" // alpha of ripple
app:mrl_rippleDimension="10dp" // radius of hover and starting ripple
app:mrl_rippleHover="true" // if true, a hover effect is drawn when view is touched
app:mrl_rippleRoundedCorners="10dp" // radius of corners of ripples. Note: it uses software rendering pipeline for API 17 and below
app:mrl_rippleInAdapter="true" // if true, MaterialRippleLayout will optimize for use in AdapterViews
app:mrl_rippleDuration="350" // duration of ripple animation
app:mrl_rippleFadeDuration="75" // duration of fade out effect on ripple
app:mrl_rippleDelayClick="true" // if true, delays calls to OnClickListeners until ripple effect ends
app:mrl_rippleBackground="#FFFFFF" // background under ripple drawable; used with rippleOverlay="false"
app:mrl_ripplePersistent="true" // if true, ripple background color persists after animation, until setRadius(0) is called
```

Set an `OnClickListener` to `MaterialRippleLayout`:

```java
findViewById(R.id.ripple).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
// handle me
}
});
```

Or if using in an `AdapterView`, simply use `OnItemClickListener`

Support for Android api versions < 14
-----

For those unlucky developers that need to support older versions than 14, there's a way to do it.

You can use this library in addition with Jake Wharton's animation backport (http://nineoldandroids.com/) changing the imports from ` import android.animation.*;` to: ` import com.nineoldandroids.animation.*;` ,
`import android.util.Property`; to `import com.nineoldandroids.util.Property;` and in MaterialRippleLayout.java file, calling function `shouldDelayChildPressedState()` only if you're using api greater than 14.


Developed By
--------------------
Balys Valentukevicius

License
-----------

```
Copyright 2015 Balys Valentukevicius

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```

[1]: https://raw.github.com/balysv/material-ripple/master/art/demo.gif
This is a fork of [Material Ripple Layout](https://github.com/balysv/material-ripple) with dynamic hover duration support.
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,22 @@ public class MaterialRippleLayout extends FrameLayout {
private static final int DEFAULT_COLOR = Color.BLACK;
private static final int DEFAULT_BACKGROUND = Color.TRANSPARENT;
private static final boolean DEFAULT_HOVER = true;
private static final int DEFAULT_HOVER_DURATION = 2500;
private static final boolean DEFAULT_DELAY_CLICK = true;
private static final boolean DEFAULT_PERSISTENT = false;
private static final boolean DEFAULT_SEARCH_ADAPTER = false;
private static final boolean DEFAULT_RIPPLE_OVERLAY = false;
private static final int DEFAULT_ROUNDED_CORNERS = 0;

private static final int FADE_EXTRA_DELAY = 50;
private static final long HOVER_DURATION = 2500;

private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Rect bounds = new Rect();

private int rippleColor;
private boolean rippleOverlay;
private boolean rippleHover;
private int rippleHoverDuration;
private int rippleDiameter;
private int rippleDuration;
private int rippleAlpha;
Expand Down Expand Up @@ -132,6 +133,7 @@ public MaterialRippleLayout(Context context, AttributeSet attrs, int defStyle) {
);
rippleOverlay = a.getBoolean(R.styleable.MaterialRippleLayout_mrl_rippleOverlay, DEFAULT_RIPPLE_OVERLAY);
rippleHover = a.getBoolean(R.styleable.MaterialRippleLayout_mrl_rippleHover, DEFAULT_HOVER);
rippleHoverDuration = a.getInt(R.styleable.MaterialRippleLayout_mrl_rippleHoverDuration, DEFAULT_HOVER_DURATION);
rippleDuration = a.getInt(R.styleable.MaterialRippleLayout_mrl_rippleDuration, DEFAULT_DURATION);
rippleAlpha = (int) (255 * a.getFloat(R.styleable.MaterialRippleLayout_mrl_rippleAlpha, DEFAULT_ALPHA));
rippleDelayClick = a.getBoolean(R.styleable.MaterialRippleLayout_mrl_rippleDelayClick, DEFAULT_DELAY_CLICK);
Expand Down Expand Up @@ -313,7 +315,7 @@ private void startHover() {
}
final float radius = (float) (Math.sqrt(Math.pow(getWidth(), 2) + Math.pow(getHeight(), 2)) * 1.2f);
hoverAnimator = ObjectAnimator.ofFloat(this, radiusProperty, rippleDiameter, radius)
.setDuration(HOVER_DURATION);
.setDuration(rippleHoverDuration);
hoverAnimator.setInterpolator(new LinearInterpolator());
hoverAnimator.start();
}
Expand Down Expand Up @@ -575,6 +577,10 @@ public void setRippleHover(boolean rippleHover) {
this.rippleHover = rippleHover;
}

public void setRippleHoverDuration(int rippleHoverDuration) {
this.rippleHoverDuration = rippleHoverDuration;
}

public void setRippleDelayClick(boolean rippleDelayClick) {
this.rippleDelayClick = rippleDelayClick;
}
Expand Down Expand Up @@ -699,6 +705,7 @@ public static class RippleBuilder {
private int rippleColor = DEFAULT_COLOR;
private boolean rippleOverlay = DEFAULT_RIPPLE_OVERLAY;
private boolean rippleHover = DEFAULT_HOVER;
private int rippleHoverDuration = DEFAULT_HOVER_DURATION;
private float rippleDiameter = DEFAULT_DIAMETER_DP;
private int rippleDuration = DEFAULT_DURATION;
private float rippleAlpha = DEFAULT_ALPHA;
Expand Down Expand Up @@ -729,6 +736,11 @@ public RippleBuilder rippleHover(boolean hover) {
return this;
}

public RippleBuilder rippleHoverDuration(int hoverDuration) {
this.rippleHoverDuration = hoverDuration;
return this;
}

public RippleBuilder rippleDiameterDp(int diameterDp) {
this.rippleDiameter = diameterDp;
return this;
Expand Down Expand Up @@ -783,6 +795,7 @@ public MaterialRippleLayout create() {
layout.setRippleDuration(rippleDuration);
layout.setRippleFadeDuration(rippleFadeDuration);
layout.setRippleHover(rippleHover);
layout.setRippleHoverDuration(rippleHoverDuration);
layout.setRipplePersistent(ripplePersistent);
layout.setRippleOverlay(rippleOverlay);
layout.setRippleBackground(rippleBackground);
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<attr name="mrl_rippleDuration" format="integer" localization="suggested" />
<attr name="mrl_rippleFadeDuration" format="integer" localization="suggested" />
<attr name="mrl_rippleHover" format="boolean" localization="suggested" />
<attr name="mrl_rippleHoverDuration" format="integer" localization="suggested" />
<attr name="mrl_rippleBackground" format="color" localization="suggested" />
<attr name="mrl_rippleDelayClick" format="boolean" localization="suggested" />
<attr name="mrl_ripplePersistent" format="boolean" localization="suggested" />
Expand Down