未命名.jpg  
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);
}

}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()