Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 實現3D動畫旋轉效果

android 實現3D動畫旋轉效果

編輯:關於Android編程


一個LinearLayout有一張背景圖片,裡面有一個ImageButton子組件,子組件完成360度順時針旋轉效果。

MainActivity:

我只是啟動Activity就顯示動畫效果,所以在oncreate()方法中模擬,但是這時候imageButton尺寸大小的值不太容易獲取,我上一篇文章已經講到了這個問題。

oncreate中獲取組件尺寸大小: http://blog.csdn.net/u010152805/article/details/17592083

public class MainActivity extends Activity {

	private ImageButton ib;
	float w,y;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
         //在oncreate()中獲取imageButton尺寸大小
         ViewTreeObserver vto = ib.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			
			@Override
			public void onGlobalLayout() {
				ib.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				w=ib.getWidth();
				y=ib.getHeight();
				rotateAnim(w,y);
			}
		});
	}
	
	/**
	 * 實現動畫旋轉效果
	 */
	private void rotateAnim(float w,float y) {
		//圖片旋轉中心坐標
		final float centerX = w/ 2.0f;
		final float centerY = y/ 2.0f;
		RotateAnimation rotation;
		        rotation = new RotateAnimation(0, -360, centerX, centerY, 310.0f,
				         false);
		rotation.setDuration(1500);//動畫持續時間
		rotation.setFillAfter(true);
		rotation.setInterpolator(new DecelerateInterpolator());//設置加速度
		rotation.setRepeatCount(Animation.INFINITE);//重復次數
		rotation.setRepeatMode(Animation.RESTART);//重復動畫
		ib.startAnimation(rotation);

	}
}
動畫類:

/**
 * 3D動畫效果類
 * 三個關鍵類 (Animation/Camera/Matrix)
 */
public class RotateAnimation extends Animation {
	/**開始角度*/
	private final float mFromDegrees;
	/**結束角度*/
	private final float mToDegrees;
	/**中心坐標X軸*/
	private final float mCenterX;
	/**中心坐標Y軸*/
	private final float mCenterY;
	/**反面*/
	private final boolean mReverse;
	/**相機類*/
	private Camera mCamera;

	/**
	 * 構造方法
	 * @param fromDegrees 開始角度
	 * @param toDegrees 結束角度
	 * @param centerX 中心坐標
	 * @param centerY 中心坐標
	 * @param depthZ
	 * @param reverse
	 */
	public RotateAnimation(float fromDegrees, float toDegrees,
			float centerX, float centerY, float depthZ, boolean reverse) {
		mFromDegrees = fromDegrees;
		mToDegrees = toDegrees;
		mCenterX = centerX;
		mCenterY = centerY;
		mReverse = 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 tran) {
		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 = tran.getMatrix();
		camera.save();//保存狀態
		if (mReverse) {
			//x,y,z距離中心軸各自的距離長度
			camera.translate(0.0f, 0.0f, 0.0f);  
		} else {
			camera.translate(0.0f, 0.0f, 0.0f);
		}
		//以多少角度圍繞Y軸旋轉,可以圍繞X,Y,Z三個方向旋轉
		camera.rotateY(degrees);
		camera.getMatrix(matrix);
		//恢復保存狀態
		camera.restore();
		matrix.preTranslate(-centerX, -centerY);
		matrix.postTranslate(centerX, centerY);
	}
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved