Android 3D 旋转

 1 public class Rotate3dAnimation extends Animation {  
    // 开始角度
2 private final float mFromDegrees;
    // 结束角度
3 private final float mToDegrees;
    // 旋转的x坐标
4 private final float mCenterX;
    // 旋转的y坐标
5 private final float mCenterY;
    // 旋转的z坐标
6 private final float mDepthZ;
    // 是否扭曲
7 private final boolean mReverse; 8 private Camera mCamera; 9 10 /** 11 * Creates a new 3D rotation on the Y axis. The rotation is defined by its 12 * start angle and its end angle. Both angles are in degrees. The rotation 13 * is performed around a center point on the 2D space, definied by a pair 14 * of X and Y coordinates, called centerX and centerY. When the animation 15 * starts, a translation on the Z axis (depth) is performed. The length 16 * of the translation can be specified, as well as whether the translation 17 * should be reversed in time. 18 * 19 * @param fromDegrees the start angle of the 3D rotation 20 * @param toDegrees the end angle of the 3D rotation 21 * @param centerX the X center of the 3D rotation 22 * @param centerY the Y center of the 3D rotation 23 * @param reverse true if the translation should be reversed, false otherwise 24 */ 25 public Rotate3dAnimation(float fromDegrees, float toDegrees, 26 float centerX, float centerY, float depthZ, boolean reverse) { 27 mFromDegrees = fromDegrees;    28 mToDegrees = toDegrees; 29 mCenterX = centerX; 30 mCenterY = centerY; 31 mDepthZ = depthZ; 32 mReverse = reverse; 33 } 34 35 @Override 36 public void initialize(int width, int height, int parentWidth, int parentHeight) { 37 super.initialize(width, height, parentWidth, parentHeight); 38 mCamera = new Camera(); 39 } 40 41 @Override 42 protected void applyTransformation(float interpolatedTime, Transformation t) { 43 final float fromDegrees = mFromDegrees; 44 float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); 45 46 final float centerX = mCenterX; 47 final float centerY = mCenterY; 48 final Camera camera = mCamera; 49 50 final Matrix matrix = t.getMatrix(); 51 52 camera.save(); 53 if (mReverse) { 54 camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); 55 } else { 56 camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); 57 } 58 camera.rotateY(degrees); 59 camera.getMatrix(matrix); 60 camera.restore(); 61 62 matrix.preTranslate(-centerX, -centerY); 63 matrix.postTranslate(centerX, centerY); 64 } 65 }

调用:

 1  img = (ImageView) view.findViewById(R.id.img);
 2         img.setOnClickListener(new View.OnClickListener() {
 3             @Override
 4             public void onClick(View v) {
 5                 if (!isFront){
 6                     applyRotation(0, 180);
 7                     isFront = true;
 8                 }else {
 9                     applyRotation(180, 360);
10                     isFront = false;
11                 }
12             }
13         });
14 
15  private void applyRotation(float start, float end) {
16         // 计算中心点
17         final float centerX = img.getWidth() / 2.0f;
18         final float centerY = img.getHeight() / 2.0f;
19         final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,
20                 centerX, centerY, 0f, true);
21         rotation.setDuration(1000);
22         rotation.setFillAfter(true);
23         rotation.setInterpolator(new AccelerateInterpolator());
24         // 设置监听
25         rotation.setAnimationListener(new DisplayNextView());
26         img.startAnimation(rotation);
27         test.startAnimation(rotation);
28         test.postDelayed(new Runnable() {
29             @Override
30             public void run() {
31                 if (isFront){
32                     test.setText("我是大帝");
33                 }else{
34                     test.setText("世界你好");
35                 }
36             }
37         },700);
38     }
39     private final class DisplayNextView implements Animation.AnimationListener {
40         public void onAnimationStart(Animation animation) {
41         }
42         // 动画结束
43         public void onAnimationEnd(Animation animation) {
44 
45         }
46 
47         public void onAnimationRepeat(Animation animation) {
48         }
49     }

 

posted on 2016-02-25 15:19  画家丶  阅读(276)  评论(0编辑  收藏  举报