Port of Android <ripple>
effect for pre-Lollipop devices (API 16+)
- XML inflating
- Ripple supports different shapes
- Custom drawable loader
- Define your custom drawable tags
This project is originally forked from ozodrukh/RippleDrawable but has been revamped and given some TLC. The parent repository has been stagnant since March 30th, 2016.
Add it to your build.gradle with:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
and:
dependencies {
implementation 'com.github.addisonElliott:RippleDrawable:$LATEST_VERSION'
}
Note: The $LATEST_VERSION
string should be replaced with the latest version. The available versions can be found
here: https://jitpack.io/#addisonElliott/RippleDrawable
The last version to use the Android support libraries was 2.1.0. Version 3.0.0 and above use the AndroidX
library. Periodic releases with the support library will be released based on user demand with the version appended with
-support
(e.g. 2.1.0-support
for $LATEST_VERSION
). It is strongly recommended to upgrade your project to AndroidX.
Create your desired ripple XML and place it in the drawable/
folder. An example ripple effect XML is shown below,
which was named fab_background.xml
in this case.
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_material_light"> <!-- ripple color -->
<!-- for Floating Action Button -->
<item>
<shape android:shape="oval">
<solid android:color="@color/accent_material_dark" />
</shape>
</item>
</ripple>
Next, the ripple XML file is inflated into a RippleDrawable
using LollipopDrawablesCompat.getDrawable
. In addition,
a DrawableHotspotTouch
instance must be created and setup for the View
so that the ripples are shown when the View
is tapped.
public class SampleActivity extends AppCompatActivity {
private FloatingActionButton mActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
mActionButton = (FloatingActionButton) findViewById(R.id.fab);
mActionButton.setBackgroundDrawable(getDrawable2(R.drawable.fab_background));
mActionButton.setClickable(true); // if we don't set it true, ripple will not be played
mActionButton.setOnTouchListener(
new DrawableHotspotTouch((LollipopDrawable) mActionButton.getBackground()));
}
/**
* {@link #getDrawable(int)} is already taken by Android API
* and method is final, so we need to give another name :(
*/
public Drawable getDrawable2(int id) {
return LollipopDrawablesCompat.getDrawable(getResources(), id, getTheme());
}
}
That's it!
You can inflate and create your own Drawable
classes. Here's how you can do it.
- Extend your Drawable from
LollipopDrawable
public class LayerDrawable extends LollipopDrawable {
- Implement your own
inflate
function
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme);
- Register your custom
LollipopDrawable
class
static {
LollipopDrawablesCompat.registerDrawable(RippleDrawable.class, "ripple");
}
- Inflate the custom class!
LollipopDrawablesCompat.getDrawable(getResources(), R.drawable.custom_drawable, getTheme());
The MIT License (MIT)
Copyright (c) 2015 Abdullaev Ozodrukh
Copyright (C) 2019 Addison Elliott
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.