Camera就像一个摄像机,一个物体在原地不动,然后我们带着这个摄像机四处移动,在摄像机里面呈现出来的画面,就会有立体感,就可以从各个角度观看这个物体。
它有旋转、平移的一系列方法,实际上都是在改变一个Matrix对象,一系列操作完毕之后,我们得到这个Matrix,然后画我们的物体,就可以了。
使用步骤:
Matrix matrix = new Matrix(); Camera c = new Camera(); c.save(); //进行旋转平移操作 c.getMatrix(matrix); //生成对应操作的矩阵保存到matrix 中。 c.restore();
Camera只有两中操作,translate和rotate。
生成的Matrix 是变换的关键。
public class MyView extends View {
private Bitmap mBitmap;
private Matrix mMatrix = new Matrix();
Camera c=new Camera();
public MyView(Context context) {
super (context);
initialize();
}
private void initialize() {
Bitmap bmp =
((BitmapDrawable)getResources().getDrawable(R.drawable.sample_2)).getBitmap();
mBitmap = bmp;
c.save();
c.rotateY(60);
c.getMatrix(mMatrix);
c.restore();
}
@Override protected void onDraw(Canvas canvas) {
// super.onDraw(canvas); //如果界面上还有其他元素需要绘制,只需要将这句话写上就行了。
canvas.drawBitmap(mBitmap, mMatrix, null );
}
}
变换API接口: