Android利用Camera实现图片的旋转动画 .

MainActivity如下:

  1. package cc.testrotatephoto;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.view.View.OnClickListener;  
  5. import android.view.animation.AccelerateInterpolator;  
  6. import android.view.animation.Animation;  
  7. import android.view.animation.Animation.AnimationListener;  
  8. import android.widget.Button;  
  9. import android.widget.ImageView;  
  10. import android.widget.RelativeLayout;  
  11. import android.app.Activity;  
  12. /** 
  13.  * Demo描述: 
  14.  * 利用Camera实现图片的旋转动画 
  15.  *  
  16.  * 注意事项: 
  17.  * 1 Rotate3dAnimation在APIDemo中的位置Views/Animation/3D Transition 
  18.  * 2 待有空时候再仔细研究Rotate3dAnimation 
  19.  *  
  20.  * 参考资料: 
  21.  * 1 http://blog.csdn.net/guolin_blog/article/details/10766017 
  22.  * 2 http://blog.csdn.net/mapdigit/article/details/7804654 
  23.  * 3 http://wang-peng1.iteye.com/blog/572886 
  24.  *   Thank you very much 
  25.  */  
  26. public class MainActivity extends Activity {  
  27.     private RelativeLayout mRelativeLayout;  
  28.     private ImageView mImageView;  
  29.     private Button mButton;  
  30.     private int count=0;  
  31.     @Override  
  32.     protected void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.main);  
  35.         init();  
  36.     }  
  37.       
  38.     private void init(){  
  39.         mRelativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);  
  40.         mImageView=(ImageView) findViewById(R.id.imageView);  
  41.         mButton=(Button) findViewById(R.id.button);  
  42.         mButton.setOnClickListener(new OnClickListener() {  
  43.             @Override  
  44.             public void onClick(View view) {  
  45.                 float centerX = mRelativeLayout.getWidth() / 2f;  
  46.                 float centerY = mRelativeLayout.getHeight() / 2f;  
  47.                 // 构建3D旋转动画对象,旋转角度为0到90度   
  48.                 Rotate3dAnimation rotation = new Rotate3dAnimation(090, centerX, centerY,310.0f, true);  
  49.                 // 动画持续时间500毫秒   
  50.                 rotation.setDuration(500);  
  51.                 // 动画完成后保持完成状态   
  52.                 rotation.setFillAfter(true);  
  53.                 rotation.setInterpolator(new AccelerateInterpolator());  
  54.                 // 设置动画的监听器   
  55.                 if (count%2==0) {  
  56.                     rotation.setAnimationListener(new RotateToTheSecondImageViewAnimationListener());  
  57.                 }else{  
  58.                     rotation.setAnimationListener(new RotateToTheFirstImageViewAnimationListener());  
  59.                 }  
  60.                 mRelativeLayout.startAnimation(rotation);  
  61.                 count++;  
  62.             }  
  63.         });  
  64.     }  
  65.       
  66.       
  67.     class RotateToTheFirstImageViewAnimationListener implements AnimationListener {  
  68.         @Override  
  69.         public void onAnimationStart(Animation animation) {  
  70.         }  
  71.           
  72.         @Override  
  73.         public void onAnimationEnd(Animation animation) {  
  74.             mImageView.setImageResource(R.drawable.c);  
  75.               
  76.             // 获取布局的中心点位置,作为旋转的中心点   
  77.             float centerX = mRelativeLayout.getWidth() / 2f;  
  78.             float centerY = mRelativeLayout.getHeight() / 2f;  
  79.               
  80.             // 构建3D旋转动画对象,旋转角度为90到0度   
  81.             Rotate3dAnimation rotation = new Rotate3dAnimation(900, centerX, centerY,310.0f, false);  
  82.             // 动画持续时间500毫秒   
  83.             rotation.setDuration(500);  
  84.             // 动画完成后保持完成状态   
  85.             rotation.setFillAfter(true);  
  86.             rotation.setInterpolator(new AccelerateInterpolator());  
  87.             mRelativeLayout.startAnimation(rotation);  
  88.         }  
  89.   
  90.         @Override  
  91.         public void onAnimationRepeat(Animation animation) {  
  92.         }  
  93.   
  94.     }  
  95.       
  96.       
  97.     class RotateToTheSecondImageViewAnimationListener implements AnimationListener {  
  98.   
  99.         @Override  
  100.         public void onAnimationStart(Animation animation) {  
  101.         }  
  102.   
  103.           
  104.         @Override  
  105.         public void onAnimationEnd(Animation animation) {  
  106.             //动画完成后展示另外的图片   
  107.             mImageView.setImageResource(R.drawable.b);  
  108.               
  109.             // 获取布局的中心点位置,作为旋转的中心点   
  110.             float centerX = mRelativeLayout.getWidth() / 2f;  
  111.             float centerY = mRelativeLayout.getHeight() / 2f;  
  112.             // 构建3D旋转动画对象,旋转角度为270到360度   
  113.             Rotate3dAnimation rotation = new Rotate3dAnimation(270360, centerX, centerY,310.0f, false);  
  114.             // 动画持续时间500毫秒   
  115.             rotation.setDuration(500);  
  116.             // 动画完成后保持完成状态   
  117.             rotation.setFillAfter(true);  
  118.             rotation.setInterpolator(new AccelerateInterpolator());  
  119.             mRelativeLayout.startAnimation(rotation);  
  120.         }  
  121.   
  122.         @Override  
  123.         public void onAnimationRepeat(Animation animation) {  
  124.         }  
  125.   
  126.     }  
  127.   
  128. }  
package cc.testrotatephoto;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.app.Activity;
/**
 * Demo描述:
 * 利用Camera实现图片的旋转动画
 * 
 * 注意事项:
 * 1 Rotate3dAnimation在APIDemo中的位置Views/Animation/3D Transition
 * 2 待有空时候再仔细研究Rotate3dAnimation
 * 
 * 参考资料:
 * 1 http://blog.csdn.net/guolin_blog/article/details/10766017
 * 2 http://blog.csdn.net/mapdigit/article/details/7804654
 * 3 http://wang-peng1.iteye.com/blog/572886
 *   Thank you very much
 */
public class MainActivity extends Activity {
	private RelativeLayout mRelativeLayout;
    private ImageView mImageView;
    private Button mButton;
    private int count=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	
	private void init(){
		mRelativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
		mImageView=(ImageView) findViewById(R.id.imageView);
		mButton=(Button) findViewById(R.id.button);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				float centerX = mRelativeLayout.getWidth() / 2f;
				float centerY = mRelativeLayout.getHeight() / 2f;
				// 构建3D旋转动画对象,旋转角度为0到90度
				Rotate3dAnimation rotation = new Rotate3dAnimation(0, 90, centerX, centerY,310.0f, true);
				// 动画持续时间500毫秒
				rotation.setDuration(500);
				// 动画完成后保持完成状态
				rotation.setFillAfter(true);
				rotation.setInterpolator(new AccelerateInterpolator());
				// 设置动画的监听器
				if (count%2==0) {
					rotation.setAnimationListener(new RotateToTheSecondImageViewAnimationListener());
				}else{
					rotation.setAnimationListener(new RotateToTheFirstImageViewAnimationListener());
				}
				mRelativeLayout.startAnimation(rotation);
				count++;
			}
		});
	}
	
	
	class RotateToTheFirstImageViewAnimationListener implements AnimationListener {
		@Override
		public void onAnimationStart(Animation animation) {
		}
		
		@Override
		public void onAnimationEnd(Animation animation) {
			mImageView.setImageResource(R.drawable.c);
			
			// 获取布局的中心点位置,作为旋转的中心点
			float centerX = mRelativeLayout.getWidth() / 2f;
			float centerY = mRelativeLayout.getHeight() / 2f;
			
			// 构建3D旋转动画对象,旋转角度为90到0度
			Rotate3dAnimation rotation = new Rotate3dAnimation(90, 0, centerX, centerY,310.0f, false);
			// 动画持续时间500毫秒
			rotation.setDuration(500);
			// 动画完成后保持完成状态
			rotation.setFillAfter(true);
			rotation.setInterpolator(new AccelerateInterpolator());
			mRelativeLayout.startAnimation(rotation);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
		}

	}
	
	
	class RotateToTheSecondImageViewAnimationListener implements AnimationListener {

		@Override
		public void onAnimationStart(Animation animation) {
		}

		
		@Override
		public void onAnimationEnd(Animation animation) {
			//动画完成后展示另外的图片
			mImageView.setImageResource(R.drawable.b);
			
			// 获取布局的中心点位置,作为旋转的中心点
			float centerX = mRelativeLayout.getWidth() / 2f;
			float centerY = mRelativeLayout.getHeight() / 2f;
			// 构建3D旋转动画对象,旋转角度为270到360度
			Rotate3dAnimation rotation = new Rotate3dAnimation(270, 360, centerX, centerY,310.0f, false);
			// 动画持续时间500毫秒
			rotation.setDuration(500);
			// 动画完成后保持完成状态
			rotation.setFillAfter(true);
			rotation.setInterpolator(new AccelerateInterpolator());
			mRelativeLayout.startAnimation(rotation);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
		}

	}

}


Rotate3dAnimation如下:

  1. package cc.testrotatephoto;  
  2.   
  3. import android.graphics.Camera;  
  4. import android.graphics.Matrix;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.Transformation;  
  7. /** 
  8.  * An animation that rotates the view on the Y axis between two specified angles. 
  9.  * This animation also adds a translation on the Z axis (depth) to improve the effect. 
  10.  */  
  11. public class Rotate3dAnimation extends Animation {  
  12.     private final float mFromDegrees;  
  13.     private final float mToDegrees;  
  14.     private final float mCenterX;  
  15.     private final float mCenterY;  
  16.     private final float mDepthZ;  
  17.     private final boolean isReverse;  
  18.     private Camera mCamera;  
  19.   
  20.     /** 
  21.      * Creates a new 3D rotation on the Y axis. The rotation is defined by its 
  22.      * start angle and its end angle. Both angles are in degrees. The rotation 
  23.      * is performed around a center point on the 2D space, definied by a pair 
  24.      * of X and Y coordinates, called centerX and centerY. When the animation 
  25.      * starts, a translation on the Z axis (depth) is performed. The length 
  26.      * of the translation can be specified, as well as whether the translation 
  27.      * should be reversed in time. 
  28.      * 
  29.      * @param fromDegrees the start angle of the 3D rotation 
  30.      * @param toDegrees the end angle of the 3D rotation 
  31.      * @param centerX the X center of the 3D rotation 
  32.      * @param centerY the Y center of the 3D rotation 
  33.      * @param reverse true if the translation should be reversed, false otherwise 
  34.      */  
  35.     public Rotate3dAnimation(float fromDegrees, float toDegrees,  
  36.             float centerX, float centerY, float depthZ, boolean reverse) {  
  37.         mFromDegrees = fromDegrees;  
  38.         mToDegrees = toDegrees;  
  39.         mCenterX = centerX;  
  40.         mCenterY = centerY;  
  41.         mDepthZ = depthZ;  
  42.         isReverse = reverse;  
  43.     }  
  44.   
  45.     @Override  
  46.     public void initialize(int width, int height, int parentWidth, int parentHeight) {  
  47.         super.initialize(width, height, parentWidth, parentHeight);  
  48.         mCamera = new Camera();  
  49.     }  
  50.   
  51.     @Override  
  52.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  53.         final float fromDegrees = mFromDegrees;  
  54.         float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);  
  55.   
  56.         final float centerX = mCenterX;  
  57.         final float centerY = mCenterY;  
  58.         final Camera camera = mCamera;  
  59.   
  60.         final Matrix matrix = t.getMatrix();  
  61.   
  62.         camera.save();  
  63.         if (isReverse) {  
  64.             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);  
  65.         } else {  
  66.             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));  
  67.         }  
  68.         camera.rotateY(degrees);  
  69.         camera.getMatrix(matrix);  
  70.         camera.restore();  
  71.   
  72.         matrix.preTranslate(-centerX, -centerY);  
  73.         matrix.postTranslate(centerX, centerY);  
  74.     }  
  75. }  
package cc.testrotatephoto;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
/**
 * An animation that rotates the view on the Y axis between two specified angles.
 * This animation also adds a translation on the Z axis (depth) to improve the effect.
 */
public class Rotate3dAnimation extends Animation {
    private final float mFromDegrees;
    private final float mToDegrees;
    private final float mCenterX;
    private final float mCenterY;
    private final float mDepthZ;
    private final boolean isReverse;
    private Camera mCamera;

    /**
     * Creates a new 3D rotation on the Y axis. The rotation is defined by its
     * start angle and its end angle. Both angles are in degrees. The rotation
     * is performed around a center point on the 2D space, definied by a pair
     * of X and Y coordinates, called centerX and centerY. When the animation
     * starts, a translation on the Z axis (depth) is performed. The length
     * of the translation can be specified, as well as whether the translation
     * should be reversed in time.
     *
     * @param fromDegrees the start angle of the 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
     * @param reverse true if the translation should be reversed, false otherwise
     */
    public Rotate3dAnimation(float fromDegrees, float toDegrees,
            float centerX, float centerY, float depthZ, boolean reverse) {
        mFromDegrees = fromDegrees;
        mToDegrees = toDegrees;
        mCenterX = centerX;
        mCenterY = centerY;
        mDepthZ = depthZ;
        isReverse = reverse;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCamera = new Camera();
    }

    @Override
    protected 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 (isReverse) {
            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
        } else {
            camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
        }
        camera.rotateY(degrees);
        camera.getMatrix(matrix);
        camera.restore();

        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}

 

main.xml如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/relativeLayout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/imageView"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="350dip"  
  11.         android:src="@drawable/c" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/button"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_below="@id/imageView"  
  18.         android:layout_centerHorizontal="true"  
  19.         android:layout_marginTop="30dip"  
  20.         android:text="button" />  
  21.   
  22. </RelativeLayout>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值