Android 3D rotation animation camera and Matrix

Source: Internet
Author: User
Tags in degrees

The previous two blogs are all about Android 2D animations, what if you want to make a very cool 3D animation effect? Android does not provide 3D animation interface to the user, so we have to rewrite such a 3D interface animation ourselves.

The interface is as follows:

/* * @Title: My3danimation.java * @Description:todo< Please describe what this file does > * @author: XJP * @data: September 15, 2014 Morning 8:54:10 * @ve rsion:v1.0 */package com.xjp.animator;import Android.graphics.camera;import android.graphics.matrix;import Android.view.animation.animation;import android.view.animation.transformation;/** * todo< Please describe what this class is for > * * @ Author XJP * @data: September 15, 2014 Morning 8:54:10 * @version: V1.0 */public class My3danimation extends Animation {private final FL Oat mfromdegrees;private final float mtodegrees;private final float mcenterx;private final float mcentery;private final fl Oat mdepthz;private Camera mcamera;private int mdirection;private final static int rotate_x = 0;//Rotate Private final stat along the X axis IC int rotate_y = 1;//rotates along the y axis/** * Creates a new 3D rotation on the y axis. The rotation is defined by its * start angle and its end angle. Both angles is in degrees. The rotation * is performed around a center point in the 2D space, definied by a pair of * X and Y coordinates, called CEN TErX and CenterY. When the animation * starts, a translation on the Z axis (depth) is performed. The length of * The translation can is specified, as well as whether the translation * should is reversed in time. * * @param direction * The direction of the 3D rotation * @param fromdegrees * The start angle of t  He 3D rotation * @param todegrees * The end angle of the 3D rotation * @param centerx * The X Center of the 3D rotation * @param centery * The Y center of the 3D rotation */public my3danimation (int direction, FL Oat fromdegrees, float todegrees,float CenterX, float centery, float depthz) {mdirection = Direction;mfromdegrees = FromDe Grees;mtodegrees = Todegrees;mcenterx = Centerx;mcentery = Centery;mdepthz = Depthz;} @Overridepublic void Initialize (int width, int height, int parentwidth,int parentheight) {super.initialize (width, height , Parentwidth, parentheight); Mcamera = new Camera ();} @Overrideprotected void Applytransformation(float interpolatedtime, transformation t)  {Final float fromdegrees = mfromdegrees;float degrees = fromdegrees+ ((mtodegrees-fromdegrees) * interpolatedtime); final float CenterX = mcenterx;final float centery = mcentery;final camera camera = mcamera;final Matrix matrix = T.getmatrix () ; Camera.save (); if (centerx!=0) {if (Interpolatedtime < 0.5) {camera.translate (0.0f, 0.0f, Mdepthz * interpolatedtime) ;} else {camera.translate (0.0f, 0.0f, Mdepthz * (1.0f-interpolatedtime));}} Switch (mdirection) {case ROTATE_X:camera.rotateX (degrees); break;case ROTATE_Y:camera.rotateY (degrees); Camera.getmatrix (matrix); Camera.restore (); Matrix.pretranslate (-centerx,-centery); Matrix.posttranslate (CenterX, centery);}}

The sample code is as follows:

Package Com.xjp.animator;import Com.xjp.animator.r;import Android.os.bundle;import android.view.view;import Android.view.viewgroup;import Android.view.animation.linearinterpolator;import Android.widget.ImageView;import Android.app.activity;public class Mainactivity extends Activity implementsandroid.view.View.OnClickListener {private ImageView img1;private ImageView img2;private ImageView img3;private viewgroup mcontainer;private final static int ROTATE _x = 0;private final static int rotate_y = 1;private my3danimation my3danimation; @Overrideprotected void OnCreate (Bundle s Avedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); FindViews ();} /** * todo< Please describe what this method is for > * * @throw * @return void * @param */private void Findviews () {//TODO auto-generated Meth Od stubmcontainer = (viewgroup) Findviewbyid (R.id.container); Mcontainer.setpersistentdrawingcache ( Viewgroup.persistent_animation_cache); img1 = (ImageView) Findviewbyid (r.id.img_left); Img1.setoNclicklistener (this), Img2 = (ImageView) Findviewbyid (r.id.img_right); Img2.setonclicklistener (this); IMG3 = ( ImageView) Findviewbyid (r.id.img_3); Img3.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubfloat CenterX = V.getwidth ()/2.0f;float CenterY = V.getheight ()/2.0f;if (V.getid () = = R.id.img_left) {my3danimation = new my3danimation (rotate_x, 0, Centerx,center Y, 310f);} if (v.getid () = = r.id.img_right) {my3danimation = new my3danimation (rotate_y, 0,, Centerx,centery, 310f);} if (v.getid () = = r.id.img_3) {CenterX = 0;my3danimation = new My3danimation (rotate_y, 0, Centerx,centery, 310f);} My3danimation.setduration (+); My3danimation.setinterpolator (new Linearinterpolator ()); My3danimation.setfillafter (True); V.startanimation (my3danimation);}}

End.

Android 3D rotation animation camera and Matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.