如何在android中设置动画按钮?

时间:2013-08-14 00:57:04

标签: android button animated shake

我正在创建一个Android应用程序,我有一个按钮,可以通向消息传递位置。在使用按钮的活动中,我检查是否有任何未读消息,如果是,我想对按钮做一些事情让用户知道有些东西未读。

我正在考虑按钮sorta水平振动,每2或3秒钟3次震动。

我知道如何在后台运行一个每隔x毫秒运行一次的线程。但我不知道该做什么是水平摇晃3次。

任何人都可以帮忙吗?

我正在考虑使用sin函数,对于动画,我可以使用sin函数的输出来获取上下的值,我可以设置按钮的水平位置......但这似乎也是极端,还有更好的方法吗?

感谢。

5 个答案:

答案 0 :(得分:24)

我无法对@ 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);

这只是做你想做的事情的一种方式,可能有更好的方法。

答案 1 :(得分:22)

在动画文件夹中创建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);

如果您想要垂直动画,请将Xdelta和Xdelta值更改为从Ydelta到Ydelta值

答案 2 :(得分:5)

<小时/> 的 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 目录

中创建 动画 文件夹

右键单击, res - &gt;新 - &gt;目录

将新目录命名为 动画

创建一个新的xml文件名 milkshake

<强> 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" />

答案 3 :(得分: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;
    }
}

uss to:

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

答案 4 :(得分:0)

依赖

将其添加到存储库末尾的根build.gradle中:

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();