Android利用Camera实现图片的旋转动画
来源:互联网 发布:java装饰模式好处 编辑:程序博客网 时间:2024/06/05 08:47
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;@Overrideprotected 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() {@Overridepublic 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 {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic 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);}@Overridepublic void onAnimationRepeat(Animation animation) {}}class RotateToTheSecondImageViewAnimationListener implements AnimationListener {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic 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);}@Overridepublic 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>
- Android利用Camera实现图片的旋转动画
- Android利用Camera实现图片的旋转动画 .
- 利用Android的Camera类实现3D旋转功能
- Android利用RotateAnimation实现旋转变化动画
- android 动画rotate实现图片不停旋转的效果
- Android 图片处理之-->android.graphics.Camera实现图像的旋转、缩放
- Android 图片旋转 动画
- android 图片旋转动画
- js实现图片旋转动画的封装
- 图片的旋转动画
- 图片的旋转动画
- 2014-11-8Android学习------Android 实现图片的旋转--------动画Animation学习篇
- Android camera onPreviewFrame 图片旋转问题
- android 实现控件的旋转动画效果
- android 实现控件的旋转动画效果
- Android实现ImageView的旋转动画
- android.graphics.Camera实现图像的旋转、缩放
- android.graphics.Camera实现图像的旋转、缩放,配合Matrix...
- [django]ubuntu下django 开发 环境配置
- js Math.random()
- CSS命名规则
- Mac下XCode超简单跑起暗黑世界客户端
- onpropertychange and oninput事件
- Android利用Camera实现图片的旋转动画
- 快速检测空间三角形相交算法的代码实现(Devillers & Guigue算法)
- 利用python实现hadoop stream操作遇到的Broken pipe错误
- COT (spoj)
- codeforces 263C. Circle of Numbers
- iOS7: 如何获取不变的UDID
- poj 3974 Palindrome 最长回文
- win7 64下暗黑世界V1.1 服务器端及客户端的安装及运行 成功
- matlab中EYEDIAGRAM(X, N)简介