如何在android的动画按钮? [英] How to animate button in android?

查看:109
本文介绍了如何在android的动画按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个Android应用程序,我有一个按钮,导致消息的地方。在与该按钮的活动,我检查是否有未读消息,如果是的话我想做点什么的按钮,让用户知道有一些未读。

I am making an android app, and I have a button which leads to a messaging place. On the activity with the button, I check if there is any unread messages, and if so I want to do something to the button to let the user know that there is something unread.

我在想有按钮八九不离十振动水平状3甩头每2〜3秒。

I was thinking of having the button sorta vibrate horizontally like 3 shakes every 2 or 3 seconds.

我知道如何在其中做一些事情每x毫秒后台运行一个线程。但我不知道是什么做的是横向摇晃3次。

I know how to run a thread in the background which does something every x milliseconds. But what I don't know what to do is shake it horizontally 3 times.

谁能帮助呢?

我想用sin函数,为动画,我可以使用输出一个正弦函数来获取上升和下降,这是我可以设置按钮的水平位置值的......但是,这似乎太极端情况下,有没有更好的办法?

I was thinking of using the sin function, for the animation, I can use output from a sin function to get values that go up and down, which I can set the horizontal position of the button... But this seems too extreme, is there a better way?

感谢。

推荐答案

我不能发表评论,@欧米茄的评论,因为我没有足够的声誉,但这个问题的答案应该是这样的:

I can't comment on @omega's comment because I don't have enough reputation but the answer to that question should be something like:

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

这是做你想要的只是一种方法,可能会有更好的方法在那里。

This is just one way of doing what you want, there may be better methods out there.

这篇关于如何在android的动画按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆