Android利用Camera实现图片的旋转动画
jopen
11年前
MainActivity如下:
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如下:
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如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="350dip" android:src="@drawable/c" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/imageView" android:layout_centerHorizontal="true" android:layout_marginTop="30dip" android:text="button" /> </RelativeLayout>