Android 图片三维翻转的效果

 
  • package chroya.demo.rotate3d;   
  •   
  • 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.view.MotionEvent;   
  • import android.view.View;   
  •   
  • /**  
  •  * 图片三维翻转  
  •  * @author chroya  
  •  */  
  • 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) {   
  •         super(context);   
  •         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(00, -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);          
  •     }   
  • }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值