在Android Studio中创建按钮动画

问题描述 投票:0回答:1

我想创建一个按钮,单击它时会改变它的颜色。此按钮应弹出一点,然后弹回或弹回。我已经检查了与此相关的帖子,但我很遗憾地说这对我没用。如果你提到代码的整个部分而不是必要的部分,它将非常有用。非常感谢提前:)我无法发布代码的图像我输入的信誉较少的cos。对此感到抱歉。

java android xml
1个回答
0
投票

只需按钮颜色更改,您可以随时使用Selector drawable作为背景,例如这样!

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary"></solid>
            <corners android:radius="20dp"></corners>

        </shape>

    </item>

<item android:state_pressed="true">
    <shape android:shape="rectangle">
        <solid android:color="#000"></solid>
        <corners android:radius="10dp"></corners>

    </shape>



</item>

//--------------------------------------------------------------------------//

对于想要提升的动画,你可以给出动画大小的感觉,或者为自由形式移动你应该使用翻译动画

这是方法!

1:放一个名为anim的新res文件夹并右键单击它,这样你就可以创建动画资源了,你可以在其中创建一个像“translate_anim.xml”这样的文件夹并将其放入其中

为了改变对象使用标签的位置。它使用fromXDelta,从YDelta到X方向和到XDelta,到YDelta属性用于Y方向。 move.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="75%p"
        android:duration="800" />
</set>

然后在java中你必须使用它来设置动画

Animation translater = AnimUtils.loadAnimation(getApplicationContext,R.anim.translate_anim);

Button btnMoving = findViewbyId(R.id.btn_moving);
btnMoving.startAnimation(btnMoving);

这是用于扩展UP的资源动画文件。

slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="0.0" />

</set>
© www.soinside.com 2019 - 2024. All rights reserved.