Chinaunix首页 | 论坛 | 博客
  • 博客访问: 837799
  • 博文数量: 182
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 11:49
文章分类

全部博文(182)

文章存档

2019年(1)

2016年(5)

2015年(29)

2014年(38)

2013年(21)

2012年(36)

2011年(52)

我的朋友

分类: Android平台

2014-06-22 17:02:45


点击(此处)折叠或打开

  1. public class FlipImage extends ImageView implements OnClickListener, Rotate3dAnimation.On90Degree {

  2.     private Drawable f;
  3.     private Drawable b;
  4.     private Bitmap bitmap;

  5.     // private Animation a;

  6.     public FlipImage(Context context, AttributeSet attrs) {
  7.         super(context, attrs);

  8.         f = context.getResources().getDrawable(R.drawable.question_test);
  9.         b = context.getResources().getDrawable(R.drawable.quest_test2);
  10.         setImageDrawable(f);
  11.         bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.quest_test2);
  12.         // a = AnimationUtils.loadAnimation(context, R.anim.rotate_anim);
  13.         setOnClickListener(this);
  14.     }

  15.     int count;

  16.     @Override
  17.     public void onClick(View v) {
  18.         float centerX = getWidth() / 2f;
  19.         float centerY = getHeight() / 2f;
  20.         // 构建3D旋转动画对象,旋转角度为0到90度
  21.         Rotate3dAnimation rotation = new Rotate3dAnimation(0, 180, centerX, centerY, 0, false);
  22.         rotation.setOn90DegreeListener(this);
  23.         // 动画持续时间500毫秒
  24.         rotation.setDuration(1000);
  25.         // 动画完成后保持完成状态
  26.         rotation.setFillAfter(true);
  27.         rotation.setInterpolator(new AccelerateInterpolator());
  28.         // 设置动画的监听器
  29.         if (count == 0) {
  30.             count = 1;
  31.         } else {
  32.             count = 0;
  33.         }
  34.         startAnimation(rotation);
  35.     }


  36.     @Override
  37.     public void on90Dgree() {
  38.         if (count == 0) {
  39.             setImageDrawable(f);
  40.         } else {
  41.             setImageDrawable(b);
  42.         }
  43.     }
  44. }

点击(此处)折叠或打开

  1. package com.example.com.leco.miles.cut.text;
  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.         if (degrees >= 90) {
  57.             //达到90度时为了不让图成反的,直接加180度
  58.             degrees += 180;
  59.             if (listener != null) {
  60.                 listener.on90Dgree();
  61.                 listener = null;
  62.             }
  63.         }
  64.         
  65.         final float centerX = mCenterX;
  66.         final float centerY = mCenterY;
  67.         final Camera camera = mCamera;
  68.   
  69.         final Matrix matrix = t.getMatrix();
  70.   
  71.         camera.save();
  72.         if (isReverse) {
  73.             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
  74.         } else {
  75.             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
  76.         }
  77.         camera.rotateY(degrees);
  78.         camera.getMatrix(matrix);
  79.         camera.restore();
  80.   
  81.         matrix.preTranslate(-centerX, -centerY);
  82.         matrix.postTranslate(centerX, centerY);
  83.     }
  84.     
  85.     
  86.     private On90Degree listener;
  87.     
  88.     public void setOn90DegreeListener(On90Degree l) {
  89.         listener = l;
  90.     }
  91.     public interface On90Degree {
  92.         public void on90Dgree();
  93.     }
  94. }



阅读(687) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~