android中图标的涟漪效应?

时间:2015-07-06 12:38:53

标签: android material-design

我正在尝试为ImageView添加涟漪效果。我知道如果你添加以下背景,你可以模拟图像视图触摸时的涟漪效果。

android:background="?android:attr/selectableItemBackground"

我挖得更深,看看它真正含有什么(在侧面平台v21中检查)。它实际上指向名为item_background.xml的文件。该文件的内容为:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_window_focused="false" android:drawable="@color/transparent" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/list_selector_background_focused" />
    <item android:drawable="@color/transparent" />

</selector>

所有这些导致一堆9个补丁图像。哪个部分会为所有这些添加涟漪效应?

2 个答案:

答案 0 :(得分:0)

该文件是'旧'item_background,显示按下/聚焦等时的黄色图像,如Android 2所示。

相反,请查看文件item_background_material.xml,该文件可在{ANDROID_HOME}/platforms/android-22/data/res/drawable中找到:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project

     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.
-->

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item android:id="@id/mask">
        <color android:color="@color/white" />
    </item>
</ripple>

答案 1 :(得分:-1)

使用此库。它非常简单,只需将图像视图放到容器中即可。 https://github.com/traex/RippleEffect