Share
Introduction
In this brief post I’ll go over how to implement an animation on some hard-coded text in an Android app. To achieve this effect, we will be using both a translate animation class and a bounce interpolator class, both provided by the Android SDK.
[Edit:] Some readers wanted to see what this effect looked like, and I was thinking the same thing when I first published this. So below is a sample of what the effect looks.
In this brief post I’ll go over how to implement an animation on some hard-coded text in an Android app. To achieve this effect, we will be using both a translate animation class and a bounce interpolator class, both provided by the Android SDK.[Edit:] Some readers wanted to see what this effect looked like, and I was thinking the same thing when I first published this. So below is a sample of what the effect looks.
The Setup
To demonstrate a simple bounce animation, we will capture any touch events and “bounce” some hard-coded text to the touched location with a horizontal offset of 100.To start out, we’ll create our own View to work with, called BounceView. First we create a new Android application, and then add a new class file. We inherit from View, and prepare to override the below methods.
1 |
public class BounceView extends View { |
The touch event is where we will capture the location where we will insert our bounce animation. The draw event is where we will actually draw our hard-coded text.
Capturing Touch Events
In the touch event, we will save off the touched coordinates for use in the draw event. Here, we will also reset our animation, and associate said animation with our custom view. Our touch event now looks like below.
1 |
float _currentX; |
We have introduced two new class level variables to hold the coordinates. In the touch event, we create a new TranslateAnimation object, telling it to start our animation 100 pixels to the left of the starting location, with no vertical offset. We then set the animation interpolator to a new BounceInterpolator object, which will give us a bounce effect during the animation. Then, we set the duration of the entire animation to 1000 ms. Finally, we start the animation.
Drawing it
The draw event is very bare bones, and is shown below.Here we are simply instantiation a new Paint object, setting the color to white and the text size to 40, and then using that new brush to draw our hard-coded test on the canvas.
The final results
Unfortunately still shots don’t do the app justice, as the whole point is an animation; just don’t forget to change your call to setContentView in the main activity to use a new instance of our custom view that we defined above. The effect is that anywhere you click on your screen, our canned text of “hey there” appears 100 pixels to the left and slides over to where we clicked, bouncing into position.Next steps
The Android SDK provides a number of different animations that you can swap in for our TranslateAnimation, such as:In addition to these, we have a handful of different interpolators that we can incorporate as well:
- AccelerateDecelerateInterpolator
- AccelerateInterpolator
- AnticipateInterpolator
- AnticipateOvershootInterpolator
- CycleInterpolator
- DecelarateInterpolator
- LinearInterpolator
- OvershootInterpolator
Via: Barebones Coder





