如何在安卓中实现按钮动画?

30

我正在制作一款安卓应用程序,其中有一个按钮可以进入消息界面。在该按钮所在的活动页面上,我会检查是否有未读消息,如果有,我希望对按钮进行一些操作,以便提醒用户有未读消息。

我想让按钮横向震动,每2到3秒钟震动3次左右。

我知道如何在后台运行一个线程,每x毫秒做一些事情。但是我不知道如何让它横向震动3次。

有人能帮忙吗?

我考虑使用正弦函数来进行动画设计,我可以利用正弦函数的输出值获取上下波动的数值,并将其设置为按钮的水平位置... 但这似乎过于复杂了,还有更好的方法吗?


你想要动画效果还是按钮按下的效果? - Prachi
7个回答

32
我不能对@omega的评论发表评论,因为我声望不够,但是那个问题的答案应该是这样的:

shake.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"          <!-- how long the animation lasts -->
    android:fromDegrees="-5"        <!-- how far to swing left -->
    android:pivotX="50%"            <!-- pivot from horizontal center -->
    android:pivotY="50%"            <!-- pivot from vertical center -->
    android:repeatCount="10"        <!-- how many times to swing back and forth -->
    android:repeatMode="reverse"    <!-- to make the animation go the other way -->
    android:toDegrees="5" />        <!-- how far to swing right -->

Class.java

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
view.startAnimation(shake);

这只是实现您想要的方式之一,可能还有更好的方法。

我该如何水平地摇它? - Srikar Reddy

28

在anim文件夹中创建shake.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromXDelta="0" 
        android:toXDelta="10" 
            android:duration="1000" 
                android:interpolator="@anim/cycle" />

并且在anim文件夹中有cycle.xml

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:cycles="4" />

现在在您的代码上添加动画效果

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
anyview.startAnimation(shake);

如果你想要垂直动画,请将 fromXdelta 和 toXdelta 的值改为 fromYdelta 和 toYdelta 的值。


3
这只是让它向右移动,然后立即返回到原始位置,重复4次,但我该如何使它向右移动,然后向左移动,再向右移动,然后向左移动回到原始位置,就像抖动效果一样? - omega
如何使这个动画持续不断地发生? - CraZyDroiD
这个答案对我在安卓中实现摇晃动画很有帮助。 - Abhishek Kumar
嘿@RVG,感谢你的代码,但是我该如何改变震动速度? - Raunak Pandey

6


Class.Java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_with_the_button);

    final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.milkshake);
    Button myButton = (Button) findViewById(R.id.new_game_btn);
    myButton.setAnimation(myAnim);
}
onClick事件
myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        v.startAnimation(myAnim);
    }
});

res目录下创建anim文件夹

右键单击res -> New -> Directory

命名新目录为anim

创建一个名为milkshake的新xml文件


milkshake.xml

<?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="100"
        android:fromDegrees="-5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="10"
        android:repeatMode="reverse"
        android:toDegrees="5" />


0

依赖关系

将以下代码添加到根目录的build.gradle文件末尾的repositories中:

allprojects {
repositories {
    ...
    maven { url "https://jitpack.io" }
}}

然后添加依赖项 dependencies { compile 'com.github.varunest:sparkbutton:1.0.5' }

用法

XML

<com.varunest.sparkbutton.SparkButton
        android:id="@+id/spark_button"
        android:layout_width="40dp"
        android:layout_height="40dp"
        app:sparkbutton_activeImage="@drawable/active_image"
        app:sparkbutton_inActiveImage="@drawable/inactive_image"
        app:sparkbutton_iconSize="40dp"
        app:sparkbutton_primaryColor="@color/primary_color"
        app:sparkbutton_secondaryColor="@color/secondary_color" />

Java(可选)

SparkButton button  = new SparkButtonBuilder(context)
            .setActiveImage(R.drawable.active_image)
            .setInActiveImage(R.drawable.inactive_image)
            .setDisabledImage(R.drawable.disabled_image)
            .setImageSizePx(getResources().getDimensionPixelOffset(R.dimen.button_size))
            .setPrimaryColor(ContextCompat.getColor(context, R.color.primary_color))
            .setSecondaryColor(ContextCompat.getColor(context, R.color.secondary_color))
            .build();

0
在 Kotlin 中,可以通过以下方式定义一个 View:

在 XML 中定义一个 View:

 val playDel = findViewById<ImageView>(R.id.player_del)

在 Android Lib 中找到一个动画:

val imgAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_out)

连接到视图

 playDel.animation=imgAnim

0

首先创建 shake.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"          
    android:fromDegrees="-5"        
    android:pivotX="50%"            
    android:pivotY="50%"            
    android:repeatCount="10"        
    android:repeatMode="reverse"    
    android:toDegrees="5" /> 

   

Class.java

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
view.startAnimation(shake);

0
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class HeightAnimation extends Animation {
    protected final int originalHeight;
    protected final View view;
    protected float perValue;

    public HeightAnimation(View view, int fromHeight, int toHeight) {
        this.view = view;
        this.originalHeight = fromHeight;
        this.perValue = (toHeight - fromHeight);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (originalHeight + perValue * interpolatedTime);
        view.requestLayout();
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

转换为:

HeightAnimation heightAnim = new HeightAnimation(view, view.getHeight(), viewPager.getHeight() - otherView.getHeight());
heightAnim.setDuration(1000);
view.startAnimation(heightAnim);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接