3D桌面效果动画类
public class CubeAnimation extends Animation implements Animation.AnimationListener { public static final int FACE_LEFT = 0; public static final int FACE_RIGHT = 1; private int mInd; private float mFromDegrees; private float mToDegrees; private float mCenterX; private float mCenterY; private int mHorizonType = 1; private float mHorizonValue = 0.5F; private Camera mCamera; private View mView; public CubeAnimation(int ind) { this.mInd = ind; this.mFromDegrees = 0.0F; this.mToDegrees = 90.0F; } public CubeAnimation(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CubeAnimation); this.mFromDegrees = 0.0F; this.mToDegrees = 90.0F; Description d = Description.parseValue(a.peekValue(0)); this.mHorizonType = d.type; this.mHorizonValue = d.value; this.mInd = a.getInt(1, 0); boolean t = a.getBoolean(2, true); if (!(t)) { this.mInd = (1 - this.mInd); this.mToDegrees = 0.0F; this.mFromDegrees = 90.0F; } a.recycle(); } public void onAnimationStart(Animation anim) { this.mView.setVisibility(View.VISIBLE); } public void onAnimationEnd(Animation anim) { this.mView.setVisibility((this.mInd == 0) ? 0 : 8); this.mInd = (1 - this.mInd); } public void onAnimationRepeat(Animation anim) { } public static void startCubeAnimation(Context context, int id, View view1, View view2) { XmlPullParser parser; try { parser = context.getResources().getAnimation(id); AttributeSet attrs = Xml.asAttributeSet(parser); int type = parser.getEventType(); int depth = parser.getDepth(); while (true) { while (true) { if ((((type = parser.next()) == 3) && (parser.getDepth() <= depth)) || (type == 1)) break label172; if (type == 2) break; } String name = parser.getName(); if (name.equals("cube")) { CubeAnimation anim1 = new CubeAnimation(context, attrs); anim1.mInd = 1; anim1.mView = view1; anim1.setAnimationListener(anim1); CubeAnimation anim2 = new CubeAnimation(context, attrs); anim2.mInd = 0; anim2.mView = view2; anim2.setAnimationListener(anim2); view1.startAnimation(anim1); label172: view2.startAnimation(anim2); } } } catch (Resources.NotFoundException ex) { Log.e("CubeAnimation", "NotFoundException"); } catch (XmlPullParserException ex) { Log.e("CubeAnimation", "XmlPullParserException"); } catch (IOException ex) { Log.e("CubeAnimation", "IOException"); } } public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); this.mCenterX = resolveSize(1, 0.5F, width, parentWidth); this.mCenterY = resolveSize(1, 0.5F, height, parentHeight); if (this.mHorizonType == 0) { this.mHorizonValue /= height; } this.mCamera = new Camera(); } protected void applyTransformation(float interpolatedTime, Transformation t) { float fromDegrees = this.mFromDegrees; float degrees = fromDegrees + (this.mToDegrees - fromDegrees) * interpolatedTime; float centerX = this.mCenterX; float centerY = this.mCenterY; Camera camera = this.mCamera; Matrix matrix = t.getMatrix(); camera.save(); float b = 0.0F; float e = -this.mHorizonValue; if (this.mInd == 0) { degrees += 90.0F; } camera.rotateY(degrees); camera.getMatrix(matrix); camera.restore(); if (this.mInd == 0) { matrix.preScale(-1.0F, 1.0F, centerX, 0.0F); } float tranX = 320.0F * interpolatedTime; float tranY = -centerY * e + b; matrix.preTranslate(0.0F, centerY * e); matrix.postTranslate(tranX, tranY); } protected static class Description { public int type; public float value; static Description parseValue(TypedValue value) { Description d = new Description(); if (value == null) { d.type = 0; d.value = 0.0F; } else { if (value.type == 6) { d.type = (((value.data & 0xF) == 1) ? 2 : 1); d.value = TypedValue.complexToFloat(value.data); return d; } if (value.type == 4) { d.type = 0; d.value = value.getFloat(); return d; } if ((value.type >= 16) && (value.type <= 31)) { d.type = 0; d.value = value.data; return d; } } d.type = 0; d.value = 0.0F; return d; } }}