見過沒有用opengl的3D動畫,看了一下,是用的Camera實(shí)現(xiàn)的,內(nèi)部機(jī)制實(shí)際上還是opengl,不過大大簡化了使用。 Camera就像一個攝像機(jī),一個物體在原地不動,然后我們帶著這個攝像機(jī)四處移動,在攝像機(jī)里面呈現(xiàn)出來的畫面,就會有立體感,就可以從各個角度觀看這個物體。 它有旋轉(zhuǎn)、平移的一系列方法,實(shí)際上都是在改變一個Matrix對象,一系列操作完畢之后,我們得到這個Matrix,然后畫我們的物體,就可以了。 常用的API如下: rotateX(float degree) 繞著x軸旋轉(zhuǎn)degree個度數(shù) rotateY(float degree) 繞著y軸旋轉(zhuǎn)degree個度數(shù) rotateZ(float degree) 繞著z軸旋轉(zhuǎn)degree個度數(shù) translate(float x,float y,float z) 平移一段距離 save()和restore() 作用跟Canvas的一樣,保存原狀態(tài),操作完之后,恢復(fù)到原狀態(tài)。 CubeView.java package longshuai.com; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Camera; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * 圖片三維翻轉(zhuǎn) * @author */ public class CubeView extends View { //攝像機(jī) private Camera mCamera; //翻轉(zhuǎn)用的圖片 private Bitmap face; private Matrix mMatrix = new Matrix(); private Paint mPaint = new Paint(); private int mLastMotionX, mLastMotionY; //圖片的中心點(diǎn)坐標(biāo) private int centerX, centerY; //轉(zhuǎn)動的總距離,跟度數(shù)比例1:1 private int deltaX, deltaY; //圖片寬度高度 private int bWidth, bHeight; public CubeView(Context context,AttributeSet attributeSet) { super(context,attributeSet); setWillNotDraw(false); mCamera = new Camera(); mPaint.setAntiAlias(true); face = BitmapFactory.decodeResource(getResources(), R.drawable.x); bWidth = face.getWidth(); bHeight = face.getHeight(); centerX = bWidth>>1; centerY = bHeight>>1; } /** * 轉(zhuǎn)動 * @param degreeX * @param degreeY */ void rotate(int degreeX, int degreeY) { deltaX += degreeX; deltaY += degreeY; mCamera.save(); mCamera.rotateY(deltaX); mCamera.rotateX(-deltaY); mCamera.translate(0, 0, -centerX); mCamera.getMatrix(mMatrix); mCamera.restore(); //以圖片的中心點(diǎn)為旋轉(zhuǎn)中心,如果不加這兩句,就是以(0,0)點(diǎn)為旋轉(zhuǎn)中心 mMatrix.preTranslate(-centerX, -centerY); mMatrix.postTranslate(centerX, centerY); mCamera.save(); postInvalidate(); } @Override public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: mLastMotionX = x; mLastMotionY = y; break; case MotionEvent.ACTION_MOVE: int dx = x - mLastMotionX; int dy = y - mLastMotionY; rotate(dx, dy); mLastMotionX = x; mLastMotionY = y; break; case MotionEvent.ACTION_UP: break; } return true; } @Override public void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); canvas.drawBitmap(face, mMatrix, mPaint); } }
main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas./apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <longshuai.com.CubeView android:id="@+id/cv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> CubeViewActivity.java package longshuai.com; import android.app.Activity; import android.os.Bundle; public class CubeViewActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 用鼠標(biāo)點(diǎn)點(diǎn)擊移動的效果如下。。
|