【问题标题】:Android button animation安卓按钮动画
【发布时间】:2017-04-04 19:11:34
【问题描述】:

我像这样在 Android 中创建了一个按钮, Button btn = new Button(getContext());

最初,当我单击该按钮时,它会显示动画。当我按下它时它会变暗,当我松开按钮时它会恢复到原来的颜色。

但是,如果我更改背景颜色,例如btn.setBackgroundColor(Color.RED),点击动画将不再存在,这让我非常恼火。

我想要的很简单,即使我更改了按钮背景颜色,我也只想保留点击动画。


更新:

最后,我为我的项目选择了库 Material,以防有人想知道。

Github 上有几个 Material UI 库向后支持 SDK 版本 EditText。

我尝试了RippleEffectmaterial-rippleMaterial,而Material 在我的情况下效果更好。

【问题讨论】:

  • 为它创建一个 XML。这个人解释的很好:here

标签: android


【解决方案1】:

如果您想用custom colorripple effect 制作button,那么您应该使用ripple drawable

  1. 这是一个ripple drawable,其中按钮background colorREDripple colorWHITE。您可以根据需要更改颜色。

ripple_effect.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#88FFFFFF"> <!-- Ripple color :: WHITE -->

    <item>
        <color android:color="#FF0000" /> <!-- Normal bg color :: RED -->
    </item>

    <!-- Make sure the ripple doesn't exceed the bounds -->
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>
  1. ripple_effect.xml 文件放入您的drawable 文件夹中。
  2. 使用可绘制的ripple_effect 作为button 背景,使用android:background="@drawable/ripple_effect"

这是你的Button XML:

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ripple_effect"
    android:text="Button">

</Button>

适用于 Android api 版本

要实现ripple effect,可以试试balysv/material-ripple库。

  1. 在您的gradle 中,添加以下行:

编译'com.balysv:material-ripple:1.0.2'

  1. buttonMaterialRippleLayout 包装在布局文件中。

  2. 使用 app:mrl_rippleColor="#88FFFFFF" 添加ripple color 并且不要忘记使用app:mrl_rippleOverlay="true"custom colored button 上显示ripple effect

这是您的 Button XML:

<com.balysv.materialripple.MaterialRippleLayout
    android:id="@+id/ripple"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:mrl_rippleColor="#88FFFFFF"
    app:mrl_rippleOverlay="true">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text="Button">

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

输出:

希望对你有帮助~

【讨论】:

  • 我忘了说我项目中的 minSdkVersion 是 16(Android 4.1)。不幸的是,minSdkVersion 16 不支持&lt;Ripple&gt;。还有其他方法可以实现吗?
  • @Tim 查看我的更新答案。对于
  • @Tim 如果我的解决方案有效,请将其标记为正确答案并投票。在此先感谢:)
  • 涟漪图仅适用于 api 21+
【解决方案2】:

如果您想为按钮制作简单的动画,请在 drawble 中创建新的 xml 文件并通过此代码,您可以选择您喜欢的颜色。转到您的按钮并使用您创建的 xml 名称设置背景

`

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#05FFCA"/>
            <corners android:radius="2dp"/>
        </shape>

    </item>

    <item>
        <shape>
            <solid android:color="@android:color/transparent"/>
            <corners android:radius="2dp"/>
            <stroke android:width="1dp" android:color="@color/amber"/>
        </shape>

    </item>
</selector>

`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 2011-11-15
    • 2011-12-04
    • 1970-01-01
    • 2011-01-18
    • 2019-04-20
    相关资源
    最近更新 更多