打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android的3D旋转——使用android.graphics.Camera

见过没有用opengl的3D动画,看了一下,是用的Camera实现的,内部机制实际上还是opengl,不过大大简化了使用。
          Camera就像一个摄像机,一个物体在原地不动,然后我们带着这个摄像机四处移动,在摄像机里面呈现出来的画面,就会有立体感,就可以从各个角度观看这个物体。
        它有旋转、平移的一系列方法,实际上都是在改变一个Matrix对象,一系列操作完毕之后,我们得到这个Matrix,然后画我们的物体,就可以了。

 

常用的API如下:

rotateX(float degree)    绕着x轴旋转degree个度数
    rotateY(float degree)      绕着y轴旋转degree个度数
    rotateZ(float degree)      绕着z轴旋转degree个度数
    translate(float x,float y,float z)  平移一段距离
    save()和restore()  作用跟Canvas的一样,保存原状态,操作完之后,恢复到原状态。

 

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;

/**
 * 图片三维翻转
 * @author
 */
public class CubeView extends View {
 //摄像机
 private Camera mCamera;
 
 //翻转用的图片
 private Bitmap face; 
 private Matrix mMatrix = new Matrix();
 private Paint mPaint = new Paint();

 private int mLastMotionX, mLastMotionY;
 
 //图片的中心点坐标
 private int centerX, centerY;
 //转动的总距离,跟度数比例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;
 } 
 
 /**
  * 转动
  * @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(); 
  //以图片的中心点为旋转中心,如果不加这两句,就是以(0,0)点为旋转中心
  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.android.com/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);
    }
}

 

用鼠标点点击移动的效果如下。。

 

 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
利用OpenCV在Android上DIY相机——第二届 Google 暑期大学生博客分享大...
利用RotateAnimation旋转图片的问题 - 移动平台 / Android
android ImageView 的几点经验总结
Android Canvas
实现Android图片圆角
Android中TextView显示彩虹字
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服