Custom animation, 3D animation rotation

Source: Internet
Author: User

/**

* Custom animation, using camera can achieve 3D effect call the method in activity image = (ImageView)

* Findviewbyid (R.id.image); Bitmap Bitmap =

* Bitmapfactory.decoderesource (Getresources (), r.drawable.main_back_pic);

* IMAGE.SETIMAGEBITMAP (bitmap); Image.startanimation (New

* Myanimation (Bitmap.getwidth ()/2, Bitmap.getheight ()/2, 3500));

* */

public class Myanimation extends Animation {


private int width;

private int height;

private int duration;//Duration

Private camera camera = new camera ();


public myanimation (int width, int height, int duration) {

This.height = height;

This.width = width;

this.duration = Duration;

}


@Override

public void Initialize (int width, int height, int parentwidth,

int parentheight) {

Setduration (duration);//Set the execution time of the animation

Setfillafter (TRUE);//The animation stops at the last frame of the animation after the animation ends, Setfillbefore (true): stops at the first frame after the animation ends

Animation set to uniform motion, set the motion state, in the XML file settings do not work, must be used in Java code set

Setinterpolator (New Linearinterpolator ());

Super.initialize (width, height, parentwidth, parentheight);

}


@Override

protected void Applytransformation (float interpolatedtime,

Transformation Trans) {

/**

* Two parameters in this method, the first Interpolatedtime, represents the time of the abstract animation, regardless of how long the time, its parameter values are from 0 to 1

* 0 for the animation to start, 1 for the end of the animation, transformation is a change to the animation camera provides methods: Getmatrix (Matrix

* Matrix) applies the changes made by the camera to the Matrix Rotatex (float deg) rotates the target component along the X axis

* Rotatey (float deg) rotates the target component along the y-axis totatez (float deg) rotates the component along the z-axis

* Translate (float x,float y,float z) shifts the target component into three-dimensional space

* Applytocanvas (Canvas canvas) applies the changes made to the camera to the canvas

* */

Super.applytransformation (Interpolatedtime, trans);

Camera.save ();

Switch the target component in a three-dimensional view

At the time of the first call, the value of Nterpolatedtime is 0, which is equivalent to moving the view by 10 pixels, and then getting less, a period past, and turning

Camera.translate (0.0f, 0.0f, (10-10 * interpolatedtime));

Camera.rotatex (Interpolatedtime);

Camera.rotatey (Interpolatedtime);

Camera.rotatez (Interpolatedtime);

Matrix matrix = Trans.getmatrix ();

Camera.getmatrix (matrix);

Pretranslate refers to the translation before Setscale, posttranslate refers to the translation after Setscale

Note that their argument is the distance of the translation, not the coordinates of the translation destination!

Since scaling is centered on (0,0), in order to align the center of the interface with (0,0), it is necessary to pretranslate (-centerx,

-centery),

After the Setscale is complete, call Posttranslate (CenterX,

CenterY), and then move the picture back, so that the animation effect is the activity of the interface image from the center of the constant scaling

CenterX and CenterY are the coordinates of the interface center.

Matrix.pretranslate (-width,-height);

Matrix.posttranslate (width, height);


Camera.restore ();

}

}


This article is from the "Qing gan tea" blog, please be sure to keep this source http://shunshuncon.blog.51cto.com/8232441/1649860

Custom animation, 3D animation rotation

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.