Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate button in 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.

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

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.

Can anyone help with this?

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?

like image 759
omega Avatar asked Aug 14 '13 00:08

omega


People also ask

How do you Animate text on Android?

To start the animation we need to call the startAnimation() function on the UI element as shown in the snippet below: sampleTextView. startAnimation(animation); Here we perform the animation on a textview component by passing the type of Animation as the parameter.


1 Answers

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.

like image 69
user2323030 Avatar answered Oct 20 '22 21:10

user2323030