ITeye博客

3D桌面效果动画类

hbczzsw · 2010-12-16 23:07 · 编程语言 · XML · Markdown

  1. public class CubeAnimation extends Animation implements
  2. Animation.AnimationListener {
  3. public static final int FACE_LEFT = 0;
  4. public static final int FACE_RIGHT = 1;
  5. private int mInd;
  6. private float mFromDegrees;
  7. private float mToDegrees;
  8. private float mCenterX;
  9. private float mCenterY;
  10. private int mHorizonType = 1;
  11. private float mHorizonValue = 0.5F;
  12. private Camera mCamera;
  13. private View mView;
  14. public CubeAnimation(int ind) {
  15. this.mInd = ind;
  16. this.mFromDegrees = 0.0F;
  17. this.mToDegrees = 90.0F;
  18. }
  19. public CubeAnimation(Context context, AttributeSet attrs) {
  20. super(context, attrs);
  21. TypedArray a = context.obtainStyledAttributes(attrs,
  22. R.styleable.CubeAnimation);
  23. this.mFromDegrees = 0.0F;
  24. this.mToDegrees = 90.0F;
  25. Description d = Description.parseValue(a.peekValue(0));
  26. this.mHorizonType = d.type;
  27. this.mHorizonValue = d.value;
  28. this.mInd = a.getInt(1, 0);
  29. boolean t = a.getBoolean(2, true);
  30. if (!(t)) {
  31. this.mInd = (1 - this.mInd);
  32. this.mToDegrees = 0.0F;
  33. this.mFromDegrees = 90.0F;
  34. }
  35. a.recycle();
  36. }
  37. public void onAnimationStart(Animation anim) {
  38. this.mView.setVisibility(View.VISIBLE);
  39. }
  40. public void onAnimationEnd(Animation anim) {
  41. this.mView.setVisibility((this.mInd == 0) ? 0 : 8);
  42. this.mInd = (1 - this.mInd);
  43. }
  44. public void onAnimationRepeat(Animation anim) {
  45. }
  46. public static void startCubeAnimation(Context context, int id, View view1,
  47. View view2) {
  48. XmlPullParser parser;
  49. try {
  50. parser = context.getResources().getAnimation(id);
  51. AttributeSet attrs = Xml.asAttributeSet(parser);
  52. int type = parser.getEventType();
  53. int depth = parser.getDepth();
  54. while (true) {
  55. while (true) {
  56. if ((((type = parser.next()) == 3) && (parser.getDepth() <= depth))
  57. || (type == 1))
  58. break label172;
  59. if (type == 2)
  60. break;
  61. }
  62. String name = parser.getName();
  63. if (name.equals("cube")) {
  64. CubeAnimation anim1 = new CubeAnimation(context, attrs);
  65. anim1.mInd = 1;
  66. anim1.mView = view1;
  67. anim1.setAnimationListener(anim1);
  68. CubeAnimation anim2 = new CubeAnimation(context, attrs);
  69. anim2.mInd = 0;
  70. anim2.mView = view2;
  71. anim2.setAnimationListener(anim2);
  72. view1.startAnimation(anim1);
  73. label172: view2.startAnimation(anim2);
  74. }
  75. }
  76. } catch (Resources.NotFoundException ex) {
  77. Log.e("CubeAnimation", "NotFoundException");
  78. } catch (XmlPullParserException ex) {
  79. Log.e("CubeAnimation", "XmlPullParserException");
  80. } catch (IOException ex) {
  81. Log.e("CubeAnimation", "IOException");
  82. }
  83. }
  84. public void initialize(int width, int height, int parentWidth,
  85. int parentHeight) {
  86. super.initialize(width, height, parentWidth, parentHeight);
  87. this.mCenterX = resolveSize(1, 0.5F, width, parentWidth);
  88. this.mCenterY = resolveSize(1, 0.5F, height, parentHeight);
  89. if (this.mHorizonType == 0) {
  90. this.mHorizonValue /= height;
  91. }
  92. this.mCamera = new Camera();
  93. }
  94. protected void applyTransformation(float interpolatedTime, Transformation t) {
  95. float fromDegrees = this.mFromDegrees;
  96. float degrees = fromDegrees + (this.mToDegrees - fromDegrees)
  97. * interpolatedTime;
  98. float centerX = this.mCenterX;
  99. float centerY = this.mCenterY;
  100. Camera camera = this.mCamera;
  101. Matrix matrix = t.getMatrix();
  102. camera.save();
  103. float b = 0.0F;
  104. float e = -this.mHorizonValue;
  105. if (this.mInd == 0) {
  106. degrees += 90.0F;
  107. }
  108. camera.rotateY(degrees);
  109. camera.getMatrix(matrix);
  110. camera.restore();
  111. if (this.mInd == 0) {
  112. matrix.preScale(-1.0F, 1.0F, centerX, 0.0F);
  113. }
  114. float tranX = 320.0F * interpolatedTime;
  115. float tranY = -centerY * e + b;
  116. matrix.preTranslate(0.0F, centerY * e);
  117. matrix.postTranslate(tranX, tranY);
  118. }
  119. protected static class Description {
  120. public int type;
  121. public float value;
  122. static Description parseValue(TypedValue value) {
  123. Description d = new Description();
  124. if (value == null) {
  125. d.type = 0;
  126. d.value = 0.0F;
  127. } else {
  128. if (value.type == 6) {
  129. d.type = (((value.data & 0xF) == 1) ? 2 : 1);
  130. d.value = TypedValue.complexToFloat(value.data);
  131. return d;
  132. }
  133. if (value.type == 4) {
  134. d.type = 0;
  135. d.value = value.getFloat();
  136. return d;
  137. }
  138. if ((value.type >= 16) && (value.type <= 31)) {
  139. d.type = 0;
  140. d.value = value.data;
  141. return d;
  142. }
  143. }
  144. d.type = 0;
  145. d.value = 0.0F;
  146. return d;
  147. }
  148. }
  149. }

评论(1)

  1. #1 楼 · public_private · 2012-03-20 16:16

    发几张效果图就更好啦 :)

评论功能维护中,当前仅展示历史评论。