Extraordinary Animations with Paths in android (2024)

Extraordinary Animations with Paths in android (3)

In this Article we are going to fly a plane or the screen

Path is an android class it is very useful or many time only choice in android for creating arbitrary shapes and drawings arbitrary figures , Path is also used in animations. But often times working with paths is very painful. If you don’t want to bear that pain anymore bear with me for rest of the article.

So we will fly a plane on a random path using path animations.

Taking a Random Path in Life

I am going to generate a random path on screen, our plane will fly on that path only.

Extraordinary Animations with Paths in android (4)

here the key method is quadTo() which is used to generate a quadratic bezier curve , I am creating multiple bezier curves which are connected to each other the starting point of each path (bezier curve) is called a contour in paths language, there is also a cubicTo() which is used to create a cubic bezier curve which has 2 control points.

I encourage you to learn more about bezier curve so that you understand what are control points and how above code works under the hood.

We want to fly a plane along a path to be able to do that we need to know about x and y co-ordinates of points which forms this path so that we can keep setting x and y or our plane on those co-ordinates to produce an animation to know about those co-ordinates we need to understand a really important class.

PathMeasure is best friend class of a Path class , a path is kind of shy it won’t directly tell you about its personal things like it’s length , tangent of a line at any point on it’s body , length of a segment between two points , whether or not it is closed etc.

To handle paths you’d like as much information as you can so you may want PathMeasure as common friend , I’ll familiarize you to it along the way.

getPosTan

getPosTan(float distance, float[] pos, float[] tan) this method is used to get co-ordinates of point on a path a given distance.

It takes three arguments

distance we will get the co-ordinates at this distance

pos the x and y co-ordinates are written in this float array

tan the tangent in radian is written in this array

Extraordinary Animations with Paths in android (5)

nextContour

Our path is made of several quadTo() commands getposTan() or other methods of PathMeasure do not operate of entire path at once rather they operate on one command at a time in our case you can say a contour is one path which is created by one quadTo() call for moving to the next we call nextContour() this method returns true if there is a next path false if we are done with entire path.

So if we continuously keep getting these co-ordinates for subsequent distances on the path we can put an object at those co-ordinates and it will appear moving , that is what we are going to do now

Extraordinary Animations with Paths in android (6)

here we are animating from 0f to 1fit will give us co-ordinates at different lengths which is tried to explain in image above after getting a co-ordinate we set the x and y of our plane at those co-ordinates when we are done with a contour on a curve we jump to the next one using nextContour() until the entire path is done.

now we just take the pos in which co-ordinates were written during animation and place our plane on these co-ordinates since we are invalidating in value animation on each animated value the animation will look smooth.

Extraordinary Animations with Paths in android (7)

but hey planes do not fly backwards or sideways we can not go against nature and break the laws of physics we need to do something about this.

Taking a right direction in Life

Do you remember the third argument to getPostTan() which gives as tangent at any point on the curve also described in image above, it will help us to align the tip of the plane in the right direction.

here we are getting tangent to pos then converting this angle to degrees are setting rotation of plane accordingly.

Extraordinary Animations with Paths in android (8)

One Plane is not enough to defeat our enemy we need more fighters , I am just going to use and array of planes and and array of pathmeasures one path measure for each plane then just modify our fly plane function a bit to accept a plane and a pathmeasure.

Extraordinary Animations with Paths in android (9)

If you find it interesting you can find complete code on my Github

and also check out part II 👇

Extraordinary Animations using Trigonometry and Coroutines (Radar Animation)In the previous article we flew some jets to fight our enemies , in this article we will focus how to defend our…proandroiddev.com
Extraordinary Animations with Paths in android (2024)
Top Articles
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 5951

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.