我正在使用AppCompat
主题为Lollipop之前的版本提供材料主题。当我将图像资源设置为导航图标或Toolbar
中的菜单按钮时,单击它们会产生涟漪效果(动态效果)。但是,当我尝试在工具栏内部放置相同的图标时,它们就像普通视图一样,点击时没有任何动态效果。
这些涟漪是如何创建的?Android能够在我提供的所有可绘制对象上创建涟漪吗?我如何使涟漪出现在我在ImageView
中使用的图像资源上?
我正在使用AppCompat
主题为Lollipop之前的版本提供材料主题。当我将图像资源设置为导航图标或Toolbar
中的菜单按钮时,单击它们会产生涟漪效果(动态效果)。但是,当我尝试在工具栏内部放置相同的图标时,它们就像普通视图一样,点击时没有任何动态效果。
这些涟漪是如何创建的?Android能够在我提供的所有可绘制对象上创建涟漪吗?我如何使涟漪出现在我在ImageView
中使用的图像资源上?
您可以通过在XML布局中设置图像的背景来创建涟漪效果。android:background="?android:attr/selectableItemBackground"
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight" />
你可以使用涟漪效果来实现。
在 Android API 9+ 中实现 Material Design 的涟漪效果。
该库可在 Maven Central 上获得,你可以通过 Gradle 找到它,请查找。
dependencies {
compile 'com.github.traex.rippleeffect:library:1.3'
}
用法
RippleView
在XML布局文件中声明一个RippleView,内容可以是ImageView或其他。
<com.andexert.library.RippleView
android:id="@+id/more"
android:layout_width="?android:actionBarSize"
android:layout_height="?android:actionBarSize"
android:layout_toLeftOf="@+id/more2"
android:layout_margin="5dp"
rv_centered="true">
<ImageView
android:layout_width="?android:actionBarSize"
android:layout_height="?android:actionBarSize"
android:src="@android:drawable/ic_menu_edit"
android:layout_centerInParent="true"
android:padding="10dp"
android:background="@android:color/holo_blue_dark"/>
</com.andexert.library.RippleView>
rippleView.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
@Override
public void onComplete(RippleView rippleView) {
Log.d("Sample", "Ripple completed");
}
});
有关代码和更多信息,请参见此链接:
https://github.com/traex/RippleEffect
对于一些更多的设计: