Skip to content

Instantly share code, notes, and snippets.

Last active April 28, 2021 07:25
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
3d rotation animation for Android
Original code found at:
Use (in set):
Rotate3dAnimation skew = new Rotate3dAnimation(20, 0, 0, 0, 0, 0);
animation = new TranslateAnimation(0, 0, 0, 0, Animation.RELATIVE_TO_SELF, 0.5f, 0, 0);
// set.setStartOffset((position ) * 10);
Use (single view):
Rotate3dAnimation skew = new Rotate3dAnimation(20, 0, 0, 0, 0, 0);
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Rotate3dAnimation extends Animation {
private final float fromXDegrees;
private final float toXDegrees;
private final float fromYDegrees;
private final float toYDegrees;
private final float fromZDegrees;
private final float toZDegrees;
private Camera camera;
private int width = 0;
private int height = 0;
public Rotate3dAnimation(float fromXDegrees, float toXDegrees, float fromYDegrees, float toYDegrees, float fromZDegrees, float toZDegrees) {
this.fromXDegrees = fromXDegrees;
this.toXDegrees = toXDegrees;
this.fromYDegrees = fromYDegrees;
this.toYDegrees = toYDegrees;
this.fromZDegrees = fromZDegrees;
this.toZDegrees = toZDegrees;
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
this.width = width / 2;
this.height = height / 2;
camera = new Camera();
protected void applyTransformation(float interpolatedTime, Transformation t) {
float xDegrees = fromXDegrees + ((toXDegrees - fromXDegrees) * interpolatedTime);
float yDegrees = fromYDegrees + ((toYDegrees - fromYDegrees) * interpolatedTime);
float zDegrees = fromZDegrees + ((toZDegrees - fromZDegrees) * interpolatedTime);
final Matrix matrix = t.getMatrix();;
matrix.preTranslate(-this.width, -this.height);
matrix.postTranslate(this.width, this.height);
Copy link

How can I set the pivot of the rotation?
I want to rotate the view around its bottom. Meaning rotate around the X axis, where pivot is the bottom of the View
I guess this has something to do with the translate matrix, but I cant figure out how to do that

Copy link

Hey there.. Same is my concern, M unable to rotate the image in X axis that the top down direction. Please help @methodin @afrishberg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment