简体   繁体   English

如何在 android 工作室中弹出按钮 animation

[英]how to do pop up button animation in android studio

In a calculator app that I am creating, I want buttons to animate in a pop-up way (scaling-up) when the button is clicked.在我正在创建的计算器应用程序中,我希望按钮在单击按钮时以弹出方式(按比例放大)进行动画处理。 But that is not the issue.但这不是问题。 I want the button to stay scaled up (bigger size) as long as the user hold the button.只要用户按住按钮,我希望按钮保持放大(更大的尺寸)。 It is supposed to go back to its original size (small size) only when the user releases the button.只有当用户释放按钮时,它才应该 go 恢复到原来的大小(小尺寸)。 In the same way button colour has to change from white to blue as long as user hold the button(back to white when released).同样,只要用户按住按钮,按钮颜色就必须从白色变为蓝色(释放时变回白色)。 Is there any way to do that?有什么办法吗? I am not very good at animations in android studio.我在android工作室的动画不是很擅长。 So please try to explain it in simple words.所以请尝试用简单的语言来解释它。

I am also attaching a video that I made for prototyping to illustrate it.我还附上了我为原型制作制作的视频来说明它。 Click link below https://drive.google.com/file/d/13B8LAuNW1ymhmliw52BEIkMbJAJQRbfw/view?usp=sharing点击下面的链接https://drive.google.com/file/d/13B8LAuNW1ymhmliw52BEIkMbJAJQRbfw/view?usp=sharing

You should create an xml file that have a selector as root element.您应该创建一个将选择器作为根元素的 xml 文件。 This element selects the shape in base of the corrent state like pressed, focused or enabled.该元素选择当前 state 的底部形状,如按下、聚焦或启用。

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@drawable/button_disabled" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/button_pressed" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@drawable/button_focused" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/button_enabled" />
</selector>

If you want a different shape with different color and size you must create a new drawable like this:如果您想要具有不同颜色和大小的不同形状,您必须像这样创建一个新的可绘制对象:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
        android:startColor="#00CCFF"
        android:centerColor="#0000CC"
        android:endColor="#00CCFF"
        android:angle="90"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <stroke
        android:width="2dip"
        android:color="#FFFFFF" />
    <corners android:radius= "8dp" />
</shape>

If you want to change the size you can use the attribute size.如果要更改大小,可以使用属性大小。 Then you must implement the on click listener in the mainActivity onCreate file and set the pressed state of the button to true.那么就必须在mainActivity的onCreate文件中实现点击监听器,并将按钮的按下的state设置为true。 In a nutshell you must create one selector drawable for the button and two drawable one for the state pressed="true" and one for pressed="false".简而言之,您必须为按钮创建一个可绘制选择器,为 state pressed="true" 创建两个可绘制选择器,一个为 pressed="false" 创建可绘制选择器。

Here I put an exaustive example: https://stackoverflow.com/a/29848987/13198061在这里我举了一个详尽的例子: https://stackoverflow.com/a/29848987/13198061

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

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