【转】 Camera模仿3D效果的小例子(图片无限旋转)

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.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class FlipImgEffectView extends View {  
    
    private Context context ;  
    private Bitmap showBmp ;  
    private Matrix matrix ; //作用矩阵  
    private Camera camera ;   
    private int deltaX , deltaY ; //翻转角度差值  
    private int centerX , centerY ; //图片中心点  
      
    public FlipImgEffectView(Context context) {  
        super(context);  
        this.context = context ;  
        initData();  
    }  
      
    private void initData(){  
        showBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.pp);  
        centerX = showBmp.getWidth()/2 ;  
        centerY = showBmp.getHeight()/2 ;  
        matrix = new Matrix();  
        camera = new Camera();  
    }    
  
    int lastMouseX ;  
    int lastMouseY ;  
    @Override  
    public boolean dispatchTouchEvent(MotionEvent event) {  
        int x = (int) event.getX();      
        int y = (int) event.getY();      
        switch(event.getAction()) {      
         case MotionEvent.ACTION_DOWN:   
             lastMouseX = x ;  
             lastMouseY = y ;  
             break;      
         case MotionEvent.ACTION_MOVE:  
             int dx = x - lastMouseX ;  
             int dy = y - lastMouseY ;  
             deltaX += dx ;  
             deltaY += dy ;  
             break;      
         }     
         
        invalidate();  
        return true;  
    }  
  
    @Override  
    protected void onDraw(Canvas canvas) {  
          
        camera.save();  
        //绕X轴翻转  
        camera.rotateX(-deltaY);  
        //绕Y轴翻转  
        camera.rotateY(deltaX);  
        
        Log.d("TAG", "-deltaY" + -deltaY) ;
        Log.d("TAG", "deltaX" + -deltaX) ;
        Log.d("TAG", "--------------------------------------") ;
        //设置camera作用矩阵  
        camera.getMatrix(matrix);  
        camera.restore();  
        //设置翻转中心点  
        matrix.preTranslate(-this.centerX, -this.centerY);  
        matrix.postTranslate(this.centerX, this.centerY);  
          
        canvas.drawBitmap(showBmp, matrix, null);  
    }             
  
}  

 

3.png

 

2.png

 

1.png

原文链接:http://www.devdiv.com/thread-104482-1-1.html

posted @ 2013-08-08 16:15  木偶然  阅读(344)  评论(0编辑  收藏  举报