Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@methodin
Last active April 28, 2021 07:25
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save methodin/5678214 to your computer and use it in GitHub Desktop.
Save methodin/5678214 to your computer and use it in GitHub Desktop.
3d rotation animation for Android
/*
Original code found at:
https://code.google.com/p/android-stocker/source/browse/trunk/src/com/twofuse/stocker/Rotate3dAnimation.java?r=2
Use (in set):
Rotate3dAnimation skew = new Rotate3dAnimation(20, 0, 0, 0, 0, 0);
set.addAnimation(skew);
animation = new TranslateAnimation(0, 0, 0, 0, Animation.RELATIVE_TO_SELF, 0.5f, 0, 0);
set.addAnimation(animation);
// set.setStartOffset((position ) * 10);
Use (single view):
Rotate3dAnimation skew = new Rotate3dAnimation(20, 0, 0, 0, 0, 0);
view.startAnimation(skew);
*/
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.graphics.Camera;
import android.graphics.Matrix;
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;
}
@Override
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();
}
@Override
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();
camera.save();
camera.rotateX(xDegrees);
camera.rotateY(yDegrees);
camera.rotateZ(zDegrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-this.width, -this.height);
matrix.postTranslate(this.width, this.height);
}
}
@afrishberg
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

@Ashutosh-Tiwari
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