简体   繁体   English

如何实现带有文本模糊和阴影的 Android 按钮动画

[英]How to achieve Android Button Animation with text-blurred and shadow

I want to achieve this button animation in Android.我想在Android中实现这个按钮动画。 The sample below is from iOS.下面的示例来自 iOS。 I want the shadow to disappear when the user clicks the button and reappear once the user releases the button.我希望阴影在用户单击按钮时消失,并在用户释放按钮后重新出现。

Any help will be highly appreciated.任何帮助将不胜感激。

在此处输入图片说明

You need to add color like the following.您需要添加如下颜色。 For example, the color name is button_text_color.xml .例如,颜色名称是button_text_color.xml Here is the .xml file which needs to be put in the color folder.这是需要放在color文件夹中的 .xml 文件。 If the color folder does not exist, create one in your res directory.如果color文件夹不存在,请在您的res目录中创建一个。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#50FFFFFF" android:state_pressed="true" />
    <item android:color="#FFFFFF" android:state_pressed="false" />
</selector>

Check that I have added 50% transparency to the white color in the pressed state.检查我是否在按下状态下为白色添加了 50% 的透明度。 Now just add this attribute in the Button where it is declared.现在只需在声明它的Button中添加此属性。

Now you need a background drawable, to be put in your drawable folder.现在你需要一个可绘制的背景,放在你的drawable文件夹中。 For example, let us take the name of the drawable is button_state_list_animator.xml .例如,让我们取 drawable 的名称是button_state_list_animator.xml This should have the following content.这应该有以下内容。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <layer-list>
            <item>
                <shape>
                    <solid android:color="@android:color/darker_gray" />
                    <corners android:radius="19dp" />
                </shape>
            </item>
            <item android:bottom="5px">
                <shape>
                    <padding android:bottom="5dp" />
                    <gradient android:angle="270" android:endColor="#163969" android:startColor="#1c4985" />
                    <corners android:radius="19dp" />
                    <padding android:bottom="10dp" android:left="10dp" android:right="5dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>

    <item android:state_pressed="true">
        <layer-list>
            <item>
                <shape>
                    <solid android:color="#102746" />
                    <corners android:radius="19dp" />

                </shape>
            </item>
            <item android:bottom="5px">
                <shape>
                    <padding android:bottom="5dp" />
                    <gradient android:angle="270" android:endColor="#163969" android:startColor="#1c4985" />
                    <corners android:radius="19dp" />
                    <padding android:bottom="10dp" android:left="10dp" android:right="5dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

Now your button construction is simple.现在您的按钮结构很简单。

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_margin="32dp"
    android:background="@drawable/button_state_list_animator"
    android:text="Save Changes"
    android:textColor="@color/button_text_color" />

Here is the screenshot from my code.这是我的代码的屏幕截图。

在此处输入图片说明

在此处输入图片说明

You can have the push-down animation using the library as well, as mentioned in an answer here.您也可以使用进行下推动画,如此处的答案中所述。

Hope that helps!希望有帮助!

Use the animation effect (as the code below) on the custom button when clicking on the button.单击按钮时,在自定义按钮上使用动画效果(如下代码)。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXScale="1"
        android:toXScale="0.8"
        android:fromYScale="1"
        android:toYScale="0.8"
        android:duration="500"
        android:pivotX="50%"
        android:pivotY="50%" >
    </scale>
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0.8"
        android:duration="500">
    </alpha>
</set>

set Animation on Button Click using.使用按钮单击设置动画。

 btn_custom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R
                        .anim.animation_btn);
                btn_custom.startAnimation(animation);
            }
        });

try changing alpha values as your need and see the effect.尝试根据需要更改 alpha 值并查看效果。

You can use this library : pushdown-anim-click你可以使用这个库: pushdown-anim-click

How to install:如何安装:

compile( 'com.github.thekhaeng:pushdown-anim-click:1.1.1' ){
    exclude group: 'com.android.support'
}

How to use:如何使用:

Button button = findViewById( R.id.button );

PushDownAnim.setPushDownAnimTo( button, ... )
    .setOnClickListener( new View.OnClickListener(){
        @Override
        public void onClick( View view ){
            Toast.makeText( MainActivity.this, "PUSH DOWN !!", Toast.LENGTH_SHORT ).show();
        }

});

For Shadow:对于阴影:

As I saw library files, There are only three java files.正如我看到的库文件,只有三个 java 文件。 He just used class to animate button.他只是用 class 来制作按钮动画。 You can give shadow in your button by this way .您可以通过这种方式在按钮中添加阴影。 Even though I have created issue You can keep watch on this.即使我已经创建了问题,您也可以对此保持关注。

在此处输入链接描述

For more functionality, you can visit github link.更多功能可以访问github链接。

Thank you.谢谢你。

Add a layout with the background as the shadow below the button.添加一个布局,背景为按钮下方的阴影。 In the button.setOnTouchListener hide the layout when button is pressed and make it visible when released.button.setOnTouchListener ,按下按钮时隐藏布局,释放时使其可见。 However it will only work if the shadow is below.但是,它只有在阴影在下方时才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM