【问题标题】:Ripple effect for icons in android?android中图标的涟漪效应?
【发布时间】:2015-07-06 12:38:53
【问题描述】:

我正在尝试向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 个补丁图像。给这一切增加涟漪效应的部分在哪里?

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    该文件是“旧”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>
    

    【讨论】:

    • 但是我使用了那个文件名platforms->android-21。怎么会老?
    • 上述句子中的错字。 *拿了
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-05-30
      • 2015-07-03
      • 2019-12-07
      • 1970-01-01
      • 2016-04-25
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      相关资源
      最近更新 更多