---
title: "3D桌面效果动画类"
author: "hbczzsw"
date: "2010-12-16T23:07:50Z"
tags: ["XML"]
categories: ["编程语言"]
slug: "3d"
draft: false
lastmod: "2010-12-16T23:07:50Z"
id: 845077
url: "https://hbczzsw.iteye.com/blog/845077"
---

001. **public** **class** CubeAnimation **extends** Animation **implements**
002. Animation.AnimationListener {
003. **public** **static** **final** **int** FACE\_LEFT = 0;
004. **public** **static** **final** **int** FACE\_RIGHT = 1;
005. **private** **int** mInd;
006. **private** **float** mFromDegrees;
007. **private** **float** mToDegrees;
008. **private** **float** mCenterX;
009. **private** **float** mCenterY;
010. **private** **int** mHorizonType = 1;
011. **private** **float** mHorizonValue = 0.5F;
012. **private** Camera mCamera;
013. **private** View mView;
014. **public** CubeAnimation(**int** ind) {
015. **this**.mInd = ind;
016. **this**.mFromDegrees = 0.0F;
017. **this**.mToDegrees = 90.0F;
018. }
019. **public** CubeAnimation(Context context, AttributeSet attrs) {
020. **super**(context, attrs);
021. TypedArray a = context.obtainStyledAttributes(attrs,
022. R.styleable.CubeAnimation);
023. **this**.mFromDegrees = 0.0F;
024. **this**.mToDegrees = 90.0F;
025. Description d = Description.parseValue(a.peekValue(0));
026. **this**.mHorizonType = d.type;
027. **this**.mHorizonValue = d.value;
028. **this**.mInd = a.getInt(1, 0);
029. **boolean** t = a.getBoolean(2, **true**);
030. **if** (!(t)) {
031. **this**.mInd = (1 - **this**.mInd);
032. **this**.mToDegrees = 0.0F;
033. **this**.mFromDegrees = 90.0F;
034. }
035. a.recycle();
036. }
037. **public** **void** onAnimationStart(Animation anim) {
038. **this**.mView.setVisibility(View.VISIBLE);
039. }
040. **public** **void** onAnimationEnd(Animation anim) {
041. **this**.mView.setVisibility((**this**.mInd == 0) ? 0 : 8);
042. **this**.mInd = (1 - **this**.mInd);
043. }
044. **public** **void** onAnimationRepeat(Animation anim) {
045. }
046. **public** **static** **void** startCubeAnimation(Context context, **int** id, View view1,
047. View view2) {
048. XmlPullParser parser;
049. **try** {
050. parser = context.getResources().getAnimation(id);
051. AttributeSet attrs = Xml.asAttributeSet(parser);
052. **int** type = parser.getEventType();
053. **int** depth = parser.getDepth();
054. **while** (**true**) {
055. **while** (**true**) {
056. **if** ((((type = parser.next()) == 3) && (parser.getDepth() &lt;= depth))
057. || (type == 1))
058. **break** label172;
059. **if** (type == 2)
060. **break**;
061. }
062. String name = parser.getName();
063. **if** (name.equals("cube")) {
064. CubeAnimation anim1 = **new** CubeAnimation(context, attrs);
065. anim1.mInd = 1;
066. anim1.mView = view1;
067. anim1.setAnimationListener(anim1);
068. CubeAnimation anim2 = **new** CubeAnimation(context, attrs);
069. anim2.mInd = 0;
070. anim2.mView = view2;
071. anim2.setAnimationListener(anim2);
072. view1.startAnimation(anim1);
073. label172: view2.startAnimation(anim2);
074. }
075. }
076. } **catch** (Resources.NotFoundException ex) {
077. Log.e("CubeAnimation", "NotFoundException");
078. } **catch** (XmlPullParserException ex) {
079. Log.e("CubeAnimation", "XmlPullParserException");
080. } **catch** (IOException ex) {
081. Log.e("CubeAnimation", "IOException");
082. }
083. }
084. **public** **void** initialize(**int** width, **int** height, **int** parentWidth,
085. **int** parentHeight) {
086. **super**.initialize(width, height, parentWidth, parentHeight);
087. **this**.mCenterX = resolveSize(1, 0.5F, width, parentWidth);
088. **this**.mCenterY = resolveSize(1, 0.5F, height, parentHeight);
089. **if** (**this**.mHorizonType == 0) {
090. **this**.mHorizonValue /= height;
091. }
092. **this**.mCamera = **new** Camera();
093. }
094. **protected** **void** applyTransformation(**float** interpolatedTime, Transformation t) {
095. **float** fromDegrees = **this**.mFromDegrees;
096. **float** degrees = fromDegrees + (**this**.mToDegrees - fromDegrees)
097. * interpolatedTime;
098. **float** centerX = **this**.mCenterX;
099. **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 &gt;= 16) && (value.type &lt;= 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. }