Java 如何模仿棒棒糖(材质设计)按钮动画?

Java 如何模仿棒棒糖(材质设计)按钮动画?,java,android,xml,animation,material-design,Java,Android,Xml,Animation,Material Design,有一种叫做Widget.Material.Button的样式,它有一种我非常喜欢的动画效果(它会在你的点击周围“飞溅”),我希望能够在我的代码中重用它 我试图使它有一个透明的背景色,因为默认情况下它是灰色的,这在应用于我的ViewGroup对象时并不好看 有没有一种简单的方法可以保留动画,但去掉背景色 我已经尝试设置属性android:background=“@android:color/transparent”,但这会导致动画完全停止工作 <?xml version="1.0" enco

有一种叫做
Widget.Material.Button
的样式,它有一种我非常喜欢的动画效果(它会在你的点击周围“飞溅”),我希望能够在我的代码中重用它

我试图使它有一个透明的背景色,因为默认情况下它是灰色的,这在应用于我的
ViewGroup
对象时并不好看

有没有一种简单的方法可以保留动画,但去掉背景色

我已经尝试设置属性
android:background=“@android:color/transparent”
,但这会导致动画完全停止工作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@android:style/Widget.Material.Button"
    tools:targetApi="lollipop">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, world!"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is just another line of text."/>

</LinearLayout>

很抱歉最初给出了错误的位。看得更深一点的问题是,背景色已经被定义为材质的一部分。颜色高光的扩展。具有讽刺意味的是,他们有来自其他地方但不是这里的来源

这样做的目的是创建两个新的xml文件来定义属性

在vales-v21/styles.xml中,您可以按自己喜欢的方式重新定义这些值,或者删除不希望覆盖的值

如果您只是想要RIPPLE,请使用底部定义的可绘制代码

 <style name="MyMaterialButton" parent=android:Widget.Material.Button>
        <item name="background">@drawable/material_background_ripple</item>
        <item name="textAppearance">?attr/textAppearanceButton</item>
        <item name="minHeight">48dip</item>
        <item name="minWidth">88dip</item>
        <item name="stateListAnimator">@anim/button_state_list_anim_material</item>
        <item name="focusable">true</item>
        <item name="clickable">true</item>
        <item name="gravity">center_vertical|center_horizontal</item>
    </style>

@可拉伸/材料\u背景\u波纹
?属性/文本外观按钮
48dip
88度
@动画/按钮\状态\列表\动画\材质
真的
真的
中心|垂直|中心|水平
然后选择按钮背景样式 /drawable-v21/material\u background\u ripple.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@android:color/PICK_RIPPLE_COLOR_HERE">
    <item android:drawable="@drawable/btn_default_mtrl_shape" />
</ripple>

修订版: 你试过了吗 customView.getWindow().setBackgroundDrawableResource(R.color.transparent)


?

很抱歉最初给出了错误的位。看得更深一点的问题是,背景色已经被定义为材质的一部分。颜色高光的扩展。具有讽刺意味的是,他们有来自其他地方但不是这里的来源

这样做的目的是创建两个新的xml文件来定义属性

在vales-v21/styles.xml中,您可以按自己喜欢的方式重新定义这些值,或者删除不希望覆盖的值

如果您只是想要RIPPLE,请使用底部定义的可绘制代码

 <style name="MyMaterialButton" parent=android:Widget.Material.Button>
        <item name="background">@drawable/material_background_ripple</item>
        <item name="textAppearance">?attr/textAppearanceButton</item>
        <item name="minHeight">48dip</item>
        <item name="minWidth">88dip</item>
        <item name="stateListAnimator">@anim/button_state_list_anim_material</item>
        <item name="focusable">true</item>
        <item name="clickable">true</item>
        <item name="gravity">center_vertical|center_horizontal</item>
    </style>

@可拉伸/材料\u背景\u波纹
?属性/文本外观按钮
48dip
88度
@动画/按钮\状态\列表\动画\材质
真的
真的
中心|垂直|中心|水平
然后选择按钮背景样式 /drawable-v21/material\u background\u ripple.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@android:color/PICK_RIPPLE_COLOR_HERE">
    <item android:drawable="@drawable/btn_default_mtrl_shape" />
</ripple>

修订版: 你试过了吗 customView.getWindow().setBackgroundDrawableResource(R.color.transparent)

?

如“可点击视图”中所述,您可以使用android:background=“?attr/selectableItemBackground:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?attr/selectableItemBackground">
  ...
</LinearLayout>

...
如“可点击视图”中所述,您可以使用android:background=“?attr/selectableItemBackground:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?attr/selectableItemBackground">
  ...
</LinearLayout>

...

对于
View
对象,没有称为
getWindow()
的方法。我已经尝试过通过编程设置背景色,但它不起作用。设置
活动
窗口的背景色也没有帮助。请阅读上面的内容。我想我一开始误解了你的问题。对于
View
对象,没有称为
getWindow()
的方法。我已经尝试过通过编程设置背景色,但它不起作用。设置
活动
窗口的背景色也没有帮助。请阅读上面的内容。我想我一开始误解了你的问题。很好,提供的链接也很有用。很好,提供的链接也很有用。